📄 customercontrollerbean.java~45~
字号:
package bookstore;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; // customer creation and 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 { //customerId根据数据库表的情况来产生 customerID= "C1"; customerValue.setCustomerID("C1"); //调用实体bean的引导接口,可见引导接口也是可以由上层随意调用 customer = customerHome.create(customerValue); } catch (Exception ex) { throw new EJBException ("createCustomer:11 " + ex.getMessage()); } return customerID; }// createCustomer public CustomerDetails getCustomerByAccount(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()) { Customer customer = (Customer)i.next(); customerValue = customer.getDetails(); } return customerValue; }//getCustomerByAccount public void ejbCreate() throws CreateException { System.out.println("CustomerControllerBean ejbCreate"); try { //创建下一层的引导接口 InitialContext initial = new InitialContext(); Object objref = initial.lookup("Customer"); customerHome=(CustomerHome)PortableRemoteObject.narrow(objref, CustomerHome.class); } catch (Exception ex) { System.out.println("CustomerControllerBean catch"); throw new EJBException("ejbCreate: " + ex.getMessage()); } customer = null; customerID = null; System.out.println("CustomerControllerBean.ejbCreate out"); } 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 void makeConnection() { System.out.println("CustomerControllerBean 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("CustomerControllerBean releaseConnection"); try { con.close(); } catch (SQLException ex) { throw new EJBException("releaseConnection: " + ex.getMessage()); } } // releaseConnection*/}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -