studentbmpbean.java

来自「100多M的J2EE培训内容」· Java 代码 · 共 516 行 · 第 1/2 页

JAVA
516
字号
package bmpsample;

import javax.ejb.*;
import java.sql.*;
import java.util.*;

public class StudentBmpBean implements EntityBean {
  EntityContext entityContext;
  java.lang.String untitledField1;
  java.lang.String studentID;
  java.lang.String firstName;
  java.lang.String lastName;
  java.lang.String email;
  java.lang.String phone;
  java.lang.String password;
  java.lang.String companyName;
  java.lang.String primaryKey;

  private Connection conn = null;
  //private DataSource dataSource = null;
  RosterDAO rosterDAO = null;

  /*public java.lang.String ejbCreate(java.lang.String untitledField1) throws CreateException {
    setUntitledField1(untitledField1);
    return null;
  }*/
  public java.lang.String ejbCreate(String key, String pw, String firstName, String lastName, String email, String phone, String company) throws CreateException {
    System.out.println("*** In StudentEJB -- ejbCreate(args..)  called *****");
    this.primaryKey = key;
    this.password = pw;
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.phone = phone;
    this.companyName = company;
    try {
      System.out.println("*** In StudentEJB -- insertStudent() called *****");
      insertStudent(this.primaryKey, this.password, this.firstName, this.lastName, this.email, this.phone,
                    this.companyName);
    }
    catch (Exception e) {
      System.out.println(" lookUp() error =" + e.getMessage());
    }
    studentID = primaryKey;
    return primaryKey;
  }
  //public void ejbPostCreate(java.lang.String untitledField1) throws CreateException {
    /**@todo Complete this method*/
  //}
  public void ejbPostCreate(String key, String pw, String firstName, String lastName, String email, String phone, String company) throws CreateException {
    System.out.println(
        "*** In StudentEJB -- ejbPostCreate(args..)  called *****");
  }
  public void ejbRemove() throws RemoveException {
    String pKey = (String) entityContext.getPrimaryKey();

    try {
      deleteStudent(pKey);
    }
    catch (Exception e) {
      throw new EJBException("ejbRemove =" + e.getMessage());
    }
    System.out.println("\n*** In StudentEJB -- ejbRemove() exiting *****");
  }
  public void setFirstName(java.lang.String firstName) {
    this.firstName = firstName;
  }
  public void setLastName(java.lang.String lastName) {
    this.lastName = lastName;
  }
  public void setEmail(java.lang.String email) {
    this.email = email;
  }
  public void setPhone(java.lang.String phone) {
    this.phone = phone;
  }
  public void setPassword(java.lang.String password) {
    this.password = password;
  }
  public void setCompanyName(java.lang.String companyName) {
    this.companyName = companyName;
  }
  public void setPrimaryKey(java.lang.String primaryKey) {
    this.primaryKey = primaryKey;
  }

  public java.lang.String getFirstName() {
    return firstName;
  }
  public java.lang.String getLastName() {
    return lastName;
  }
  public java.lang.String getEmail() {
    return email;
  }
  public java.lang.String getPhone() {
    return phone;
  }
  public java.lang.String getPassword() {
    return password;
  }
  public java.lang.String getCompanyName() {
    return companyName;
  }
  public java.lang.String getPrimaryKey() {
    return primaryKey;
  }

  public java.lang.String ejbFindByPrimaryKey(java.lang.String primaryKey) throws FinderException {
    System.out.println(
        "*** In StudentEJB -- ejbFindByPrimaryKey() called *****");
    ResultSet rs = null;

    // search the database for the primary key.
    try {
      rs = selectByPrimaryKey(primaryKey);
      rs.last();
      int count = rs.getRow();
      System.out.println("已成功完成查询");
      rs.beforeFirst();
      if (count == 0) {
        throw new NoSuchEntityException(" Primary Key =" + primaryKey +
                                        " not found in the database!");
      }
      else if (count == 1) {
        if (rs.next()) {
          primaryKey = rs.getString(1);
        }
      }
      else if (count > 1) {
        throw new FinderException(" Duplicate Primary Key +" + primaryKey);
      }
      rs.close();
    }
    catch (SQLException se) {
      throw new EJBException("ejbFindByPrimaryKey  SQL error =" + se.getMessage());
    }
    catch (Exception e) {
      throw new EJBException("ejbFindByPrimaryKey error =" + e.getMessage());
    }
    studentID = primaryKey;
    return primaryKey;
  }
  public void ejbLoad() {
    System.out.println("\n*** In StudentEJB -- ejbLoad() called *****");
    ResultSet rs = null;
    primaryKey = (String) entityContext.getPrimaryKey();
    String returnedPK = null;
    try {
      loadStudent();
    }
    catch (Exception e) {
      throw new EJBException("ejbStore -- UPDATE =" + e.getMessage());
    }
    System.out.println("\n*** In StudentEJB -- ejbLoad() exiting *****");
  }
  public void ejbStore() {
    System.out.println("*** In StudentEJB -- ejbStore() called *****\n\n");
    System.out.println("\nIn ejbStore() primary Key=" + primaryKey +
                       ", password=" + password + ", first=" + firstName +
                       ", last=" + lastName + ", email=" + email + ", phone=" +
                       phone + ", company=" + companyName);

    try {
      updateStudent(primaryKey, password, firstName, lastName, email, phone,
                    companyName);
    }
    catch (Exception e) {
      throw new EJBException(" <<--- ejbStore -- UPDATE =" + e.getMessage());
    }
  }
  public void ejbActivate() {
    System.out.println("*** In StudentEJB -- ejbActivate() called **");
    primaryKey = (String) entityContext.getPrimaryKey();
  }
  public void ejbPassivate() {
    System.out.println("*** In StudentEJB -- ejbPassivate() called **");
    primaryKey = null;
  }
  public void unsetEntityContext() {
    this.entityContext = null;
  }
  public void setEntityContext(EntityContext entityContext) {
    this.entityContext = entityContext;
    try {
      rosterDAO = new RosterDAO();
      JNDIlookUp();
    }
    catch (Exception e) {
      System.out.println("Error in setEntityContext() -  =" + e.getMessage());
    }
  }
  public java.util.Collection ejbFindByCompanyName(String key) throws FinderException {
    ArrayList list = null;
    System.out.println(
        "\n*** In StudentEJB -- ejbFindByCompanyName() called *****");
    try {
      list = (ArrayList) selectByCompanyName(key);
    }
    catch (SQLException se) {
      throw new FinderException(" SQL exception " + se.getMessage());
    }
    catch (StudentDAOException daoe) {
      throw new FinderException("StudentDAOException =" + daoe.getMessage());
    }
    catch (Exception e) {
      throw new FinderException("Exception " + e.getMessage());
    }
    System.out.println(
        "\n*** In StudentEJB -- ejbFindByCompanyName() returning to client *****");
    return list;
  }

//*******************************************************************************
   private void JNDIlookUp() throws StudentDAOException {
     try {
       System.out.println(
           "\n====== In StudentEJB-- lookUp() before initialcontext =====");

       //InitialContext ictx = new InitialContext();
       System.out.println(
           "********* In StudentEJB -- lookUp() after initialcontext *****");

       //dataSource = (DataSource) ictx.lookup("java:comp/env/jdbc/JCampDS");
//           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
//           conn=DriverManager.getConnection("jdbc:odbc:CWJ","sa","sa");
      conn = DriverManager.getConnection(
          "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=RegSystem;User=sa;Password=sa");
       //conn = dataSource.getConnection();

       System.out.println(
           "== In StudentEJB got connection -- jcampDataSource lookup OK!");

     }
     catch (Exception se) {
       throw new StudentDAOException(
           " SQL exception while attempting to OPEN connection =" +
           se.getMessage());
     }

   }

  public void insertStudent(String pKey, String password, String firstName, String lastName, String email, String phone, String companyName) throws StudentDAOException {

    System.out.println("----->>>   - insertStudent() ");

    PreparedStatement pstmt = null;

    /*Calendar calendar = Calendar.getInstance();
    java.util.Date theTime = calendar.getTime();
    java.sql.Date now = new java.sql.Date(theTime.getTime());*/

    try {
      pstmt = conn.prepareStatement("Insert into StudentEJBTable(studentID, password, firstName, lastName, email, phone, companyName) values(?,?,?,?,?,?,?)");

      pstmt.setString(1, pKey);
      pstmt.setString(2, password);

⌨️ 快捷键说明

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