1 22 package org.jboss.test.jca.bank.ejb; 23 24 25 26 import java.rmi.RemoteException ; 27 import java.sql.Connection ; 28 import java.sql.PreparedStatement ; 29 import java.sql.ResultSet ; 30 import java.util.Collection ; 31 import javax.ejb.CreateException ; 32 import javax.ejb.EJBException ; 33 import javax.ejb.EntityBean ; 34 import javax.ejb.EntityContext ; 35 import javax.ejb.FinderException ; 36 import javax.ejb.NoSuchEntityException ; 37 import javax.ejb.ObjectNotFoundException ; 38 import javax.sql.DataSource ; 39 40 import javax.naming.InitialContext ; 41 import java.util.ArrayList ; 42 import java.sql.SQLException ; 43 44 import org.jboss.logging.Logger; 45 46 47 60 public class AccountBean 61 implements EntityBean 62 { 63 64 private Connection c; 65 66 private Integer id; 67 private int balance; 68 private Integer customerId; 69 70 private EntityContext ctx; 71 72 79 public Integer getId() 80 { 81 return id; 82 } 83 84 90 public void setId(final Integer id) 91 { 92 this.id = id; 93 } 94 95 96 97 104 public int getBalance() 105 { 106 return balance; 107 } 108 109 115 public void setBalance(final int balance) 116 { 117 this.balance = balance; 118 } 119 120 121 122 129 public Integer getCustomerId() 130 { 131 return customerId; 132 } 133 134 140 public void setCustomerId(final Integer customerId) 141 { 142 this.customerId = customerId; 143 } 144 145 146 147 148 154 public void deposit(int amount) 155 { 156 setBalance(getBalance()+amount); 157 } 158 159 165 public void withdraw(int amount) 166 { 167 setBalance(getBalance()-amount); 168 } 169 170 171 181 public Integer ejbCreate(final Integer id, final int balance, final Integer customerId) 182 throws CreateException 183 { 184 setId(id); 185 setBalance(balance); 186 setCustomerId(customerId); 187 PreparedStatement ps = null; 188 try 189 { 190 191 ps = getConnection().prepareStatement("INSERT INTO CCBMPACCOUNT (ID, BALANCE, CUSTOMERID) VALUES (?, ?, ?)"); 192 ps.setInt(1, id.intValue()); 193 ps.setInt(2, balance); 194 ps.setObject(3, customerId); 195 } 196 catch (Exception e) 197 { 198 Logger.getLogger(getClass().getName()).info("Exception in ejbCreate", e); 199 throw new CreateException ("Can't insert: " + e); 200 } finally 202 { 203 try 204 { 205 if (ps != null) {ps.close();} 206 } 207 catch (Exception e) 208 { 209 Logger.getLogger(getClass().getName()).info("Exception closing ps: " + e); 210 } 212 } return id; 214 } 215 216 public void ejbPostCreate(final Integer id, final int balance, final Integer customerId) 217 { 218 } 219 220 228 public Integer ejbFindByPrimaryKey(final Integer id) 229 throws FinderException 230 { 231 PreparedStatement ps = null; 232 try 233 { 234 ps = getConnection().prepareStatement("SELECT ID FROM CCBMPACCOUNT WHERE ID = ?"); 235 ps.setInt(1, id.intValue()); 236 ResultSet rs = ps.executeQuery(); 237 if (!rs.next()) 238 { 239 throw new ObjectNotFoundException ("No such account: " + id); 240 } rs.close(); 242 243 } 244 catch (Exception e) 245 { 246 Logger.getLogger(getClass().getName()).info("Exception in findByPK", e); 247 throw new EJBException ("Problem in findByPrimaryKey: " + e); 248 } finally 250 { 251 try 252 { 253 if (ps != null) {ps.close();} 254 } 255 catch (Exception e) 256 { 257 Logger.getLogger(getClass().getName()).info("Exception closing ps: " + e); 258 } } return id; 261 } 262 263 270 public Collection ejbFindByCustomerId(final Integer customerId) 271 { 272 PreparedStatement ps = null; 273 try 274 { 275 ps = getConnection().prepareStatement("SELECT ID FROM CCBMPACCOUNT WHERE CUSTOMERID = ?"); 276 ps.setInt(1, customerId.intValue()); 277 ResultSet rs = ps.executeQuery(); 278 Collection result = new ArrayList (); 279 while (rs.next()) 280 { 281 result.add(new Integer (rs.getInt(1))); 282 } rs.close(); 284 return result; 285 } 286 catch (Exception e) 287 { 288 Logger.getLogger(getClass().getName()).info("Exception in findbyCustomerID", e); 289 throw new EJBException (e); 290 } finally 292 { 293 try 294 { 295 if (ps != null) {ps.close();} 296 } 297 catch (Exception e) 298 { 299 Logger.getLogger(getClass().getName()).info("Exception closing ps: " + e); 300 } } } 303 304 public void ejbActivate() 305 { 306 } 307 308 public void ejbPassivate() 309 { 310 if (c != null) 311 { 312 try 313 { 314 c.close(); 315 } 316 catch (Exception e) 317 { 318 Logger.getLogger(getClass().getName()).info("Exception closing c: " + e); 319 } c = null; 321 } } 323 324 public void ejbLoad() 325 { 326 id = (Integer ) ctx.getPrimaryKey(); 327 if (id == null) 328 { 329 Logger.getLogger(getClass().getName()).info("null id!"); 330 } 332 PreparedStatement ps = null; 333 try 334 { 335 336 ps = getConnection().prepareStatement("SELECT BALANCE, CUSTOMERID FROM CCBMPACCOUNT WHERE ID = ?"); 337 if (ps == null) 338 { 339 Logger.getLogger(getClass().getName()).info("WFT? null ps!"); 340 } 342 ps.setInt(1, id.intValue()); 343 ResultSet rs = ps.executeQuery(); 344 if (rs.next() == false) 345 throw new NoSuchEntityException ("Account does not exist " + id.toString()); 346 this.balance = rs.getInt(1); 347 this.customerId = (Integer )rs.getObject(2); 348 } 349 catch (Exception e) 350 { 351 Logger.getLogger(getClass().getName()).info("Exception in ejbLoad", e); 352 throw new EJBException (e); 353 } finally 355 { 356 try 357 { 358 if (ps != null) {ps.close();} 359 } 360 catch (Exception e) 361 { 362 Logger.getLogger(getClass().getName()).info("Exception closing ps: " + e); 363 } } } 366 367 public void ejbStore() 368 { 369 PreparedStatement ps = null; 370 try 371 { 372 ps = getConnection().prepareStatement("UPDATE CCBMPACCOUNT SET BALANCE = ?, CUSTOMERID = ? WHERE ID = ?"); 373 ps.setInt(1, balance); 374 ps.setObject(2, customerId); 375 ps.setInt(3, id.intValue()); 376 ps.execute(); 377 } 378 catch (Exception e) 379 { 380 Logger.getLogger(getClass().getName()).info("Exception in ejbStore", e); 381 throw new EJBException (e); 382 } finally 384 { 385 try 386 { 387 if (ps != null) {ps.close();} 388 } 389 catch (Exception e) 390 { 391 Logger.getLogger(getClass().getName()).info("Exception closing ps: " + e); 392 } } 395 } 396 397 public void ejbRemove() 398 { 399 PreparedStatement ps = null; 400 try 401 { 402 ps = getConnection().prepareStatement("DELETE FROM CCBMPACCOUNT WHERE ID = ?"); 403 ps.setInt(1, id.intValue()); 404 ps.execute(); 405 } 406 catch (Exception e) 407 { 408 Logger.getLogger(getClass().getName()).info("Exception in ejbRemove", e); 409 throw new EJBException (e); 410 } finally 412 { 413 try 414 { 415 if (ps != null) {ps.close();} 416 } 417 catch (Exception e) 418 { 419 Logger.getLogger(getClass().getName()).info("Exception closing ps: " + e); 420 } } } 423 424 public void setEntityContext(EntityContext ctx) 425 { 426 this.ctx = ctx; 427 } 428 429 public void unsetEntityContext() 430 { 431 ctx = null; 432 } 433 434 private Connection getConnection() throws Exception 435 { 436 if (c == null) 437 { 438 DataSource ds = (DataSource )new InitialContext ().lookup("java:/DefaultDS"); 439 c = ds.getConnection(); 440 441 } return c; 443 } 444 } 445 446 | Popular Tags |