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

📄 listtablebean.java~1~

📁 JSP+SQL应用开发网上书店系统,采用BMP模式开发
💻 JAVA~1~
字号:
package ejb;import javax.ejb.*;import java.util.*;import java.sql.*;import javax.sql.*;import javax.naming.*;import java.math.*;public class ListTableBean implements EntityBean {  EntityContext entityContext;  java.lang.String tableId;  java.lang.String bookId;  java.lang.String bookName;  java.lang.String bookAuthor;  java.lang.String bookDetail;  java.math.BigDecimal bookMoney;  java.lang.String userId;  DataSource ds;  private Connection con;  private String dbName = "java:/MSSQLDS";  public java.lang.String ejbCreate(java.lang.String tableId, java.lang.String bookId, java.lang.String bookName, java.lang.String bookAuthor, java.lang.String bookDetail, java.math.BigDecimal bookMoney, java.lang.String userId) throws CreateException {    this.tableId=tableId;    try{    insertRow(tableId,bookId, bookName, bookAuthor,bookDetail,bookMoney,userId);  }  catch(Exception ex){    throw new EJBException("ejbFindInRange: " +            ex.getMessage());  }    setUserId(userId);    setBookAuthor(bookAuthor);    setBookDetail(bookDetail);    setBookMoney(bookMoney);    setBookId(bookId);    setBookName(bookName);    setTableId(tableId);    return null;    return tableId;  }  public void ejbPostCreate(java.lang.String tableId, java.lang.String bookId, java.lang.String bookName, java.lang.String bookAuthor, java.lang.String bookDetail, java.math.BigDecimal bookMoney, java.lang.String userId) throws CreateException {    /**@todo Complete this method*/  }  public void ejbRemove() throws RemoveException {  }  public java.lang.String ejbFindByPrimaryKey(java.lang.String tableId) throws FinderException {    boolean result;     try {        result = selectByPrimaryKey(tableId);      } catch (Exception ex) {          throw new EJBException("ejbFindByPrimaryKey: " +             ex.getMessage());      }     if (result) {        return tableId;     }     else {        throw new ObjectNotFoundException           ("Row for id " + userId + " not found.");     }  }  abstract public java.lang.String ejbFindMethod1() throws FinderException {    /**@todo Complete this method*/    return null;  }  public void ejbLoad() {    try {         loadRow();       } catch (Exception ex) {           throw new EJBException("ejbLoad: " +              ex.getMessage());       }  }  public void ejbStore() {    try {          storeRow();        } catch (Exception ex) {           System.out.print("oh no" + ex.getMessage());        }  }  public void ejbActivate() {   tableId  = (String)entityContext.getPrimaryKey();  }  public void ejbPassivate() {     tableId = null;  }  public void unsetEntityContext() {      /*try {         con.close();      } catch (SQLException ex) {          throw new EJBException("unsetEntityContext: " + ex.getMessage());      }*/  }  public void setEntityContext(EntityContext entityContext) {    this.entityContext = entityContext;    try {      makeConnection();   } catch (Exception ex) {       throw new EJBException("Unable to connect to database. " +          ex.getMessage());   }  }  /*********************** Database Routines *************************/     private void makeConnection() throws NamingException, SQLException {        InitialContext ic = new InitialContext();        ds = (DataSource) ic.lookup(dbName);        //con =  ds.getConnection();     }     private String getTaId() throws SQLException {     String number = "0";     String selectStatement =           "select Max(TableId) " +           "from ListTable ";     Connection con = ds.getConnection();     PreparedStatement prepStmt =           con.prepareStatement(selectStatement);     ResultSet rs = prepStmt.executeQuery();     int num = rs.getInt(1)+1;     number = Integer.toString(num);     prepStmt.close();     con.close();     return number;   }     private boolean selectByPrimaryKey(String TableId) throws SQLException {     System.out.println("==selectByPrimaryKey===="+TableId);     Connection con = ds.getConnection();        String selectStatement =              "select TableId " +              "from ListTable where TableId = ? ";        PreparedStatement prepStmt =              con.prepareStatement(selectStatement);        prepStmt.setString(1, TableId);        ResultSet rs = prepStmt.executeQuery();        boolean result = rs.next();        prepStmt.close();        con.close();        return result;     }     private Collection selectByuserId(String userId) throws SQLException {  Connection con = ds.getConnection();   String selectStatement =         "select TableId " +         "from ListTable where userId = ? ";   PreparedStatement prepStmt =         con.prepareStatement(selectStatement);   prepStmt.setString(1, userId);   ResultSet rs = prepStmt.executeQuery();   ArrayList a = new ArrayList();   while (rs.next()) {      String TableId=rs.getString(1);      a.add(TableId);   }   prepStmt.close();   return a;} private void insertRow (java.lang.String TableId, java.lang.String BookId, java.lang.String BookName, java.lang.String BookAuthor, java.lang.String BookDetail, java.math.BigDecimal BookMoney, java.lang.String userId) throws SQLException {System.out.println("+++insertRow+++");          String insertStatement =               "insert into ListTable values ( ? , ? , ? , ? , ?)";            Connection con = ds.getConnection();          PreparedStatement prepStmt =                con.prepareStatement(insertStatement);           prepStmt.setString(1,TableId);          prepStmt.setString(2,BookId);          prepStmt.setString(3, BookName);          prepStmt.setString(4, BookAuthor);          prepStmt.setString(5, BookDetail);          prepStmt.setBigDecimal(6, BookMoney);          prepStmt.setString(7, userId);          prepStmt.executeUpdate();          prepStmt.close();          con.close();   } private void loadRow() throws SQLException {    System.out.println("+++loadRow+++"+userId);     Connection con = ds.getConnection();     String selectStatement =           "select BookId, BookName, BookAuthor, BookDetail,BookMoney,userId" +           "from ListTable where TableId = ? ";     PreparedStatement prepStmt =           con.prepareStatement(selectStatement);     prepStmt.setString(1, this.tableId);     ResultSet rs = prepStmt.executeQuery();     if (rs.next()) {        this.bookId = rs.getString(1);        this.bookName = rs.getString(2);        this.bookAuthor = rs.getString(3);        this. bookDetail = rs.getString(4);        this.bookMoney=rs.getBigDecimal(5);        this. userId = rs.getString(6);        prepStmt.close();        con.close();     }     else {        prepStmt.close();        throw new NoSuchEntityException("Row for TableId " + tableId +           " not found in database.");     }  }  private void deleteRow(String userId) throws SQLException {        System.out.println("+++deleteRow+++");         Connection con = ds.getConnection();     String deleteStatement =           "delete from ListTable where TableId = ? ";     PreparedStatement prepStmt =           con.prepareStatement(deleteStatement);     prepStmt.setString(1,tableId);     prepStmt.executeUpdate();     prepStmt.close();     con.close();  } private void storeRow() throws SQLException {       Connection con = ds.getConnection();    String updateStatement =          "update ListTable set BookId =  ? ," +          "BookName = ? ,BookAuthor = ? , BookDetail = ? ,BookMoney = ?,userId = ? " +          "where TableId=?";    PreparedStatement prepStmt =          con.prepareStatement(updateStatement);    prepStmt.setString(1, bookId);    prepStmt.setString(2, bookName);    prepStmt.setString(3, bookAuthor);    prepStmt.setString(6, userId);     prepStmt.setString(4, bookDetail);      prepStmt.setBigDecimal(5, bookMoney);    int rowCount = prepStmt.executeUpdate();    prepStmt.close();    con.close();    if (rowCount == 0) {       throw new EJBException("Storing row for userId " + userId + " failed.");    } } abstract public java.util.Collection ejbFindByuserId(String userId) throws FinderException {     Collection result;     try {        result = selectByuserId(userId);      } catch (Exception ex) {          throw new EJBException("ejbFindByLastName " +             ex.getMessage());      }      return result;  }  public java.lang.String getUserId() {    return userId;  }  public java.lang.String getBookId() {    return bookId;  }  public java.lang.String getBookName() {    return bookName;  }  public java.lang.String getBookAuthor() {    return bookAuthor;  }  public java.lang.String getBookDetail() {    return bookDetail;  }  public java.math.BigDecimal getBookMoney() {    return bookMoney;  }  public void setUserId(java.lang.String userId) {    this.userId = userId;   }  public void setBookId(java.lang.String bookId) {    this.bookId= bookId;  }  public void setBookName(java.lang.String bookName) {    this.bookName = bookName;  }  public void setBookAuthor(java.lang.String bookAuthor) {    this.bookAuthor = bookAuthor;  }  public void setBookDetail(java.lang.String bookDetail) {    this.bookDetail = bookDetail;  }  public void setBookMoney(java.math.BigDecimal bookMoney) {    this.bookMoney = bookMoney;  }  public java.lang.String getTableId() {    return tableId;  }  }

⌨️ 快捷键说明

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