Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 44 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
44
Dung lượng
238,82 KB
Nội dung
Bean-Managed Persistence 241 public int ejbCreate(String username, String password) throws CreateException, RemoteException { } public void ejbPostCreate(String course, String instructor) { } public int ejbLoad() throws RemoteException { } public int ejbStore()throws RemoteException { } public int ejbRemove()throws RemoteException { } public String findByPrimaryKey() throws RemoteException { } } ejbCreate() We can use the ejbCreate() method when we want to insert a new row into the database To accomplish this, we need to access the database, check to see if the row already exists, and either insert a row into the table or throw an exception The code might look like this: public int ejbCreate(String username, String password) throws CreateException, RemoveException { InitialContext ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup( "java:comp/env/jdbc/AccountsDB" ); Connection conn = ds.getConnection(); PreparedStatement stmt = conn.prepareStatement( "SELECT * FROM acc_acc WHERE username=? and password=?" ); stmt.setString( 1, username ); stmt.setString(2, password); ResultSet rs = stmt.executeQuery(); duplicateKey = rs.next(); rs.close(); stmt.close(); if ( ! duplicateKey ) { stmt = conn.prepareStatement( "INSERT INTO acc_acc (null, ?, ?, ? ,?)"); stmt.setString( 1, username ); stmt.setString( 2, password ); stmt.setTimestamp(null); stmt.setTimestamp(new Timestamp(new Date().getDate())); 242 EJBs with MySQL stmt.executeUpdate(); stmt.close(); } conn.close(); } catch (Exception ex) { throw new java.rmi.RemoteException( "ejbCreate Error", ex ); } if ( duplicateKey ) throw new javax.ejb.DuplicateKeyException(); return null; } As the preceding code shows, we are responsible for doing all of the database work in a bean-managed persistence mode ejbLoad() In the ejbLoad() method, the code has to load all of the fields for the row associated with a particular primary key For example: public void ejbLoad() throws java.rmi.RemoteException { boolean found = false; try { InitialContext ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup( "java:comp/env/jdbc/AccountsDB" ); Connection conn = ds.getConnection(); PreparedStatement stmt = conn.prepareStatement( "SELECT username FROM acc_acc WHERE acc_id=?"); stmt.setString( 1, acc_id ); ResultSet rs = stmt.executeQuery(); if ( rs.next()) { found = true; username = rs.getString(2); password = rs.getString(3); ts = rs.getTimestamp(4); act_ts = re.getTimestamp(5); } rs.close(); stmt.close(); conn.close(); } catch (Exception ex) { throw new java.rmi.RemoteException( "ejbLoad Error", ex ); } if ( ! found ) throw new java.rmi.RemoteException( "Bean not found" ); } Bean-Managed Persistence 243 In this code, the database table is checked for a row with a specific acc_id value If a match is found, all of the bean’s attributes are set based on the returned row; otherwise, an exception is thrown ejbStore() The ejbStore() method is responsible for placing the bean’s information back into the database table This isn’t an INSERT into the table, but an UPDATE of a previously created row The code might look like this: public void ejbStore() throws java.rmi.RemoteException { boolean found = false; try { InitialContext ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup( "java:comp/env/jdbc/AccountsDB" ); Connection conn = ds.getConnection(); PreparedStatement stmt = conn.prepareStatement( "UPDATE acc_acc set username=?, password=?, ts=? act_ts =?WHERE acc_id=?"); stmt.setString( 1, username ); stmt.setString( 2, password ); stmt.setTimestamp( 3, ts ); stmt.setTimestamp 4, act_ts ); stmt.setInt( 5, acc_id ); if (stmt.executeUpdate()