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

📄 commentbean.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 CommentBean implements EntityBean {  java.lang.String commentID;  java.lang.String customerID;  java.lang.String bookID;  int rank;  java.lang.String content;  private Connection con=null;  EntityContext entityContext;  /**************************business methods**********************************/  public void setCommentID(java.lang.String commentID) {    this.commentID = commentID;  }  public void setCustomerID(java.lang.String customerID) {    this.customerID = customerID;  }  public void setBookID(java.lang.String bookID) {    this.bookID = bookID;  }  public void setRank(int rank) {    this.rank = rank;  }  public void setContent(java.lang.String content) {    this.content = content;  }  public java.lang.String getCommentID() {    return commentID;  }  public java.lang.String getCustomerID() {    return customerID;  }  public java.lang.String getBookID() {    return bookID;  }  public int getRank() {    return rank;  }  public java.lang.String getContent() {    return content;  }  public CommentDetails getDetails()    {       System.out.println("commentBean getDetails");       return new CommentDetails (commentID,customerID ,bookID,rank,content);    }//getDetails   /********************************ejb methods**************************/   public java.lang.String ejbCreate(CommentDetails commentValue) throws CreateException {     System.out.println("commentBean ejbCreate");         try         {           makeConnection();           this.commentID=DBHelper.getNextCommentID(con);         }catch(Exception ex)         {           throw new EJBException("ejbCreate: "+ex.getMessage());         }         this.customerID=commentValue.getCustomerID();         this.bookID=commentValue.getBookID();         this.rank=commentValue.getRank();         this.content=commentValue.getContent();         try {             insertRow();         } catch (Exception ex) {              throw new EJBException("ejbCreate: " +                  ex.getMessage());         }         return commentID;   }   public void ejbPostCreate(CommentDetails commentValue) throws CreateException {     System.out.println("commentBean ejbPostCreate");   }   public java.util.Collection ejbFindByCondition(String condition) throws FinderException {     System.out.println("commentBean 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("commentBean ejbRemove");     try {             deleteRow(commentID);          } catch (Exception ex) {               throw new EJBException("ejbRemove: " +                   ex.getMessage());          }   }//ejbRemove   public java.lang.String ejbFindByPrimaryKey(java.lang.String primaryKey) throws FinderException {          System.out.println("commentBean 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("commentBean ejbLoad");           try {               loadComment();            } catch (Exception ex) {                 throw new EJBException("ejbLoad: " +                     ex.getMessage());            }       }//ejbLoad       public void ejbStore()       {             System.out.println("commentBean ejbStore");             try {                 storeComment();              } catch (Exception ex) {                   throw new EJBException("ejbStore: " +                       ex.getMessage());              }         }//ejbStore   public void ejbActivate()   {         System.out.println("commentBean ejbActivate");         //         commentID = (String)entityContext.getPrimaryKey();     }     public void ejbPassivate()     {         System.out.println("commentBean ejbPassivate");         commentID= null;     }   public void unsetEntityContext()   {     System.out.println("commentBean ussetEntiyContext");     this.entityContext = null;   }   public void setEntityContext(EntityContext entityContext) {     System.out.println("commentBean setEntiyContext");     this.entityContext = entityContext;   }   /****************************util methods*******************************/   private void makeConnection() {         System.out.println("commentBean 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("commentBean makeConnection out");     } // makeConnection     private void releaseConnection() {         System.out.println("commentBean releaseConnection in");         try {             con.close();         } catch (SQLException ex) {              throw new EJBException("releaseConnection: " + ex.getMessage());         }         System.out.println("commentBean releaseConnection out");     } // releaseConnection     private void insertRow () throws SQLException {         System.out.println("commentBean insertRow in");         makeConnection();         String insertStatement =             "insert into comment values ( ? , ? , ? , ? ,?)";         PreparedStatement prepStmt =             con.prepareStatement(insertStatement);         prepStmt.setString(1, commentID);         prepStmt.setString(2, customerID);         prepStmt.setString(3, bookID);         prepStmt.setInt(4,rank);         prepStmt.setString(5,content);         prepStmt.executeUpdate();         prepStmt.close();         releaseConnection();         System.out.println("commentBean insertRow out");     }//insertRow     private Collection selectByCondition(String condition) throws SQLException {        System.out.println("commentBean selectByCondition in");        makeConnection();        String selectStatement =                "select commentID " +                "from comment 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("commentBean selectByCondition out");        return a;    }//selectByCondition    private boolean selectByPrimaryKey(String primaryKey) throws SQLException {         //本函数主要目的还在于确保comment_id在数据库表中是存在的         System.out.println("commentBean selectByPrimaryKey in");         makeConnection();         //a question here         String selectStatement =                 "select commentID " +                 "from comment where commentID = ? ";         PreparedStatement prepStmt =                 con.prepareStatement(selectStatement);         prepStmt.setString(1, primaryKey);         ResultSet rs = prepStmt.executeQuery();         boolean result = rs.next();         prepStmt.close();         releaseConnection();         System.out.println("commentBean selectByPrimaryKey out");         return result;     }//selectByPrimaryKey     private void loadComment() throws SQLException {         System.out.println("commentBean loadcomment in");         makeConnection();         String selectStatement =                 "select CustomerID, BookID,Rank,Content " +                 "from comment where commentID = ? ";         PreparedStatement prepStmt =                 con.prepareStatement(selectStatement);         prepStmt.setString(1, commentID);         ResultSet rs = prepStmt.executeQuery();         if (rs.next())         {             customerID = rs.getString(1);             bookID = rs.getString(2);             rank=rs.getInt(3);             content=rs.getString(4);             prepStmt.close();             releaseConnection();         }         else {             prepStmt.close();             releaseConnection();             throw new NoSuchEntityException("loadcomment:Row for id " +                 commentID + " not found in database.");         }         System.out.println("commentBean loadcomment out");     }//loadcomment     private void storeComment() throws SQLException {         System.out.println("commentBean storecomment in");         makeConnection();         String updateStatement =                 "update comment " +                 "set customerID = ? , bookID = ? , rank = ? ,content= ? " +                 "where commentID = ? ";         PreparedStatement prepStmt =                 con.prepareStatement(updateStatement);         prepStmt.setString(1,customerID);         prepStmt.setString(2, bookID);         prepStmt.setInt(3,rank);         prepStmt.setString(4,content);         prepStmt.setString(5, commentID);         int rowCount = prepStmt.executeUpdate();         prepStmt.close();         releaseConnection();         if (rowCount == 0) {             throw new EJBException("storecomment:Storing row for id " + commentID + " failed.");         }         System.out.println("commentBean storecomment out");     }//storecomment     private void deleteRow(String id) throws SQLException {         System.out.println("commentBean deleteRow in");         makeConnection();         String deleteStatement =                 "delete from comment where commentID = ? ";         PreparedStatement prepStmt =                 con.prepareStatement(deleteStatement);         prepStmt.setString(1, id);         prepStmt.executeUpdate();         prepStmt.close();         releaseConnection();         System.out.println("commentBean deleteRow out");     }////deleteRow}

⌨️ 快捷键说明

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