⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basketbean.java~10~

📁 一个java
💻 JAVA~10~
字号:
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.Timestamp 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.Timestamp 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.Timestamp 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("CustomerBean 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("CustomerBean makeConnection out");    } // makeConnection    private void releaseConnection() {        System.out.println("CustomerBean releaseConnection in");        try {            con.close();        } catch (SQLException ex) {             throw new EJBException("releaseConnection: " + ex.getMessage());        }        System.out.println("CustomerBean releaseConnection out");    } // releaseConnection    private void insertRow () throws SQLException {        System.out.println("CustomerBean insertRow in");        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();        System.out.println("CustomerBean insertRow out");    }//insertRow    private Collection selectByCondition(String condition) throws SQLException {       System.out.println("CustomerBean selectByCondition in");       makeConnection();       String selectStatement =               "select CustomerID " +               "from Customer 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("CustomerBean selectByCondition out");       return a;   }//selectByCondition   private boolean selectByPrimaryKey(String primaryKey) throws SQLException {        //本函数主要目的还在于确保customer_id在数据库表中是存在的        System.out.println("CustomerBean selectByPrimaryKey in");        makeConnection();        //a question here        String selectStatement =                "select CustomerID " +                "from Customer where CustomerID = ? ";        PreparedStatement prepStmt =                con.prepareStatement(selectStatement);        prepStmt.setString(1, primaryKey);        ResultSet rs = prepStmt.executeQuery();        boolean result = rs.next();        prepStmt.close();        releaseConnection();        System.out.println("CustomerBean selectByPrimaryKey out");        return result;    }//selectByPrimaryKey    private void loadCustomer() throws SQLException {        System.out.println("CustomerBean loadCustomer in");        makeConnection();        String selectStatement =                "select Name, Account, Password,Sexual,BirthDay, City, Profession, IDCardNum,Address,Zip, Phone, EMail ,Rank " +                "from Customer where CustomerID = ? ";        PreparedStatement prepStmt =                con.prepareStatement(selectStatement);        prepStmt.setString(1, customerID);        ResultSet rs = prepStmt.executeQuery();        if (rs.next())        {            name = rs.getString(1);            account = rs.getString(2);            password = rs.getString(3);            sexual = rs.getInt(4);            birthDay=rs.getDate(5);            city = rs.getString(6);            profession = rs.getString(7);            iDCardNum=rs.getString(8);            address=rs.getString(9);            zip = rs.getString(10);            phone = rs.getString(11);            eMail = rs.getString(12);            rank=rs.getInt(13);            prepStmt.close();            releaseConnection();        }        else {            prepStmt.close();            releaseConnection();            throw new NoSuchEntityException("loadCustomer:Row for id " +                customerID + " not found in database.");        }        System.out.println("CustomerBean loadCustomer out");    }//loadCustomer    private void storeCustomer() throws SQLException {        System.out.println("CustomerBean storeCustomer in");        makeConnection();        String updateStatement =                "update Customer " +                "set Name = ? , Account = ? , Password = ? , Sexual = ? ,"+                "BirthDay=?,City = ? ,Profession= ?,IDCardNum= ? ,"+                "Address = ? ,Zip= ? ,Phone = ? ,EMail = ? ,Rank = ?" +                "where CustomerID = ? ";        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, customerID);        int rowCount = prepStmt.executeUpdate();        prepStmt.close();        releaseConnection();        if (rowCount == 0) {            throw new EJBException("storeCustomer:Storing row for id " + customerID + " failed.");        }        System.out.println("CustomerBean storeCustomer out");    }//storeCustomer    private void deleteRow(String id) throws SQLException {        System.out.println("CustomerBean deleteRow in");        makeConnection();        String deleteStatement =                "delete from customer where CustomerID = ? ";        PreparedStatement prepStmt =                con.prepareStatement(deleteStatement);        prepStmt.setString(1, id);        prepStmt.executeUpdate();        prepStmt.close();        releaseConnection();        System.out.println("CustomerBean deleteRow out");    }//deleteRow}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -