一个图书管理系统

源代码在线查看: userbean.java

软件大小: 344 K
上传用户: tanhua1981
关键词: 图书管理
下载地址: 免注册下载 普通下载 VIP

相关代码

				/*				 * UserBean.java				 *				 * Created on 2003年5月13日, 上午10:12				 */								/**				 *				 * @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 UserBean implements EntityBean{				    //entity variables				    private String id;				    private String userName;				    private String userPassword;				    private String userSex;				    private String userAge;				    private String userJob;				    private String userEdu;				    				    //other variables				    private EntityContext context;				    private Connection con;				    private String dbName = "java:comp/env/jdbc/book";				    				    //business method				    public String getUserName() throws RemoteException				    {				        System.out.println("In UserBean:getUserName(),userName="+userName);				        				        return userName;				    }				   				   				   public String getUserPassword() throws RemoteException				   {				       return userPassword;				   }				   				 				   public String getUserSex() throws RemoteException				   {				       return userSex;				   }				   				  				   public String getUserAge() throws RemoteException				   {				       return userAge;				   }				   				  				   public String getUserJob() throws RemoteException				   {				       return userJob;				   }				   				   public String getUserEdu() throws RemoteException				   {				       return userEdu;				   }				    				    //entity bean's method				    public void ejbActivate() throws javax.ejb.EJBException, java.rmi.RemoteException 				    {				        System.out.println("In UserBean:ejbActive()");				        				        id = (String)context.getPrimaryKey();				        				        System.out.println("End UserBean:ejbActive(),id="+id);				    }				    				    public void ejbLoad() throws javax.ejb.EJBException, java.rmi.RemoteException 				    {				        System.out.println("In UserBean:ejbLoad(),id="+id);				        				        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 				    {				        System.out.println("In UserBean:ejbStore()");				        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				    //we don't use this method for this programm				    public String ejbCreate() throws CreateException 				    {				        return "1";				    }				    				    public void ejbPostCreate() 				    {				    }				    				    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 				    {				      System.out.println("In UserBean:ejbFindByUserName(),userName="+userName);				      Collection result;								      try {				         result = selectByUserName(userName);				       } catch (Exception ex) {				           throw new EJBException("ejbFindByUserName " + 				              ex.getMessage());				       }				      System.out.println("End UserBean:ejbFindByUserName()"); 				      return result;				    }				    				    				    				    //========================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 user 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 				    {				      System.out.println("In UserBean:ejbSelectByUserName(),userName="+userName);				      				      String selectStatement =				            "select id " +				            "from user where user_name like ? ";				      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);				         				         System.out.println("In UserBean:ejbSelectByUserName(),id="+id);				         				         a.add(id);				      }								      prepStmt.close();				       				      System.out.println("End UserBean:ejbSelectByUserName()");				      return a;				    }				    				    private void loadRow() throws SQLException {				        				        System.out.println("In UserBean:loadRow()");				        				      String selectStatement =				            "select user_name,user_password,user_sex,user_age,user_job,user_edu " +				            "from user 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.userPassword = rs.getString(2);				         this.userSex = rs.getString(3);				         this.userAge = rs.getString(4);				         this.userJob = rs.getString(5);				         this.userEdu = rs.getString(6);				         				         System.out.println("In UserBean:loadRow(),userPassword="+this.userPassword);				         				         prepStmt.close();				      }				      else {				         prepStmt.close();				         throw new NoSuchEntityException("Row for id " + id +				            " not found in database.");				      }				   }												   private void storeRow() throws SQLException 				   {				      System.out.println("In UserBean:storeRow()");				      				       String updateStatement =				            "update user set user_name =  ? ," +				            "user_password = ? , user_sex = ? ," +				            "user_age = ? , user_job = ? , user_edu = ? " +				            "where id = ?";				      PreparedStatement prepStmt = 				            con.prepareStatement(updateStatement);								      prepStmt.setString(1, userName);				      prepStmt.setString(2, userPassword);				      prepStmt.setString(3, userSex);				      prepStmt.setString(4, userAge);				      prepStmt.setString(5, userJob);				      prepStmt.setString(6, userEdu);				      prepStmt.setInt(7, Integer.parseInt(this.id));				      int rowCount = prepStmt.executeUpdate();				      prepStmt.close();				      				      System.out.println("End UserBean:storeRow()");				      				      if (rowCount == 0) {				         throw new EJBException("Storing row for id " + id + " failed.");				      }				   }				    				    				    				}							

相关资源