📄 basketbean.java~13~
字号:
package bookstore.ejb;import bookstore.util.*;import java.sql.*;import javax.sql.*;import java.util.*;import javax.ejb.*;import javax.naming.*;public class BasketBean implements EntityBean { java.lang.String basketID; java.lang.String customerID; java.lang.String bookID; java.sql.Date addDate; private Connection con=null; EntityContext entityContext; /*********************business methods***************************/ public void setBasketID(java.lang.String basketID) { this.basketID = basketID; } public void setCustomerID(java.lang.String customerID) { this.customerID = customerID; } public void setBookID(java.lang.String bookID) { this.bookID = bookID; } public void setAddDate(java.sql.Date addDate) { this.addDate = addDate; } public java.lang.String getBasketID() { return basketID; } public java.lang.String getCustomerID() { return customerID; } public java.lang.String getBookID() { return bookID; } public java.sql.Date getAddDate() { return addDate; } /********************************ejb methods**************************/ public java.lang.String ejbCreate(BasketDetails customerValue) throws CreateException { System.out.println("CustomerBean ejbCreate"); try { makeConnection(); this.customerID=DBHelper.getNextCustomerID(con); }catch(Exception ex) { throw new EJBException("ejbCreate: "+ex.getMessage()); } this.name=customerValue.getName(); this.account=customerValue.getAccount(); this.password=customerValue.getPassword(); this.sexual=customerValue.getSexual(); this.birthDay=customerValue.getBirthDay(); this.city=customerValue.getCity(); this.profession=customerValue.getProfession(); this.iDCardNum=customerValue.getIDCardNum(); this.address=customerValue.getAddress(); this.zip=customerValue.getZip(); this.phone=customerValue.getPhone(); this.eMail=customerValue.getEmail(); this.rank=customerValue.getRank(); try { insertRow(); } catch (Exception ex) { throw new EJBException("ejbCreate: " + ex.getMessage()); } return customerID; } public void ejbPostCreate(CustomerDetails customerValue) throws CreateException { System.out.println("CustomerBean ejbPostCreate"); } public java.util.Collection ejbFindByCondition(String condition) throws FinderException { System.out.println("CustomerBean ejbFindByCondition"); Collection result; try { result = selectByCondition(condition); } catch (Exception ex) { throw new EJBException("ejbFindByCondition " + ex.getMessage()); } return result; } public void ejbRemove() throws RemoveException { System.out.println("CustomerBean ejbRemove"); try { deleteRow(customerID); } catch (Exception ex) { throw new EJBException("ejbRemove: " + ex.getMessage()); } }//ejbRemove public java.lang.String ejbFindByPrimaryKey(java.lang.String primaryKey) throws FinderException { System.out.println("CustomerBean ejbFindByPrimaryKey"); boolean result; try { result = selectByPrimaryKey(primaryKey); } catch (Exception ex) { throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage()); } if (result) { return primaryKey; } else { throw new ObjectNotFoundException ("Row for id " + primaryKey + " not found."); } } public void ejbLoad() { System.out.println("CustomerBean ejbLoad"); try { loadCustomer(); } catch (Exception ex) { throw new EJBException("ejbLoad: " + ex.getMessage()); } }//ejbLoad public void ejbStore() { System.out.println("CustomerBean ejbStore"); try { storeCustomer(); } catch (Exception ex) { throw new EJBException("ejbStore: " + ex.getMessage()); } }//ejbStore public void ejbActivate() { System.out.println("CustomerBean ejbActivate"); // customerID = (String)entityContext.getPrimaryKey(); } public void ejbPassivate() { System.out.println("CustomerBean ejbPassivate"); customerID= null; } public void unsetEntityContext() { System.out.println("CustomerBean ussetEntiyContext"); this.entityContext = null; } public void setEntityContext(EntityContext entityContext) { System.out.println("CustomerBean setEntiyContext"); this.entityContext = entityContext; } /****************************util methods*******************************/ private void makeConnection() { System.out.println("BasketBean makeConnection in"); try { InitialContext ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup("DBSource"); con = ds.getConnection(); } catch (Exception ex) { throw new EJBException("Unable to connect to database. " + ex.getMessage()); } System.out.println("BasketBean makeConnection out"); } // makeConnection private void releaseConnection() { System.out.println("BasketBean releaseConnection in"); try { con.close(); } catch (SQLException ex) { throw new EJBException("releaseConnection: " + ex.getMessage()); } System.out.println("BasketBean releaseConnection out"); } // releaseConnection private void insertRow () throws SQLException { System.out.println("basketBean insertRow in"); makeConnection(); String insertStatement = "insert into basket values ( ? , ? , ? , ? )"; PreparedStatement prepStmt = con.prepareStatement(insertStatement); prepStmt.setString(1, basketID); prepStmt.setString(2, customerID); prepStmt.setString(3, bookID); prepStmt.setDate(4,addDate); prepStmt.executeUpdate(); prepStmt.close(); releaseConnection(); System.out.println("BasketBean insertRow out"); }//insertRow private Collection selectByCondition(String condition) throws SQLException { System.out.println("BasketBean selectByCondition in"); makeConnection(); String selectStatement = "select basketID " + "from basket where "+condition; System.out.println(selectStatement); Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery(selectStatement); ArrayList a = new ArrayList(); while (rs.next()) { a.add(rs.getString(1)); } stmt.close(); releaseConnection(); System.out.println("basketBean selectByCondition out"); return a; }//selectByCondition private boolean selectByPrimaryKey(String primaryKey) throws SQLException { //本函数主要目的还在于确保basket_id在数据库表中是存在的 System.out.println("basketBean selectByPrimaryKey in"); makeConnection(); //a question here String selectStatement = "select basketID " + "from basket where basketID = ? "; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, primaryKey); ResultSet rs = prepStmt.executeQuery(); boolean result = rs.next(); prepStmt.close(); releaseConnection(); System.out.println("basketBean selectByPrimaryKey out"); return result; }//selectByPrimaryKey private void loadbasket() throws SQLException { System.out.println("basketBean loadbasket in"); makeConnection(); String selectStatement = "select CustomerID, BookID,addDate " + "from basket where basketID = ? "; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, basketID); ResultSet rs = prepStmt.executeQuery(); if (rs.next()) { customerID = rs.getString(1); bookID = rs.getString(2); addDate=rs.getDate(3); prepStmt.close(); releaseConnection(); } else { prepStmt.close(); releaseConnection(); throw new NoSuchEntityException("loadbasket:Row for id " + basketID + " not found in database."); } System.out.println("basketBean loadbasket out"); }//loadbasket private void storebasket() throws SQLException { System.out.println("basketBean storebasket in"); makeConnection(); String updateStatement = "update basket " + "set Name = ? , Account = ? , Password = ? , Sexual = ? ,"+ "BirthDay=?,City = ? ,Profession= ?,IDCardNum= ? ,"+ "Address = ? ,Zip= ? ,Phone = ? ,EMail = ? ,Rank = ?" + "where basketID = ? "; PreparedStatement prepStmt = con.prepareStatement(updateStatement); prepStmt.setString(1, name); prepStmt.setString(2, account); prepStmt.setString(3, password); prepStmt.setInt(4, sexual); prepStmt.setDate(5,birthDay); prepStmt.setString(6, city); prepStmt.setString(7,profession); prepStmt.setString(8,iDCardNum); prepStmt.setString(9,address); prepStmt.setString(10, zip); prepStmt.setString(11, phone); prepStmt.setString(12, eMail); prepStmt.setInt(13,rank); prepStmt.setString(14, basketID); int rowCount = prepStmt.executeUpdate(); prepStmt.close(); releaseConnection(); if (rowCount == 0) { throw new EJBException("storebasket:Storing row for id " + basketID + " failed."); } System.out.println("basketBean storebasket out"); }//storebasket private void deleteRow(String id) throws SQLException { System.out.println("basketBean deleteRow in"); makeConnection(); String deleteStatement = "delete from basket where basketID = ? "; PreparedStatement prepStmt = con.prepareStatement(deleteStatement); prepStmt.setString(1, id); prepStmt.executeUpdate(); prepStmt.close(); releaseConnection(); System.out.println("basketBean deleteRow out"); }//deleteRow}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -