📄 mylistbean.java
字号:
package ejb;import hell.*;import ejb.*;import java.sql.*;import javax.sql.*;import javax.naming.*;import javax.ejb.*;import java.util.*;import java.math.*;import javax.naming.Context;import javax.naming.InitialContext;import javax.rmi.PortableRemoteObject;public class MylistBean implements SessionBean { SessionContext sessionContext; private userHome home2; private Connection con; private ListTableHome ListTablehome; private String dbName = "java:/MSSQL$XZW"; public void ejbCreate() throws CreateException { try{ InitialContext initial = new InitialContext(); home2 = (userHome)initial.lookup("user"); ListTablehome = (ListTableHome)initial.lookup("ListTable"); makeConnection(); } catch (Exception ex) { throw new EJBException("Unable to connect to database. " + ex.getMessage()); } } public void ejbRemove() { try { con.close(); } catch (SQLException ex) { throw new EJBException("ejbRemove: " + ex.getMessage()); } } public void ejbActivate() { try { makeConnection(); } catch (Exception ex) { throw new EJBException("ejbActivate Exception: " + ex.getMessage()); } } public void ejbPassivate() { try { con.close(); } catch (SQLException ex) { throw new EJBException("ejbRemove: " + ex.getMessage()); } } public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; try { makeConnection(); } catch (Exception ex) { throw new EJBException("Unable to connect to database. " + ex.getMessage()); } } public Vector DfindDTableIfoByuserId(String userId) { Vector result; try { result = selectDTableIfoByuserId(userId); } catch (Exception ex) { throw new EJBException("DfinduserTableIfoByuserId " + ex.getMessage()); } return result; } public void Createuser(java.lang.String userId, java.lang.String userName, java.lang.String password) { System.out.println("Createuser error"); try{ user a = home2.create( userId, userName,password ); }catch(Exception ex){ throw new EJBException("setDverify catch an Exception:" + ex.getMessage()); } } public Vector DfindListTablebyuserId(String userId) { Vector result; try { result = selectListTablebyuserId(userId); } catch (Exception ex) { throw new EJBException("DfindListTablebyuserId " + ex.getMessage()); } return result; } /*********************** Database Routines *************************/ private void makeConnection() throws NamingException, SQLException { InitialContext ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup(dbName); con = ds.getConnection(); } private Vector selectListTablebyuserId(String userId) throws SQLException { System.out.println("selectListTablebyuserId error"+userId ); String selectStatement = "select TableId,BookId,BookName,BookAuthor,BookDetail,BookMoney " + "from ListTable where userId = ? "; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, userId); ResultSet rs = prepStmt.executeQuery(); Vector a = new Vector(); while (rs.next()) { String TableId = rs.getString(1); String BookId = rs.getString(2); String BookName = rs.getString(3); String BookAuthor = rs.getString(4); String BookDetail = rs.getString(5); BigDecimal BookMoney = rs.getBigDecimal(6); a.add(new ListDetail(TableId,BookId,BookName,BookAuthor,BookDetail,BookMoney,userId)); } System.out.println(a); prepStmt.close(); return a; } private Vector selectDTableIfoByuserId(String userId) throws SQLException { System.out.println("selectDTableIfoByuserId error" ); String selectStatement = "select userName,password " + " from usertable where userId = ? "; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, userId); ResultSet rs = prepStmt.executeQuery(); Vector a = new Vector(); while (rs.next()) { String userName= rs.getString(1); String password = rs.getString(2); a.add(new userdetail(userId,userName,password)); } System.out.println(a); prepStmt.close(); return a; } public Vector findbookbyname(String bookName) { Vector result; try { result = selectbookbyname(bookName); } catch (Exception ex) { throw new EJBException("selectbookbyname --" + ex.getMessage()); } return result; } private Vector selectbookbyname(String bookName) throws SQLException { System.out.println("--selectbookbyname-- "+bookName ); String selectStatement = "select * " + " from BookList where BookName = ? "; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, bookName); ResultSet rs = prepStmt.executeQuery(); Vector a = new Vector(); while (rs.next()) { String BookId= rs.getString(1); String BookName = rs.getString(2); String BookAuthor = rs.getString(3); String BookDetail = rs.getString(4); BigDecimal BookMoney = rs.getBigDecimal(5); a.add(new BookListDetail(BookId,BookName,BookAuthor,BookDetail,BookMoney)); } System.out.println(a); prepStmt.close(); return a; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -