📄 booksessionbean.java
字号:
/* * BookSessionBean.java * * Created on 2003年5月13日, 下午4:02 *//** * * @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 BookSessionBean implements SessionBean{ //private variables for our three entity beans private BookHome bookHome = null; private UserHome userHome = null; private BorrowHome borrowHome = null; //Session user_name private String userName = null; //session bean's method public void ejbCreate() throws CreateException { try { bookHome = lookupBook(); userHome = lookupUser(); borrowHome = lookupBorrow(); } catch (NamingException ex) { throw new CreateException(ex.getMessage()); } } public void ejbActivate() throws javax.ejb.EJBException, java.rmi.RemoteException { try { bookHome = lookupBook(); userHome = lookupUser(); borrowHome = lookupBorrow(); } catch (NamingException ex) { throw new EJBException(ex.getMessage()); } } public void ejbPassivate() throws javax.ejb.EJBException, java.rmi.RemoteException { bookHome = null; userHome = null; borrowHome = null; } public void ejbRemove() throws javax.ejb.EJBException, java.rmi.RemoteException { } public void setSessionContext(javax.ejb.SessionContext sessionContext) throws javax.ejb.EJBException, java.rmi.RemoteException { } //business method public Vector getAllBook(String str) throws RemoteException { System.out.println("In getAllBook()"); Collection c; try { c = bookHome.findByBookName(""); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } Iterator i=c.iterator(); Vector v=new Vector(); Vector row=new Vector(); row.add("ID"); row.add("书名"); row.add("作者"); row.add("出版社"); row.add("出版日期"); row.add("说明"); v.add(row); while (i.hasNext()) { Book b = (Book)i.next(); row=new Vector(); //add the book_id row.add((String)b.getPrimaryKey()); //add the book_name row.add(b.getBookName()); //add the book_author row.add(b.getBookAuthor()); //add the book_publisher row.add(b.getBookPublisher()); //add the book_date row.add(b.getBookDate()); //add the book_note row.add(b.getBookNote()); v.add(row); } return v; } public Vector getQueryBook(String bookName) throws RemoteException { System.out.println("In getQueryBook()"); Collection c; try { c = bookHome.findByBookName(bookName); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } Iterator i=c.iterator(); Vector v=new Vector(); Vector row=new Vector(); row.add("ID"); row.add("书名"); row.add("作者"); row.add("出版社"); row.add("出版日期"); row.add("说明"); v.add(row); while (i.hasNext()) { Book b = (Book)i.next(); row=new Vector(); //add the book_id row.add((String)b.getPrimaryKey()); //add the book_name row.add(b.getBookName()); //add the book_author row.add(b.getBookAuthor()); //add the book_publisher row.add(b.getBookPublisher()); //add the book_date row.add(b.getBookDate()); //add the book_note row.add(b.getBookNote()); v.add(row); } return v; } public Vector getBorrowedBook(String str) throws RemoteException { System.out.println("In getBorrowedBook()"); Collection c; try { c = borrowHome.findByUserName(userName); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } Iterator i=c.iterator(); Vector v=new Vector(); Vector row=new Vector(); row.add("ID"); row.add("用户名"); row.add("书ID"); row.add("借阅日期"); v.add(row); while (i.hasNext()) { Borrow b = (Borrow)i.next(); row=new Vector(); //add the borrow_id row.add((String)b.getPrimaryKey()); //add the user_name row.add(b.getUserName()); //add the book_id row.add(b.getBookId()); //add the borrow_date row.add(b.getBorrowDate()); v.add(row); } return v; } public Vector getQueryBorrowedBook(String bookId) throws RemoteException { System.out.println("In getQueryBorrowedBook()"); Collection c; try { c = borrowHome.findByUserNameBookId(userName,bookId); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } Iterator i=c.iterator(); Vector v=new Vector(); Vector row=new Vector(); row.add("ID"); row.add("用户名"); row.add("书ID"); row.add("借阅日期"); v.add(row); while (i.hasNext()) { Borrow b = (Borrow)i.next(); row=new Vector(); //add the borrow_id row.add((String)b.getPrimaryKey()); //add the user_name row.add(b.getUserName()); //add the book_id row.add(b.getBookId()); //add the borrow_date row.add(b.getBorrowDate()); v.add(row); } return v; } public int borrowBook(String bookId) throws RemoteException { System.out.println("In borrowBook()"); Borrow b; try { b=(Borrow)borrowHome.create(userName,bookId); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } return 1; } public int returnBook(String borrowId) throws RemoteException { System.out.println("In returnBook()"); Borrow b; try { b=(Borrow)borrowHome.findByPrimaryKey(borrowId); b.setReturned(); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } return 1; } public int checkUser(String userName,String userPassword) throws RemoteException { System.out.println("In checkUser()"); User u=null; Collection c=null; int result=0; try { c=userHome.findByUserName(userName); // System.out.println("In checkUser() try{}"); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } // System.out.println("In checkUser() collection:"+c); Iterator i=c.iterator(); if(i.hasNext()) u=(User)i.next(); if(u==null) return 0; // System.out.println("In checkUser() get user password,id="+(String)u.getPrimaryKey()+" name:"+(String)u.getUserName() ); String pass=u.getUserPassword(); // System.out.println("In checkUser() check password"); if(pass.compareTo(userPassword)==0) { result=1; this.userName=userName; } // System.out.println("End checkUser() "); return result; } //=============================helper functions=============================== private BookHome lookupBook() throws NamingException { Context initial = new InitialContext(); Object objref = initial.lookup("java:comp/env/ejb/SimpleBook"); return (BookHome) objref; } private UserHome lookupUser() throws NamingException { Context initial = new InitialContext(); Object objref = initial.lookup("java:comp/env/ejb/SimpleUser"); return (UserHome) objref; } private BorrowHome lookupBorrow() throws NamingException { Context initial = new InitialContext(); Object objref = initial.lookup("java:comp/env/ejb/SimpleBorrow"); return (BorrowHome) objref; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -