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

📄 supplierbean.java

📁 一个java
💻 JAVA
字号:
package bookstore.ejb;import bookstore.util.*;import java.sql.*;import javax.sql.*;import java.util.*;import javax.ejb.*;import javax.naming.*;public class SupplierBean implements EntityBean{  java.lang.String supplierID;  java.lang.String company;  java.lang.String person;  java.lang.String phone;  java.lang.String address;  java.lang.String zip;  private Connection con=null;  EntityContext entityContext;  /********************************business methods****************************/  public void setSupplierID(java.lang.String supplierID) {    this.supplierID = supplierID;  }  public void setCompany(java.lang.String company) {    this.company = company;  }  public void setPerson(java.lang.String person) {    this.person = person;  }  public void setPhone(java.lang.String phone) {    this.phone = phone;  }  public void setAddress(java.lang.String address) {    this.address = address;  }  public void setZip(java.lang.String zip) {    this.zip = zip;  }  public java.lang.String getSupplierID() {    return supplierID;  }  public java.lang.String getCompany() {    return company;  }  public java.lang.String getPerson() {    return person;  }  public java.lang.String getPhone() {    return phone;  }  public java.lang.String getAddress() {    return address;  }  public java.lang.String getZip() {    return zip;  }  public SupplierDetails getDetails()   {      System.out.println("commentBean getDetails");      return new SupplierDetails (supplierID,company ,person,phone,address,zip);   }//getDetails  /********************************ejb methods**************************/  public java.lang.String ejbCreate(SupplierDetails supplierValue) throws CreateException {    System.out.println("supplierBean ejbCreate");        try        {          makeConnection();          this.supplierID=DBHelper.getNextSupplierID(con);        }catch(Exception ex)        {          throw new EJBException("ejbCreate: "+ex.getMessage());        }        this.company=supplierValue.getCompany();        this.person=supplierValue.getPerson();        this.phone=supplierValue.getPhone();        this.address=supplierValue.getAddress();        this.zip=supplierValue.getZip();        try {            insertRow();        } catch (Exception ex) {             throw new EJBException("ejbCreate: " +                 ex.getMessage());        }        return supplierID;  }  public void ejbPostCreate(SupplierDetails supplierValue) throws CreateException {    System.out.println("supplierBean ejbPostCreate");  }  public java.util.Collection ejbFindByCondition(String condition) throws FinderException {    System.out.println("supplierBean 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("supplierBean ejbRemove");    try {            deleteRow(supplierID);         } catch (Exception ex) {              throw new EJBException("ejbRemove: " +                  ex.getMessage());         }  }//ejbRemove  public java.lang.String ejbFindByPrimaryKey(java.lang.String primaryKey) throws FinderException {         System.out.println("supplierBean 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("supplierBean ejbLoad");          try {              loadSupplier();           } catch (Exception ex) {                throw new EJBException("ejbLoad: " +                    ex.getMessage());           }      }//ejbLoad      public void ejbStore()      {            System.out.println("supplierBean ejbStore");            try {                storeSupplier();             } catch (Exception ex) {                  throw new EJBException("ejbStore: " +                      ex.getMessage());             }        }//ejbStore  public void ejbActivate()  {        System.out.println("supplierBean ejbActivate");        supplierID = (String)entityContext.getPrimaryKey();    }    public void ejbPassivate()    {        System.out.println("supplierBean ejbPassivate");        supplierID= null;    }  public void unsetEntityContext()  {    System.out.println("supplierBean ussetEntiyContext");    this.entityContext = null;  }  public void setEntityContext(EntityContext entityContext) {    System.out.println("supplierBean setEntiyContext");    this.entityContext = entityContext;  }  /****************************util methods*******************************/  private void makeConnection() {        System.out.println("supplierBean 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("supplierBean makeConnection out");    } // makeConnection    private void releaseConnection() {        System.out.println("supplierBean releaseConnection in");        try {            con.close();        } catch (SQLException ex) {             throw new EJBException("releaseConnection: " + ex.getMessage());        }        System.out.println("supplierBean releaseConnection out");    } // releaseConnection    private void insertRow () throws SQLException {        System.out.println("supplierBean insertRow in");        makeConnection();        String insertStatement =            "insert into supplier values ( ? , ? , ? , ? ,? ,?)";        PreparedStatement prepStmt =            con.prepareStatement(insertStatement);        prepStmt.setString(1, supplierID);        prepStmt.setString(2, company);        prepStmt.setString(3, person);        prepStmt.setString(4,phone);        prepStmt.setString(5,address);        prepStmt.setString(6,zip);        prepStmt.executeUpdate();        prepStmt.close();        releaseConnection();        System.out.println("supplierBean insertRow out");    }//insertRow    private Collection selectByCondition(String condition) throws SQLException {       System.out.println("supplierBean selectByCondition in");       makeConnection();       String selectStatement =               "select supplierID " +               "from supplier 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("supplierBean selectByCondition out");       return a;   }//selectByCondition   private boolean selectByPrimaryKey(String primaryKey) throws SQLException {        //本函数主要目的还在于确保supplier_id在数据库表中是存在的        System.out.println("supplierBean selectByPrimaryKey in");        makeConnection();        //a question here        String selectStatement =                "select supplierID " +                "from supplier where supplierID = ? ";        PreparedStatement prepStmt =                con.prepareStatement(selectStatement);        prepStmt.setString(1, primaryKey);        ResultSet rs = prepStmt.executeQuery();        boolean result = rs.next();        prepStmt.close();        releaseConnection();        System.out.println("supplierBean selectByPrimaryKey out");        return result;    }//selectByPrimaryKey    private void loadSupplier() throws SQLException {        System.out.println("supplierBean loadsupplier in");        makeConnection();        String selectStatement =                "select company, person,phone,address,zip " +                "from supplier where supplierID = ? ";        PreparedStatement prepStmt =                con.prepareStatement(selectStatement);        prepStmt.setString(1, supplierID);        ResultSet rs = prepStmt.executeQuery();        if (rs.next())        {            company = rs.getString(1);            person = rs.getString(2);            phone=rs.getString(3);            address=rs.getString(4);            zip=rs.getString(5);            prepStmt.close();            releaseConnection();        }        else {            prepStmt.close();            releaseConnection();            throw new NoSuchEntityException("loadsupplier:Row for id " +                supplierID + " not found in database.");        }        System.out.println("supplierBean loadsupplier out");    }//loadsupplier    private void storeSupplier() throws SQLException {        System.out.println("supplierBean storesupplier in");        makeConnection();        String updateStatement =                "update supplier " +                "set company = ? , person = ? , phone = ? ,address= ? ,zip= ? " +                "where supplierID = ? ";        PreparedStatement prepStmt =                con.prepareStatement(updateStatement);        prepStmt.setString(1,company);        prepStmt.setString(2, person);        prepStmt.setString(3,phone);        prepStmt.setString(4,address);        prepStmt.setString(5,zip);        prepStmt.setString(6, supplierID);        int rowCount = prepStmt.executeUpdate();        prepStmt.close();        releaseConnection();        if (rowCount == 0) {            throw new EJBException("storesupplier:Storing row for id " + supplierID + " failed.");        }        System.out.println("supplierBean storesupplier out");    }//storesupplier    private void deleteRow(String id) throws SQLException {        System.out.println("supplierBean deleteRow in");        makeConnection();        String deleteStatement =                "delete from supplier where supplierID = ? ";        PreparedStatement prepStmt =                con.prepareStatement(deleteStatement);        prepStmt.setString(1, id);        prepStmt.executeUpdate();        prepStmt.close();        releaseConnection();        System.out.println("supplierBean deleteRow out");    }/////deleteRow}

⌨️ 快捷键说明

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