StudentBmpBean.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:17k
源码类别:
Java编程
开发平台:
Java
- 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 *****nn");
- 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);
- pstmt.setString(3, firstName);
- pstmt.setString(4, lastName);
- pstmt.setString(5, email);
- pstmt.setString(6, phone);
- pstmt.setString(7, companyName);
- //pstmt.setDate(8, now);
- System.out.println(" prepared statment OK");
- pstmt.executeUpdate();
- System.out.println(" Student inserted");
- }
- catch (SQLException se) {
- throw new StudentDAOException(" Query exception " + se.getMessage());
- }
- finally {
- closeStatement(pstmt);
- }
- System.out.println(" - inserted successfully");
- }
- public void updateStudent(String pKey, String password, String firstName, String lastName, String email, String phone, String companyName) throws StudentDAOException {
- System.out.println("--->>> - updateStudent() key =" + pKey +
- ", password=" + password + ", last=" + lastName +
- ", first=" + firstName + ", email=" + email + ", phone=" +
- phone + ", company=" + companyName + "nn");
- PreparedStatement pstmt = null;
- try {
- String updateStatement = "UPDATE StudentEJBTable set password = ?, firstName = ?, lastName = ?, email = ?, phone = ?, companyName = ? WHERE studentID = ?";
- System.out.println(" Executing Update Statement ");
- pstmt = conn.prepareStatement(updateStatement);
- pstmt.setString(1, password);
- pstmt.setString(2, firstName);
- pstmt.setString(3, lastName);
- pstmt.setString(4, email);
- pstmt.setString(5, phone);
- pstmt.setString(6, companyName);
- pstmt.setString(7, pKey);
- int rowCount = pstmt.executeUpdate();
- if (rowCount == 0) {
- throw new StudentDAOException("Update Failed for Student primary key =" +
- pKey);
- }
- else {
- System.out.println(" - Updated successfully");
- }
- }
- catch (SQLException se) {
- throw new StudentDAOException(
- " SQL exception while attempting to UPDATE =" + se.getMessage());
- }
- finally {
- closeStatement(pstmt);
- }
- }
- public ResultSet selectByPrimaryKey(String pKey) throws StudentDAOException {
- System.out.println("---->>> - selectByPrimaryKey pkey =" + pKey);
- //PreparedStatement pstmt = null;
- Statement pstmt=null;
- ResultSet rs = null;
- System.out.println(
- "n - in selectPrimaryKey() before select statement ********");
- try {
- if(conn==null){
- JNDIlookUp();
- }
- //pstmt = conn.prepareStatement(selectStatement);
- pstmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
- String selectStatement =
- "SELECT * from StudentEJBTable where studentID ='"+pKey+"'";
- // pstmt.setString(1, pKey);
- rs = pstmt.executeQuery(selectStatement);
- System.out.println(
- "n - in selectPrimaryKey() successful ");
- }
- catch (SQLException se) {
- throw new StudentDAOException(
- " SQL exception while attempting to SELECT By Primary Key =" +
- se.getMessage());
- }
- finally {
- // closeStatement(pstmt);
- }
- return rs;
- }
- public void deleteStudent(String pKey) throws StudentDAOException {
- System.out.println("---->>> - delete() pkey =" + pKey);
- PreparedStatement pstmt = null;
- try {
- String updateStatement =
- "DELETE FROM StudentEJBTable WHERE studentID = ?";
- pstmt = conn.prepareStatement(updateStatement);
- pstmt.setString(1, pKey);
- int rowCount = pstmt.executeUpdate();
- if (rowCount == 0) {
- throw new StudentDAOException("DELETE Failed for Student studentID =" +
- pKey);
- }
- }
- catch (SQLException se) {
- throw new StudentDAOException(
- " SQL exception while attempting to DELETE =" + se.getMessage());
- }
- finally {
- closeStatement(pstmt);
- }
- }
- public void closeStatement(PreparedStatement pstmt) throws StudentDAOException {
- System.out.println("n ==== - closeStatement() ********");
- try {
- if (pstmt != null) {
- pstmt.close();
- }
- }
- catch (SQLException se) {
- throw new StudentDAOException(
- " SQL exception while attempting to close statement =" +
- se.getMessage());
- }
- }
- private void loadStudent() throws StudentDAOException {
- System.out.println("n*** In StudentEJB -- loadStudent() called *****");
- ResultSet rs = null;
- primaryKey = (String) entityContext.getPrimaryKey();
- String returnedPK = null;
- try {
- rs = selectByPrimaryKey(primaryKey);
- rs.last();
- int size = rs.getRow();
- System.out.println("已成功完成查询");
- rs.beforeFirst();
- System.out.println(" ---> in loadStudent after selectByPrimry() -----");
- if (size == 1) {
- if (rs.next()) {
- returnedPK = rs.getString(1);
- password = rs.getString(2);
- firstName = rs.getString(3);
- lastName = rs.getString(4);
- email = rs.getString(5);
- phone = rs.getString(6);
- companyName = rs.getString(7);
- //createDate = rs.getDate(8);
- rs.close();
- }
- }
- else {
- System.out.println(" ejbLoad() error size =" + size);
- }
- // rs.close();
- }
- catch (Exception e) {
- throw new EJBException("ejbStore -- UPDATE =" + e.getMessage());
- }
- System.out.println("--- after loadStudent() primary Key=" + primaryKey +
- ", password=" + password + ", first=" + firstName +
- ", last=" + lastName + ", email=" + email + ", phone=" +
- phone + ", company=" + companyName);
- System.out.println("n*** In StudentEJB -- loadStudent() exiting *****");
- primaryKey = returnedPK;
- }
- private Collection selectByCompanyName(String cName) throws StudentDAOException, SQLException {
- System.out.println(
- "n*** In StudentEJB -- selectByComanyName() companyName =" + cName +
- " *****");
- String selectStatement =
- "SELECT studentID FROM StudentEJBTable WHERE companyName = ? ";
- PreparedStatement pstmt = conn.prepareStatement(selectStatement);
- pstmt.setString(1, cName);
- ResultSet rs = pstmt.executeQuery();
- ArrayList aList = new ArrayList();
- while (rs.next()) {
- String studentID = rs.getString(1);
- aList.add(studentID);
- System.out.println(" selectByCompanyName added to ArrayList =" +
- studentID);
- }
- pstmt.close();
- return aList;
- }
- public void getRegisteredClasses() {
- System.out.println(
- "*** In StudentEJB -- getRegisteredClasses() called *****");
- }
- public void addASchedule(String scheduleID) throws RosterDAOException {
- System.out.println("*** In StudentEJB -- addASchedule() called *****");
- try {
- //insert studentID and scheduleID
- rosterDAO.insert(scheduleID, studentID);
- }
- catch (RosterDAOException re) {
- System.out.println(" StudentEJB - addASchedule() error =" + re.getMessage());
- }
- catch (Exception e) {
- System.out.println(" StudentEJB - addASchedule() error =" + e.getMessage());
- }
- }
- public void deleteASchedule(String scheduleID) throws RosterDAOException {
- System.out.println("*** In StudentEJB -- deleteASchedule() called *****");
- try {
- //delete scheduleID
- rosterDAO.delete(scheduleID, studentID);
- }
- catch (RosterDAOException re) {
- System.out.println(" StudentEJB - addASchedule() error =" + re.getMessage());
- }
- catch (Exception e) {
- System.out.println(" StudentEJB - addASchedule() error =" + e.getMessage());
- }
- }
- public Vector getScheduleList() throws RosterDAOException {
- System.out.println("*** In StudentEJB -- getScheduleList() called *****");
- Vector classList = new Vector(20);
- try {
- //get list of scheduleID
- classList = rosterDAO.getClassList(studentID);
- }
- catch (RosterDAOException re) {
- System.out.println(" StudentEJB - addASchedule() error =" + re.getMessage());
- }
- catch (Exception e) {
- System.out.println(" StudentEJB - addASchedule() error =" + e.getMessage());
- }
- return classList;
- }
- }