📄 customercontrollerbean.java
字号:
package bookstore.ejb;import bookstore.util.*;import java.sql.*;import javax.sql.*;import java.util.*;import javax.ejb.*;//import javax.naming.*;//import java.rmi.RemoteException;//import javax.rmi.PortableRemoteObject;//import javax.naming.InitialContext;public class CustomerControllerBean implements SessionBean { SessionContext sessionContext; private String customerID; private CustomerHome customerHome; private Customer customer; private Connection con; /*************************** removal methods*****************************/ public String createCustomer(CustomerDetails customerValue) throws Exception { // makes a new customer and enters it into db, // returns customerId System.out.println("CustomerControllerBean createCustomer"); try { CustomerDetails cV=getCustomerValueByAccount(customerValue.getAccount()); if (cV !=null) { throw new Exception(); } customer = customerHome.create(customerValue); } catch (Exception ex) { throw new EJBException ("createCustomer: " + ex.getMessage()); } return customerID; }// createCustomer public CustomerDetails getCustomerValueByAccount(String account) { // returns an ArrayList of CustomerDetails // that correspond to the account specified System.out.println("CustomerControllerBean getCustomersByAccount"); Collection customers; CustomerDetails customerValue=null; try { String condition="Account="+"'"+account+"'"; customers = customerHome.findByCondition(condition); } catch (Exception ex) { return null; } Iterator i = customers.iterator(); if(i.hasNext()) { System.out.println("CustomerControllerBean: i.hasNext"); Customer customer = (Customer)i.next(); customerValue = customer.getDetails(); } return customerValue; } public void removeCustomer(String customerId) throws NoSuchEntityException { //抛出RunTimeException的子类,有利于事务管理 // removes customer from db System.out.println("CustomerControllerBean removeCustomer"); if (isCustomerExist(customerId) == false) throw new NoSuchEntityException(customerId); try { customer.remove();//customer已在isCustomerExist中获得 }catch (Exception ex) { throw new EJBException ("removeCustomer: " + ex.getMessage()); } } // removeCustomer public void updateCustomer(CustomerDetails customerValue) throws NoSuchEntityException { System.out.println("CustomerControllerBean updateCustomer"); if (isCustomerExist(customerValue.getCustomerID()) == false) throw new NoSuchEntityException(customerValue.getCustomerID()); customer.setName(customerValue.getName()); customer.setAccount(customerValue.getAccount()); customer.setPassword(customerValue.getPassword()); customer.setSexual(customerValue.getSexual()); customer.setBirthDay(customerValue.getBirthDay()); customer.setCity(customerValue.getCity()); customer.setProfession(customerValue.getProfession()); customer.setIDCardNum(customerValue.getIDCardNum()); customer.setAddress(customerValue.getAddress()); customer.setZip(customerValue.getZip()); customer.setPhone(customerValue.getPhone()); customer.setEMail(customerValue.getEmail()); customer.setRank(customerValue.getRank()); }//updateCustomer public CustomerDetails getCustomerValue(String customerId) { System.out.println("customerControllerBean getcustomerValue"); try { customer = customerHome.findByPrimaryKey(customerId); this.customerID = customerId; } catch (Exception ex) { return null; } return customer.getDetails(); }/******************************ejb methods*********************************/ public void ejbCreate() throws CreateException { System.out.println("CustomerControllerBean ejbCreate"); try { //创建下一层的引导接口 customerHome=EJBGetter.getCustomerHome(); } catch (Exception ex) { System.out.println("CustomerControllerBean catch"); throw new EJBException("ejbCreate: " + ex.getMessage()); } customer = null; customerID = null; }//ejbCreate public void ejbRemove() { /**@todo Complete this method*/ } public void ejbActivate() { /**@todo Complete this method*/ } public void ejbPassivate() { /**@todo Complete this method*/ } public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; } /**********************************util methods****************************/ private boolean isCustomerExist(String customerId) { // If a business method has been invoked with // a different customerId, then update the // customerId and customer variables. // Return null if the customer is not found. System.out.println("CustomerControllerBean isCustomerExist"); if (customerId.equals(this.customerID) == false) { try { customer = customerHome.findByPrimaryKey(customerId); this.customerID = customerId; } catch (Exception ex) { return false; } } // if return true; }// isCustomerExist}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -