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

📄 ordersbean.java~24~

📁 一个java
💻 JAVA~24~
字号:
package bookstore.ejb;import bookstore.util.*;import java.sql.*;import javax.sql.*;import java.util.*;import javax.ejb.*;import javax.naming.*;public class OrdersBean implements EntityBean{  java.lang.String orderID;  java.lang.String customerID;  java.lang.String bookID;  int num;  java.sql.Date orderDate;  java.sql.Date deliverDate;  private Connection con=null;  EntityContext entityContext;  /**********************business methods**************************************/  public void setOrderID(java.lang.String orderID) {    this.orderID = orderID;  }  public void setCustomerID(java.lang.String customerID) {    this.customerID = customerID;  }  public void setBookID(java.lang.String bookID) {    this.bookID = bookID;  }  public void setNum(int num) {    this.num = num;  }  public void setOrderDate(java.sql.Date orderDate) {    this.orderDate = orderDate;  }  public void setDeliverDate(java.sql.Date deliverDate) {    this.deliverDate = deliverDate;  }  public java.lang.String getOrderID() {    return orderID;  }  public java.lang.String getCustomerID() {    return customerID;  }  public java.lang.String getBookID() {    return bookID;  }  public int getNum() {    return num;  }  public java.sql.Date getOrderDate() {    return orderDate;  }  public java.sql.Date getDeliverDate() {    return deliverDate;  }  public OrderDetails getDetails()   {      System.out.println("CustomerBean getDetails");      return new OrderDetails (orderID,customerID,bookID,             num, orderDate,deliverDate);   }//getDetails  /**********************************ejb methods*************************/  public java.lang.String ejbCreate(OrderDetails orderValue)      throws CreateException  {    System.out.println("orderBean ejbCreate");        try        {          makeConnection();          this.orderID=DBHelper.getNextOrderID(con);        }catch(Exception ex)        {          throw new EJBException("ejbCreate: "+ex.getMessage());        }        this.customerID=orderValue.getCustomerID();        this.bookID=orderValue.getBookID();        this.num=orderValue.getNum();        this.orderDate=orderValue.getOrderDate();        this.deliverDate=orderValue.getDeliverDate();        try {            insertRow();        } catch (Exception ex) {             throw new EJBException("ejbCreate: " +                 ex.getMessage());        }        return orderID;  }  public void ejbPostCreate(OrderDetails orderValue) throws CreateException  {    System.out.println("orderBean ejbPostCreate");  }  public java.util.Collection ejbFindByCondition(String condition) throws FinderException {    System.out.println("orderBean ejbFindByCondition");    Collection result;    try {           result = selectByCondition(condition);       } catch (Exception ex) {             throw new EJBException("ejbFindByCondition " +                 ex.getMessage());       }       return result;  }  public java.util.Collection ejbFindOrderAble(String SQLStatement) throws FinderException {    System.out.println("orderBean ejbFindOrderAble");    Collection result;    try {           result = selectOrderAble(SQLStatement);       } catch (Exception ex) {             throw new EJBException("ejbFindByCondition " +                 ex.getMessage());       }       return result;  }  public void ejbRemove() throws RemoveException {    System.out.println("orderBean ejbRemove");    try {            deleteRow(orderID);         } catch (Exception ex) {              throw new EJBException("ejbRemove: " +                  ex.getMessage());         }  }//ejbRemove  public java.lang.String ejbFindByPrimaryKey(java.lang.String primaryKey) throws FinderException {         System.out.println("orderBean 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("orderBean ejbLoad");          try {              loadOrder();           } catch (Exception ex) {                throw new EJBException("ejbLoad: " +                    ex.getMessage());           }      }//ejbLoad      public void ejbStore()      {            System.out.println("orderBean ejbStore");            try {                storeOrder();             } catch (Exception ex) {                  throw new EJBException("ejbStore: " +                      ex.getMessage());             }        }//ejbStore  public void ejbActivate()  {        System.out.println("orderBean ejbActivate");        orderID = (String)entityContext.getPrimaryKey();    }    public void ejbPassivate()    {        System.out.println("orderBean ejbPassivate");        orderID= null;    }  public void unsetEntityContext()  {    System.out.println("orderBean ussetEntiyContext");    this.entityContext = null;  }  public void setEntityContext(EntityContext entityContext) {    System.out.println("orderBean setEntiyContext");    this.entityContext = entityContext;  }  /******************************util methods*************************/  private void makeConnection() {        System.out.println("orderBean 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("orderBean makeConnection out");    } // makeConnection    private void releaseConnection() {        System.out.println("orderBean releaseConnection in");        try {            con.close();        } catch (SQLException ex) {             throw new EJBException("releaseConnection: " + ex.getMessage());        }        System.out.println("orderBean releaseConnection out");    } // releaseConnection    private void insertRow () throws SQLException {        System.out.println("orderBean insertRow in");        makeConnection();        String insertStatement =            "insert into orders values ( ? , ? , ? , ? , ? , ?)";        PreparedStatement prepStmt =            con.prepareStatement(insertStatement);        prepStmt.setString(1, orderID);        prepStmt.setString(2,customerID);        prepStmt.setString(3, bookID);        prepStmt.setInt(4, num);        prepStmt.setDate(5,orderDate);        prepStmt.setDate(6,deliverDate);        prepStmt.executeUpdate();        prepStmt.close();        releaseConnection();        System.out.println("orderBean insertRow out");    }//insertRow    private Collection selectByCondition(String condition) throws SQLException {       System.out.println("orderBean selectByCondition in");       makeConnection();       String selectStatement =               "select orderID " +               "from orders 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("orderBean selectByCondition out");       return a;   }//selectByCondition   private Collection selectOrderAble(String condition) throws SQLException {       System.out.println("orderBean selectByCondition in");       makeConnection();       String selectStatement =               "select orderID " +               "from orders 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("orderBean selectByCondition out");       return a;   }//selectOrderAble   private boolean selectByPrimaryKey(String primaryKey) throws SQLException {        //本函数主要目的还在于确保order_id在数据库表中是存在的        System.out.println("orderBean selectByPrimaryKey in");        makeConnection();        //a question here        String selectStatement =                "select orderID " +                "from orders where orderID = ? ";        PreparedStatement prepStmt =                con.prepareStatement(selectStatement);        prepStmt.setString(1, primaryKey);        ResultSet rs = prepStmt.executeQuery();        boolean result = rs.next();        prepStmt.close();        releaseConnection();        System.out.println("orderBean selectByPrimaryKey out");        return result;    }//selectByPrimaryKey    private void loadOrder() throws SQLException {        System.out.println("orderBean loadorder in");        makeConnection();        String selectStatement =                "select customerID,bookID, num,orderDate,deliverDate " +                "from orders where orderID = ? ";        PreparedStatement prepStmt =                con.prepareStatement(selectStatement);        prepStmt.setString(1, orderID);        ResultSet rs = prepStmt.executeQuery();        if (rs.next())        {            customerID = rs.getString(1);            bookID = rs.getString(2);            num = rs.getInt(3);            orderDate=rs.getDate(4);            deliverDate=rs.getDate(5);            prepStmt.close();            releaseConnection();        }        else {            prepStmt.close();            releaseConnection();            throw new NoSuchEntityException("loadorder:Row for id " +                orderID + " not found in database.");        }        System.out.println("orderBean loadorder out");    }//loadorder    private void storeOrder() throws SQLException {        System.out.println("orderBean storeorder in");        makeConnection();        String updateStatement =                "update orders " +                "set customerID = ? , bookID = ? , num = ? , orderDate = ? ,deliverDate = ?"+                "where orderID = ? ";        PreparedStatement prepStmt =                con.prepareStatement(updateStatement);        prepStmt.setString(1,customerID);        prepStmt.setString(2, bookID);        prepStmt.setInt(3, num);        prepStmt.setDate(4,orderDate);        prepStmt.setDate(5,deliverDate);        prepStmt.setString(6, orderID);        int rowCount = prepStmt.executeUpdate();        prepStmt.close();        releaseConnection();        if (rowCount == 0) {            throw new EJBException("storeorder:Storing row for id " + orderID + " failed.");        }        System.out.println("orderBean storeorder out");    }//storeorder    private void deleteRow(String id) throws SQLException {        System.out.println("orderBean deleteRow in");        makeConnection();        String deleteStatement =                "delete from orders where orderID = ? ";        PreparedStatement prepStmt =                con.prepareStatement(deleteStatement);        prepStmt.setString(1, id);        prepStmt.executeUpdate();        prepStmt.close();        releaseConnection();        System.out.println("orderBean deleteRow out");    }//deleteRow}

⌨️ 快捷键说明

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