⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userbean.java

📁 JSP+SQL应用开发网上书店系统,采用BMP模式开发
💻 JAVA
字号:
package ejb;import javax.ejb.*;import java.util.*;import java.sql.*;import javax.sql.*;import javax.naming.*;public class userBean implements EntityBean {  EntityContext entityContext;  java.lang.String userId;  java.lang.String userName;  java.lang.String password;  DataSource ds; // private Connection con;  private String dbName = "java:/MSSQL$XZW";  public java.lang.String ejbCreate(java.lang.String userId, java.lang.String userName, java.lang.String password) throws CreateException {    try {   System.out.println("+++Calling Create userbean++3+");            insertRow(userId, userName, password);         } catch (Exception ex) {             throw new EJBException("ejbCreate: " +                ex.getMessage());         }    this.userId = userId;    this.userName = userName;    this.password = password;    setUserName(userName);    setPassword(password);    return userId;  }  public void ejbPostCreate(java.lang.String userId, java.lang.String userName, java.lang.String password) throws CreateException {    /**@todo Complete this method*/  }  public void ejbRemove() throws RemoveException {    try {         deleteRow(userId);       } catch (Exception ex) {           throw new EJBException("ejbRemove: " +              ex.getMessage());       }  }  public java.lang.String getUserId() {    return userId;  }  public java.lang.String getUserName() {    return userName;  }  public java.lang.String getPassword() {    return password;  }  public java.lang.String ejbFindByPrimaryKey(java.lang.String userId) throws FinderException {    boolean result;     try {        result = selectByPrimaryKey(userId);      } catch (Exception ex) {          throw new EJBException("ejbFindByPrimaryKey: " +             ex.getMessage());      }     if (result) {        return userId;     }     else {        throw new ObjectNotFoundException           ("Row for id " + userId + " not found.");     }  } public Collection ejbFindByuserName(String userName) throws FinderException {  Collection result;   try {      System.out.println("+++ejbFindByusername +++"+userName);      this.userName=userName;      result = selectByuserName(userName);    } catch (Exception ex) {        throw new EJBException("ejbFindByuserName " +           ex.getMessage());    }          //Iterator i=result.iterator(); System.out.println("+++ejbFindByusername +++"+result);    return result;}  public java.lang.String ejbFindMethod1() throws FinderException {    /**@todo Complete this method*/    return null;  }  public void ejbLoad() {    try {         loadRow();       } catch (Exception ex) {           throw new EJBException("ejbLoad: " +              ex.getMessage());       }  }  public void ejbStore() {    try {System.out.println("+++Store userId+++"+userId);          storeRow();        } catch (Exception ex) {           System.out.print("oh no" + ex.getMessage());        }  }  public void ejbActivate() {   userId  = (String)entityContext.getPrimaryKey();  }  public void ejbPassivate() {     userId = null;  }  public void unsetEntityContext() {      /*try {         con.close();      } catch (SQLException ex) {          throw new EJBException("unsetEntityContext: " + ex.getMessage());      }*/  }  public void setEntityContext(EntityContext entityContext) {    this.entityContext = entityContext;    try {      makeConnection();   } catch (Exception ex) {       throw new EJBException("Unable to connect to database. " +          ex.getMessage());   }  }  public void setUserName(java.lang.String userName) {   this.userName= userName;  }  public void setPassword(java.lang.String password) {    this.password = password;    System.out.print(this.password);  }  /*********************** Database Routines *************************/     private void makeConnection() throws NamingException, SQLException {        InitialContext ic = new InitialContext();        ds = (DataSource) ic.lookup(dbName);        //con =  ds.getConnection();     }     private boolean selectByPrimaryKey(String userId) throws SQLException {     System.out.println("==selectByPrimaryKey===="+userId);     Connection con = ds.getConnection();        String selectStatement =              "select userId " +              "from usertable where userId = ? ";        PreparedStatement prepStmt =              con.prepareStatement(selectStatement);        prepStmt.setString(1, userId);        ResultSet rs = prepStmt.executeQuery();        boolean result = rs.next();        prepStmt.close();        con.close();        return result;     }     private Collection selectByuserName(String userName) throws SQLException {     System.out.println("==__===="+userName);     Connection con = ds.getConnection();     String selectStatement =         "select userId " +         "from usertable where userName = ? ";    PreparedStatement prepStmt =         con.prepareStatement(selectStatement);   prepStmt.setString(1, userName);   ResultSet rs = prepStmt.executeQuery();   ArrayList a = new ArrayList();   while (rs.next()) {      String userId = rs.getString(1);       System.out.println("===userId==="+userId);      a.add(userId);   }   prepStmt.close();   return a;} private void insertRow (java.lang.String userId, java.lang.String userName, java.lang.String password) throws SQLException {System.out.println("+++insertRow+++");          String insertStatement =               "insert into usertable values ( ? , ? , ? , ? , ?)";          Connection con = ds.getConnection();          PreparedStatement prepStmt =                con.prepareStatement(insertStatement);          prepStmt.setString(1, userId);          prepStmt.setString(2, userName);          prepStmt.setString(3, password);          System.out.println("+++insertRow++5+"+userId+userName+password);          prepStmt.executeUpdate();          System.out.println("+++insertRow++5+"+userName);          prepStmt.close();          System.out.println("+++userId++5+"+userName);          con.close();   } private void loadRow() throws SQLException {     System.out.println("+++loadRow+++"+userId);     Connection con = ds.getConnection();     String selectStatement =           "select userName, password " +           "from usertable where userId = ? ";     PreparedStatement prepStmt =           con.prepareStatement(selectStatement);     prepStmt.setString(1, this.userId);     ResultSet rs = prepStmt.executeQuery();     if (rs.next()) {        this.userName = rs.getString(1);        this.password = rs.getString(2);        prepStmt.close();        con.close();     }     else {        prepStmt.close();        throw new NoSuchEntityException("Row for userId " + userId +           " not found in database.");     }  }  private void deleteRow(String userId) throws SQLException {   System.out.println("+++deleteRow+++");   Connection con = ds.getConnection();     String deleteStatement =           "delete from usertable where userId = ? ";     PreparedStatement prepStmt =     con.prepareStatement(deleteStatement);     prepStmt.setString(1, userId);     prepStmt.executeUpdate();     prepStmt.close();     con.close();  } private void storeRow() throws SQLException {    System.out.println("+++storeRow++5+"+userName);    Connection con = ds.getConnection();    String updateStatement =          "update usertable set userName =  ? ," +          "password = ?  " +          "where userId = ?";    PreparedStatement prepStmt =          con.prepareStatement(updateStatement);    System.out.println("+++storeRow+++"+this.password);    prepStmt.setString(1, userName);    prepStmt.setString(2, password);    prepStmt.setString(3, userId);    int rowCount = prepStmt.executeUpdate();    prepStmt.close();    con.close();    if (rowCount == 0) {       throw new EJBException("Storing row for userId " + userId + " failed.");    } }  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -