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

📄 bookbean.java

📁 第一步:将MyBookApp目录复制到C:下。 第二步:将book.mdb复制到C:下。 第三步:配制ODBC数据源
💻 JAVA
字号:
/* * BookBean.java * * Created on 2003年5月12日, 下午10:11 *//** * * @author  administrator */import java.sql.*;import javax.sql.*;import java.util.*;import java.math.*;import javax.ejb.*;import javax.naming.*;import javax.rmi.PortableRemoteObject;import java.rmi.RemoteException;public class BookBean implements EntityBean{    //entity variables    private String id;    private String bookName;    private String bookAuthor;    private String bookPublisher;    private String bookDate;    private String bookNote;        //other variables    private EntityContext context;    private Connection con;    private String dbName = "java:comp/env/jdbc/book";        //entity bean's method    public void ejbActivate() throws javax.ejb.EJBException, java.rmi.RemoteException     {        id = (String)context.getPrimaryKey();    }        public void ejbLoad() throws javax.ejb.EJBException, java.rmi.RemoteException     {        try {         loadRow();       } catch (Exception ex) {           throw new EJBException("ejbLoad: " +               ex.getMessage());       }    }        public void ejbPassivate() throws javax.ejb.EJBException, java.rmi.RemoteException     {        id = null;    }        //we don't use this method for this programm    public void ejbRemove() throws javax.ejb.RemoveException, javax.ejb.EJBException, java.rmi.RemoteException     {    }        public void ejbStore() throws javax.ejb.EJBException, java.rmi.RemoteException     {        try {         storeRow();       } catch (Exception ex) {           throw new EJBException("ejbStore: " +               ex.getMessage());       }    }        public void setEntityContext(javax.ejb.EntityContext entityContext) throws javax.ejb.EJBException, java.rmi.RemoteException     {      this.context = entityContext;      try {         makeConnection();      } catch (Exception ex) {          throw new EJBException("Unable to connect to database. " +             ex.getMessage());      }    }        public void unsetEntityContext() throws javax.ejb.EJBException, java.rmi.RemoteException     {        try {         con.close();      } catch (SQLException ex) {          throw new EJBException("unsetEntityContext: " + ex.getMessage());      }    }          //home method    //we don't use this method for this programm    public String ejbCreate() throws CreateException     {        return "1";    }        public void ejbPostCreate()     {    }        public String ejbFindByPrimaryKey(String primaryKey) throws FinderException     {      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 Collection ejbFindByBookName(String bookName) throws FinderException     {      Collection result;      try {         result = selectByBookName(bookName);       } catch (Exception ex) {           throw new EJBException("ejbFindByBookName " +               ex.getMessage());       }       return result;    }        //business method    public String getBookName() throws RemoteException    {        return bookName;    }         public String getBookAuthor() throws RemoteException   {       return bookAuthor;   }       public String getBookPublisher() throws RemoteException   {       return bookPublisher;   }        public String getBookDate() throws RemoteException   {       return bookDate;   }        public String getBookNote() throws RemoteException   {       return bookNote;   }        //========================helper functions========================    private void makeConnection() throws NamingException, SQLException     {      InitialContext ic = new InitialContext();      DataSource ds = (DataSource) ic.lookup(dbName);      con =  ds.getConnection();    }        private boolean selectByPrimaryKey(String primaryKey) throws SQLException     {      String selectStatement =            "select id " +            "from book where id = ? ";      PreparedStatement prepStmt =con.prepareStatement(selectStatement);      prepStmt.setInt(1, Integer.parseInt(primaryKey));      ResultSet rs = prepStmt.executeQuery();      boolean result = rs.next();      prepStmt.close();      return result;    }        private Collection selectByBookName(String bookName) throws SQLException     {      String selectStatement =            "select id " +            "from book where book_name like ? ";      PreparedStatement prepStmt =             con.prepareStatement(selectStatement);      prepStmt.setString(1, "%"+bookName+"%");      ResultSet rs = prepStmt.executeQuery();      ArrayList a = new ArrayList();      while (rs.next()) {         String id = rs.getString(1);         a.add(id);      }      prepStmt.close();      return a;    }        private void loadRow() throws SQLException {      String selectStatement =            "select book_name,book_author,book_publisher,book_date,book_note " +            "from book where id = ? ";      PreparedStatement prepStmt =             con.prepareStatement(selectStatement);      prepStmt.setInt(1, Integer.parseInt(this.id));      ResultSet rs = prepStmt.executeQuery();      if (rs.next()) {         this.bookName = rs.getString(1);         this.bookAuthor = rs.getString(2);         this.bookPublisher = rs.getString(3);         this.bookDate = rs.getString(4);         this.bookNote = rs.getString(5);         prepStmt.close();      }      else {         prepStmt.close();         throw new NoSuchEntityException("Row for id " + id +            " not found in database.");      }   }   private void storeRow() throws SQLException    {      String updateStatement =            "update book set book_name =  ? ," +            "book_author = ? , book_publisher = ? ," +            "book_date = ? , book_note = ? " +            "where id = ?";      PreparedStatement prepStmt =             con.prepareStatement(updateStatement);      prepStmt.setString(1, bookName);      prepStmt.setString(2, bookAuthor);      prepStmt.setString(3, bookPublisher);      prepStmt.setString(4, bookDate);      prepStmt.setString(5, bookNote);      prepStmt.setInt(6, Integer.parseInt(this.id));      int rowCount = prepStmt.executeUpdate();      prepStmt.close();      if (rowCount == 0) {         throw new EJBException("Storing row for id " + id + " failed.");      }   }            }

⌨️ 快捷键说明

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