📄 bookbean.java~38~
字号:
package bookstore.ejb;import bookstore.util.*;import java.sql.*;import javax.sql.*;import java.util.*;import javax.ejb.*;import javax.naming.*;public class BookBean implements EntityBean{ java.lang.String bookID; java.lang.String name; java.lang.String author; java.lang.String translator; int pageNum; int size; java.lang.String publisher; java.sql.Date publishDate; int stockNum; int saleNum; int monthSaleNum; java.lang.String sort; int rank; float price; byte[] image; private Connection con=null; EntityContext entityContext; int preMonthSaleNum; /*****************************business methods******************************/ public void setBookID(java.lang.String bookID) { this.bookID = bookID; } public void setName(java.lang.String name) { this.name = name; } public void setAuthor(java.lang.String author) { this.author = author; } public void setTranslator(java.lang.String translator) { this.translator = translator; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public void setSize(int size) { this.size = size; } public void setPublisher(java.lang.String publisher) { this.publisher = publisher; } public void setPublishDate(java.sql.Date publishDate) { this.publishDate = publishDate; } public void setStockNum(int stockNum) { this.stockNum = stockNum; } public void setSaleNum(int saleNum) { this.saleNum = saleNum; } public void setMonthSaleNum(int monthSaleNum) { this.monthSaleNum = monthSaleNum; } public void setSort(java.lang.String sort) { this.sort = sort; } public void setRank(int rank) { this.rank = rank; } public void setPrice(float price) { this.price = price; } public void setImage(byte[] image) { this.image = image; } public void setPreMonthSaleNum(int preMonthSaleNum) { this.preMonthSaleNum = preMonthSaleNum; } public java.lang.String getBookID() { return bookID; } public java.lang.String getName() { return name; } public java.lang.String getAuthor() { return author; } public java.lang.String getTranslator() { return translator; } public int getPageNum() { return pageNum; } public int getSize() { return size; } public java.lang.String getPublisher() { return publisher; } public java.sql.Date getPublishDate() { return publishDate; } public int getStockNum() { return stockNum; } public int getSaleNum() { return saleNum; } public int getMonthSaleNum() { return monthSaleNum; } public java.lang.String getSort() { return sort; } public int getRank() { return rank; } public float getPrice() { return price; } public byte[] getImage() { return image; } public int getPreMonthSaleNum() { return preMonthSaleNum; } public BookDetails getDetails() { System.out.println("bookBean getDetails"); return new BookDetails (bookID, name,author,translator,pageNum, size,publisher,publishDate,stockNum,saleNum,preMonthSaleNum,monthSaleNum, sort,rank,price,image); }//getDetails /**********************************ejb methods*************************/ public java.lang.String ejbCreate(BookDetails bookValue) throws CreateException { System.out.println("bookBean ejbCreate"); try { makeConnection(); this.bookID=DBHelper.getNextBookID(con); }catch(Exception ex) { throw new EJBException("ejbCreate: "+ex.getMessage()); } this.name=bookValue.getName(); this.author=bookValue.getAuthor(); this.translator=bookValue.getTranslator(); this.pageNum=bookValue.getPageNum(); this.size=bookValue.getSize(); this.publisher=bookValue.getPublisher(); this.publishDate=bookValue.getPublishDate(); this.stockNum=bookValue.getStockNum(); this.saleNum=bookValue.getSaleNum(); this.preMonthSaleNum=bookValue.getPreMonthSaleNum(); this.monthSaleNum=bookValue.getMonthSaleNum(); this.sort=bookValue.getSort(); this.rank=bookValue.getRank(); this.price=bookValue.getPrice(); this.image=bookValue.getImage(); try { insertRow(); } catch (Exception ex) { throw new EJBException("ejbCreate: " + ex.getMessage()); } return bookID; } public void ejbPostCreate(BookDetails bookValue) throws CreateException { System.out.println("bookBean ejbPostCreate"); } public Collection ejbFindByCondition(String condition) throws FinderException { System.out.println("bookBean ejbFindByCondition"); Collection result; try { result = selectByCondition(condition); } catch (Exception ex) { throw new EJBException("ejbFindByCondition " + ex.getMessage()); } return result; } public Collection ejbFindTopTen(String condition) throws FinderException { System.out.println("bookBean ejbFindTopTen"); 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("bookBean ejbRemove"); try { deleteRow(bookID); } catch (Exception ex) { throw new EJBException("ejbRemove: " + ex.getMessage()); } }//ejbRemove public java.lang.String ejbFindByPrimaryKey(java.lang.String primaryKey) throws FinderException { System.out.println("bookBean 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("bookBean ejbLoad"); try { loadBook(); } catch (Exception ex) { throw new EJBException("ejbLoad: " + ex.getMessage()); } }//ejbLoad public void ejbStore() { System.out.println("bookBean ejbStore"); try { storeBook(); } catch (Exception ex) { throw new EJBException("ejbStore: " + ex.getMessage()); } }//ejbStore public void ejbActivate() { System.out.println("bookBean ejbActivate"); bookID = (String)entityContext.getPrimaryKey(); } public void ejbPassivate() { System.out.println("bookBean ejbPassivate"); bookID= null; } public void unsetEntityContext() { System.out.println("bookBean ussetEntiyContext"); this.entityContext = null; } public void setEntityContext(EntityContext entityContext) { System.out.println("bookBean setEntiyContext"); this.entityContext = entityContext; } /******************************util methods*************************/ private void makeConnection() { System.out.println("bookBean 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("bookBean makeConnection out"); } // makeConnection private void releaseConnection() { System.out.println("bookBean releaseConnection in"); try { con.close(); } catch (SQLException ex) { throw new EJBException("releaseConnection: " + ex.getMessage()); } System.out.println("bookBean releaseConnection out"); } // releaseConnection private void insertRow () throws SQLException { System.out.println("bookBean insertRow in"); makeConnection(); String insertStatement = "insert into book values ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ,?)"; PreparedStatement prepStmt = con.prepareStatement(insertStatement); prepStmt.setString(1, bookID); prepStmt.setString(2, name); prepStmt.setString(3, author); prepStmt.setString(4, translator); prepStmt.setInt(5,pageNum); prepStmt.setInt(6,size); prepStmt.setString(7,publisher); prepStmt.setDate(8,publishDate); prepStmt.setInt(9, stockNum); prepStmt.setInt(10,saleNum); prepStmt.setInt(11,preMonthSaleNum); prepStmt.setInt(12, monthSaleNum); prepStmt.setString(13, sort); prepStmt.setInt(14,rank); prepStmt.setFloat(15,price); prepStmt.setBytes(16,image); prepStmt.executeUpdate(); prepStmt.close(); releaseConnection(); System.out.println("bookBean insertRow out"); }//insertRow private Collection selectByCondition(String condition) throws SQLException { System.out.println("bookBean selectByCondition in"); makeConnection(); String selectStatement = "select bookID " + "from book 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("bookBean selectByCondition out"); return a; }//selectByCondition private boolean selectByPrimaryKey(String primaryKey) throws SQLException { //本函数主要目的还在于确保book_id在数据库表中是存在的 System.out.println("bookBean selectByPrimaryKey in"); makeConnection(); //a question here String selectStatement = "select bookID " + "from book where bookID = ? "; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, primaryKey); ResultSet rs = prepStmt.executeQuery(); boolean result = rs.next(); prepStmt.close(); releaseConnection(); System.out.println("bookBean selectByPrimaryKey out"); return result; }//selectByPrimaryKey private void loadBook() throws SQLException { System.out.println("bookBean loadbook in"); makeConnection(); String selectStatement = "select Name, author, translator,pageNum,size,publisher,publishDate,"+ "stockNum,saleNum,preMonthSaleNum,monthSaleNum,sort,rank,price,image " + "from book where bookID = ? "; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, bookID); ResultSet rs = prepStmt.executeQuery(); if (rs.next()) { name = rs.getString(1); author = rs.getString(2); translator = rs.getString(3); pageNum = rs.getInt(4); size = rs.getInt(5); publisher=rs.getString(6); publishDate=rs.getDate(7); stockNum=rs.getInt(8); saleNum=rs.getInt(9); preMonthSaleNum=rs.getInt(10); monthSaleNum=rs.getInt(11); sort=rs.getString(12); rank=rs.getInt(13); price=rs.getFloat(14); image=rs.getBytes(15); prepStmt.close(); releaseConnection(); } else { prepStmt.close(); releaseConnection(); throw new NoSuchEntityException("loadbook:Row for id " + bookID + " not found in database."); } System.out.println("bookBean loadbook out"); }//loadbook private void storeBook() throws SQLException { System.out.println("bookBean storebook in"); makeConnection(); String updateStatement = "update book " + "set Name = ? , author = ? , translator = ? , pageNum = ? ,"+ "size=?,publisher= ? ,publishDate= ?,stockNum= ? ,"+ "saleNum = ? ,preMonthSaleNum= ? ,monthSaleNum= ? ,sort = ? ,rank = ? ,price= ?,image= ? " + "where bookID = ? "; PreparedStatement prepStmt = con.prepareStatement(updateStatement); prepStmt.setString(1, name); prepStmt.setString(2, author); prepStmt.setString(3, translator); prepStmt.setInt(4, pageNum); prepStmt.setInt(5, size); prepStmt.setString(6,publisher); prepStmt.setDate(7,publishDate); prepStmt.setInt(8,stockNum); prepStmt.setInt(9,saleNum); prepStmt.setInt(10,preMonthSaleNum); prepStmt.setInt(11,monthSaleNum); prepStmt.setString(12,sort); prepStmt.setInt(13,rank); prepStmt.setFloat(14,price); prepStmt.setBytes(15,image); prepStmt.setString(16, bookID); int rowCount = prepStmt.executeUpdate(); prepStmt.close(); releaseConnection(); if (rowCount == 0) { throw new EJBException("storebook:Storing row for id " + bookID + " failed."); } System.out.println("bookBean storebook out"); }//storebook private void deleteRow(String id) throws SQLException { System.out.println("bookBean deleteRow in"); makeConnection(); String deleteStatement = "delete from book where bookID = ? "; PreparedStatement prepStmt = con.prepareStatement(deleteStatement); prepStmt.setString(1, id); prepStmt.executeUpdate(); prepStmt.close(); releaseConnection(); System.out.println("bookBean deleteRow out"); }////deleteRow}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -