1 25 26 package eb; 27 28 import java.rmi.RemoteException; 29 import java.sql.Connection; 30 import java.sql.PreparedStatement; 31 import java.sql.ResultSet; 32 import java.sql.SQLException; 33 import java.util.Enumeration; 34 import java.util.Vector; 35 36 import javax.ejb.CreateException; 37 import javax.ejb.EJBException; 38 import javax.ejb.EntityBean; 39 import javax.ejb.EntityContext; 40 import javax.ejb.FinderException; 41 import javax.ejb.ObjectNotFoundException; 42 import javax.ejb.RemoveException; 43 import javax.naming.Context; 44 import javax.naming.InitialContext; 45 import javax.sql.DataSource; 46 47 57 58 public class AccountExplBean implements EntityBean { 59 60 private DataSource dataSource = null; 61 62 protected EntityContext entityContext; 64 65 public Integer accno; 67 68 public String customer; 69 70 public double balance; 71 72 73 74 84 public Integer ejbCreate(int val_accno, String val_customer, double val_balance) throws CreateException { 85 86 accno = new Integer(val_accno); 88 customer = val_customer; 89 balance = val_balance; 90 91 Connection conn = null; 92 PreparedStatement stmt = null; 93 try { 94 conn = getConnection(); 96 97 stmt = conn.prepareStatement("insert into accountsample_ (accno_, customer_, balance_) values (?, ?, ?)"); 99 stmt.setInt(1, accno.intValue()); 100 stmt.setString(2, customer); 101 stmt.setDouble(3, balance); 102 stmt.executeUpdate(); 103 104 } catch (SQLException e) { 105 throw new CreateException("Failed to create bean in database: " + e); 106 } finally { 107 try { 108 if (stmt != null) { 109 stmt.close(); 111 } 112 if (conn != null) { 113 conn.close(); 115 } 116 } catch (Exception ignore) { 117 } 118 } 119 return accno; 121 } 122 123 126 public void ejbPostCreate(int val_accno, String val_customer, double val_balance) { 127 } 129 130 134 135 143 public void ejbActivate() { 144 } 146 147 159 public void ejbLoad() { 160 Connection conn = null; 161 PreparedStatement stmt = null; 162 try { 163 conn = getConnection(); 165 166 stmt = conn.prepareStatement("select customer_,balance_ from accountsample_ where accno_=?"); 168 Integer pk = (Integer) entityContext.getPrimaryKey(); 169 stmt.setInt(1, pk.intValue()); 170 ResultSet rs = stmt.executeQuery(); 171 if (!rs.next()) { 172 throw new EJBException("Failed to load bean from database"); 173 } 174 175 accno = pk; 177 customer = rs.getString("customer_"); 178 balance = rs.getDouble("balance_"); 179 180 } catch (SQLException e) { 181 throw new EJBException("Failed to load bean from database " + e); 182 } finally { 183 try { 184 if (stmt != null) { 185 stmt.close(); 187 } 188 if (conn != null) { 189 conn.close(); 191 } 192 } catch (Exception ignore) { 193 } 194 } 195 } 196 197 206 public void ejbPassivate() { 207 } 209 210 227 public void ejbRemove() throws RemoveException { 228 Connection conn = null; 229 PreparedStatement stmt = null; 230 try { 231 conn = getConnection(); 233 234 stmt = conn.prepareStatement("delete from accountsample_ where accno_=?"); 236 Integer pk = (Integer) entityContext.getPrimaryKey(); 237 stmt.setInt(1, pk.intValue()); 238 stmt.executeUpdate(); 239 240 } catch (SQLException e) { 241 throw new RemoveException("Failed to delete bean from database" + e); 242 } finally { 243 try { 244 if (stmt != null) { 245 stmt.close(); 247 } 248 if (conn != null) { 249 conn.close(); 251 } 252 } catch (Exception ignore) { 253 } 254 } 255 } 256 257 269 public void ejbStore() { 270 Connection conn = null; 271 PreparedStatement stmt = null; 272 try { 273 conn = getConnection(); 275 276 stmt = conn.prepareStatement("update accountsample_ set customer_=?,balance_=? where accno_=?"); 278 stmt.setString(1, customer); 279 stmt.setDouble(2, balance); 280 Integer pk = (Integer) entityContext.getPrimaryKey(); 281 stmt.setInt(3, pk.intValue()); 282 stmt.executeUpdate(); 283 284 } catch (SQLException e) { 285 throw new EJBException("Failed to store bean to database " + e); 286 } finally { 287 try { 288 if (stmt != null) { 289 stmt.close(); 291 } 292 if (conn != null) { 293 conn.close(); 295 } 296 } catch (Exception ignore) { 297 } 298 } 299 } 300 301 315 public void setEntityContext(EntityContext ctx) { 316 317 entityContext = ctx; 319 320 } 321 322 336 public void unsetEntityContext() { 337 entityContext = null; 338 } 339 340 341 342 352 public Integer ejbFindByPrimaryKey(Integer pk) throws ObjectNotFoundException, FinderException { 353 354 Connection conn = null; 355 PreparedStatement stmt = null; 356 try { 357 conn = getConnection(); 359 360 stmt = conn.prepareStatement("select accno_ from accountsample_ where accno_=?"); 362 stmt.setInt(1, pk.intValue()); 363 ResultSet rs = stmt.executeQuery(); 364 if (!rs.next()) { 365 throw new javax.ejb.ObjectNotFoundException(); 366 } 367 368 } catch (SQLException e) { 369 throw new javax.ejb.FinderException("Failed to executeQuery " + e); 370 } finally { 371 try { 372 if (stmt != null) { 373 stmt.close(); 375 } 376 if (conn != null) { 377 conn.close(); 379 } 380 } catch (Exception ignore) { 381 } 382 } 383 384 return pk; 386 } 387 388 395 public Integer ejbFindByNumber(int accno) throws ObjectNotFoundException, FinderException { 396 397 Connection conn = null; 398 PreparedStatement stmt = null; 399 try { 400 conn = getConnection(); 402 stmt = conn.prepareStatement("select accno_ from accountsample_ where accno_=?"); 404 stmt.setInt(1, accno); 405 ResultSet rs = stmt.executeQuery(); 406 if (!rs.next()) { 407 throw new javax.ejb.ObjectNotFoundException(); 408 } 409 410 } catch (SQLException e) { 411 throw new javax.ejb.FinderException("Failed to executeQuery " + e); 412 } finally { 413 try { 414 if (stmt != null) { 415 stmt.close(); 417 } 418 if (conn != null) { 419 conn.close(); 421 } 422 } catch (Exception ignore) { 423 } 424 } 425 426 return new Integer(accno); 428 } 429 430 435 public Enumeration ejbFindAllAccounts() throws FinderException { 436 Connection conn = null; 437 PreparedStatement stmt = null; 438 Vector pkv = new Vector(); 439 try { 440 conn = getConnection(); 442 443 stmt = conn.prepareStatement("select accno_ from accountsample_"); 445 ResultSet rs = stmt.executeQuery(); 446 447 while (rs.next()) { 449 Integer pk = new Integer(rs.getInt("accno_")); 450 pkv.addElement((Object) pk); 451 } 452 453 } catch (SQLException e) { 454 throw new javax.ejb.FinderException("Failed to executeQuery " + e); 455 } finally { 456 try { 457 if (stmt != null) { 458 stmt.close(); 460 } 461 if (conn != null) { 462 conn.close(); 464 } 465 } catch (Exception ignore) { 466 } 467 } 468 469 return pkv.elements(); 471 } 472 473 479 private Connection getConnection() throws EJBException, SQLException { 480 if (dataSource == null) { 481 Context initialContext = null; 483 try { 484 initialContext = new InitialContext(); 485 dataSource = (DataSource) initialContext.lookup("java:comp/env/jdbc/AccountExplDs"); 486 } catch (Exception e) { 487 System.out.println("Cannot lookup dataSource" + e); 488 throw new javax.ejb.EJBException("Cannot lookup dataSource "); 489 } 490 } 491 return dataSource.getConnection(); 492 } 493 494 498 499 503 public double getBalance() { 504 505 return balance; 506 } 507 508 512 public void setBalance(double d) { 513 514 balance = balance + d; 515 } 516 517 521 public String getCustomer() { 522 523 return customer; 524 } 525 526 530 public void setCustomer(String c) { 531 532 customer = c; 533 } 534 535 538 public int getNumber() { 539 return accno.intValue(); 540 } 541 } | Popular Tags |