📄 customerbean.java~2~
字号:
package bookstore;import java.sql.*;import javax.sql.*;import java.util.*;import javax.ejb.*;import javax.naming.*;public class CustomerBean implements EntityBean { java.lang.String customerID; java.lang.String name; java.lang.String account; java.lang.String password; int sexual; java.sql.Date birthDay; java.lang.String city; java.lang.String profession; java.lang.String iDCardNum; java.lang.String address; java.lang.String zip; java.lang.String phone; java.lang.String eMail; int rank; private Connection con; EntityContext entityContext; /*********************************business methods*************************/ public void setCustomerID(java.lang.String customerID) { this.customerID = customerID; } public void setName(java.lang.String name) { this.name = name; } public void setAccount(java.lang.String account) { this.account = account; } public void setPassword(java.lang.String password) { this.password = password; } public void setSexual(int sexual) { this.sexual = sexual; } public void setBirthDay(java.sql.Date birthDay) { this.birthDay = birthDay; } public void setCity(java.lang.String city) { this.city = city; } public void setProfession(java.lang.String profession) { this.profession = profession; } public void setIDCardNum(java.lang.String iDCardNum) { this.iDCardNum = iDCardNum; } public void setAddress(java.lang.String address) { this.address = address; } public void setZip(java.lang.String zip) { this.zip = zip; } public void setPhone(java.lang.String phone) { this.phone = phone; } public void setEMail(java.lang.String eMail) { this.eMail = eMail; } public void setRank(int rank) { this.rank = rank; } public java.lang.String getCustomerID() { return customerID; } public java.lang.String getName() { return name; } public java.lang.String getAccount() { return account; } public java.lang.String getPassword() { return password; } public int getSexual() { return sexual; } public java.sql.Date getBirthDay() { return birthDay; } public java.lang.String getCity() { return city; } public java.lang.String getProfession() { return profession; } public java.lang.String getIDCardNum() { return iDCardNum; } public java.lang.String getAddress() { return address; } public java.lang.String getZip() { return zip; } public java.lang.String getPhone() { return phone; } public java.lang.String getEMail() { return eMail; } public int getRank() { return rank; } /**********************************ejb methods*************************/ public java.lang.String ejbCreate(CustomerDetails customerValue) throws CreateException { System.out.println("CustomerBean ejbCreate"); if ((customerValue.getCustomerID()==null) || (customerValue.getCustomerID().trim().length() == 0)) { throw new CreateException("ejbCreate: customerId arg is null or empty"); } this.customerID=customerValue.getCustomerID(); 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 account) throws FinderException { System.out.println("CustomerBean ejbFindByCondition"); Collection result; try { result = selectByCondition(account); } catch (Exception ex) { throw new EJBException("ejbFindByCondition " + ex.getMessage()); } return result; } public void ejbRemove() throws RemoveException { System.out.println("CustomerBean ejbRemove"); } public java.lang.String ejbFindByPrimaryKey(java.lang.String customerID) throws FinderException { System.out.println("CustomerBean ejbFindByPrimaryKey"); return null; } public void ejbLoad() { System.out.println("CustomerBean ejbLoad"); } public void ejbStore() { System.out.println("CustomerBean ejbStore"); } public void ejbActivate() { System.out.println("CustomerBean ejbActivate"); } public void ejbPassivate() { System.out.println("CustomerBean ejbPassivate"); } 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("CustomerBean makeConnection"); 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()); } } // makeConnection private void releaseConnection() { System.out.println("CustomerBean releaseConnection"); try { con.close(); } catch (SQLException ex) { throw new EJBException("releaseConnection: " + ex.getMessage()); } } // releaseConnection private void insertRow () throws SQLException { System.out.println("CustomerBean insertRow"); makeConnection(); String insertStatement = "insert into Customer values ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? )"; PreparedStatement prepStmt = con.prepareStatement(insertStatement); prepStmt.setString(1, customerID); prepStmt.setString(2, name); prepStmt.setString(3, account); prepStmt.setString(4, password); prepStmt.setInt(5, sexual); prepStmt.setDate(6,birthDay); prepStmt.setString(7, city); prepStmt.setString(8, profession); prepStmt.setString(9,iDCardNum); prepStmt.setString(10,address); prepStmt.setString(11, zip); prepStmt.setString(12, phone); prepStmt.setString(13, eMail); prepStmt.setInt(14,rank); prepStmt.executeUpdate(); prepStmt.close(); releaseConnection(); }//insertRow private Collection selectByCondition(String condition) throws SQLException { System.out.println("CustomerBean selectByAccountId"); makeConnection(); String selectStatement = "select customerID " + "from Customer where"+condition; Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery(selectStatement); ArrayList a = new ArrayList(); while (rs.next()) { a.add(rs.getString(1)); System.out.println("CustomerBean.selectByCondition rs.getString(1):"+rs.getString(1)); } stmt.close(); releaseConnection(); return a; }//selectByCondition public CustomerDetails getDetails() { System.out.println("CustomerBean getDetails"); return new CustomerDetails (customerID, name, account, password, sexual, birthDay, city, profession, iDCardNum,address,zip,phone, eMail,rank); }//////////////////getDetails}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -