📄 borrowbean.java
字号:
/* * BorrowBean.java * * Created on 2003年5月13日, 上午11:48 *//** * * @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 BorrowBean implements EntityBean{ //entity variables private String id; private String userName; private String bookId; private String borrowDate; private String returned; //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 public String ejbCreate(String userName,String bookId) throws CreateException { try { this.id=insertRow(userName, bookId); } catch (Exception ex) { throw new EJBException("ejbCreate: " + ex.getMessage()); } this.userName = userName; this.bookId = bookId;// this.borrowDate = ;// this.returned=new String("0"); return id; } public void ejbPostCreate(String userName,String bookId) { } 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 ejbFindByUserName(String userName) throws FinderException { Collection result; try { result = selectByUserName(userName); } catch (Exception ex) { throw new EJBException("ejbFindByUserName " + ex.getMessage()); } return result; } public Collection ejbFindByUserNameBookId(String userName,String bookId) throws FinderException { Collection result; try { result = selectByUserNameBookId(userName,bookId); } catch (Exception ex) { throw new EJBException("ejbFindByUserName " + ex.getMessage()); } return result; } //business method public String getUserName() throws RemoteException { return userName; } public String getBookId() throws RemoteException { return bookId; } public String getBorrowDate() throws RemoteException { return borrowDate; } public String getReturned() throws RemoteException { return returned; } public void setReturned() throws RemoteException { returned="1"; } //========================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 borrow 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 selectByUserName(String userName) throws SQLException { String selectStatement = "select id " + "from borrow where user_name = ? and returned='0'"; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, userName); ResultSet rs = prepStmt.executeQuery(); ArrayList a = new ArrayList(); while (rs.next()) { String id = rs.getString(1); a.add(id); } prepStmt.close(); return a; } private Collection selectByUserNameBookId(String userName,String bookId) throws SQLException { String selectStatement = "select id " + "from borrow where user_name = ? and book_id like ? and returned='0'"; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, userName); prepStmt.setString(2, "%"+bookId+"%"); 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 user_name,book_id,borrow_date,returned " + "from borrow where id = ? "; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setInt(1, Integer.parseInt(this.id)); ResultSet rs = prepStmt.executeQuery(); if (rs.next()) { this.userName = rs.getString(1); this.bookId = rs.getString(2); this.borrowDate = rs.getString(3); this.returned = rs.getString(4); prepStmt.close(); } else { prepStmt.close(); throw new NoSuchEntityException("Row for id " + id + " not found in database."); } } private void storeRow() throws SQLException { String updateStatement = "update borrow set user_name = ? ," + "book_id = ? , borrow_date = ? , returned = ? " + "where id = ?"; PreparedStatement prepStmt = con.prepareStatement(updateStatement); prepStmt.setString(1, userName); prepStmt.setString(2, bookId); prepStmt.setString(3, borrowDate); prepStmt.setString(4, returned); prepStmt.setInt(5, Integer.parseInt(this.id)); int rowCount = prepStmt.executeUpdate(); prepStmt.close(); if (rowCount == 0) { throw new EJBException("Storing row for id " + id + " failed."); } } private String insertRow (String userName, String bookId) throws SQLException { System.out.println("In InsertRow,userName="+userName+",book_id="+bookId); String insertStatement = "insert into borrow(user_name,book_id,borrow_date,returned) values(?,?,?,?) "; PreparedStatement prepStmt = con.prepareStatement(insertStatement); String curTime=(java.util.Calendar.getInstance().getTime()).toString(); prepStmt.setString(1, userName); prepStmt.setString(2, bookId); prepStmt.setString(3, curTime); prepStmt.setString(4, "0"); //==================need lock==================== //System.out.println("Insert sql:"+insertStatement); prepStmt.executeUpdate(); prepStmt.close(); System.out.println("Insert complete! date="+(java.util.Calendar.getInstance().getTime()).toString()); //get the id String selectStatement = "select id " + "from borrow where user_name=? order by id desc "; prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1,userName); ResultSet rs = prepStmt.executeQuery(); rs.next(); String result=rs.getString(1); prepStmt.close(); //=================unlock==================== System.out.println("End InsertRow"); this.id=result; this.userName=userName; this.bookId=bookId; this.borrowDate=curTime; this.returned="0"; return result; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -