/* * * Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. * * This software is the proprietary information of Sun Microsystems, Inc. * Use is subject to license terms. * */ import java.sql.*; import javax.sql.*; import java.util.*; import javax.ejb.*; import javax.naming.*; import javax.rmi.PortableRemoteObject; public class StudentEJB implements EntityBean { private String studentId; private String name; private ArrayList courseIds; private Connection con; private String dbName = "java:comp/env/jdbc/CollegeDB"; private EntityContext context; private EnrollerHome enrollerHome; public ArrayList getCourseIds() { System.out.println("in getCourseIds"); return courseIds; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String ejbCreate(String studentId, String name) throws CreateException { System.out.println("in ejbCreate"); try { insertStudent(studentId, name); } catch (Exception ex) { throw new EJBException("ejbCreate: " + ex.getMessage()); } this.studentId = studentId; this.name = name; System.out.println("about to leave ejbCreate"); return studentId; } 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 void ejbRemove() { try { deleteStudent(studentId); } catch (Exception ex) { throw new EJBException("ejbRemove: " + ex.getMessage()); } } public void setEntityContext(EntityContext context) { System.out.println("in setEntityContext"); this.context = context; courseIds = new ArrayList(); try { makeConnection(); Context initial = new InitialContext(); Object objref = initial.lookup("java:comp/env/ejb/Enroller"); enrollerHome = (EnrollerHome)PortableRemoteObject.narrow(objref, EnrollerHome.class); } catch (Exception ex) { throw new EJBException("setEntityContext: " + ex.getMessage()); } } public void unsetEntityContext() { try { con.close(); } catch (SQLException ex) { throw new EJBException("unsetEntityContext: " + ex.getMessage()); } } public void ejbActivate() { studentId = (String)context.getPrimaryKey(); } public void ejbPassivate() { studentId = null; } public void ejbLoad() { System.out.println("in ejbLoad"); try { loadStudent(); loadCourseIds(); } catch (Exception ex) { throw new EJBException("ejbLoad: " + ex.getMessage()); } System.out.println("leaving ejbLoad"); } private void loadCourseIds() { System.out.println("in loadCourseIds"); courseIds.clear(); System.out.println("in loadCourseIds, about to try"); try { Enroller enroller = enrollerHome.create(); ArrayList a = enroller.getCourseIds(studentId); courseIds.addAll(a); } catch (Exception ex) { throw new EJBException("Exception in loadCourseIds: " + ex.getMessage()); } System.out.println("leaving loadCourseIds"); } public void ejbStore() { System.out.println("in ejbStore"); try { storeStudent(); } catch (Exception ex) { throw new EJBException("ejbStore: " + ex.getMessage()); } System.out.println("leaving ejbStore"); } public void ejbPostCreate(String studentId, String name) { } /*********************** Database Routines *************************/ private void makeConnection() throws NamingException, SQLException { InitialContext ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup(dbName); con = ds.getConnection(); } private void insertStudent (String studentId, String name) throws SQLException { String insertStatement = "insert into student values ( ? , ? )"; PreparedStatement prepStmt = con.prepareStatement(insertStatement); prepStmt.setString(1, studentId); prepStmt.setString(2, name); prepStmt.executeUpdate(); prepStmt.close(); } private boolean selectByPrimaryKey(String primaryKey) throws SQLException { String selectStatement = "select studentid " + "from student where studentid = ? "; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, primaryKey); ResultSet rs = prepStmt.executeQuery(); boolean result = rs.next(); prepStmt.close(); return result; } private void deleteStudent(String studentId) throws SQLException { String deleteStatement = "delete from student " + "where studentid = ?"; PreparedStatement prepStmt = con.prepareStatement(deleteStatement); prepStmt.setString(1, studentId); prepStmt.executeUpdate(); prepStmt.close(); } private void loadStudent() throws SQLException { String selectStatement = "select name " + "from student where studentid = ? "; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, studentId); ResultSet rs = prepStmt.executeQuery(); if (rs.next()) { name = rs.getString(1); prepStmt.close(); } else { prepStmt.close(); throw new NoSuchEntityException("Row for studentId " + studentId + " not found in database."); } } private void storeStudent() throws SQLException { String updateStatement = "update student set name = ? " + "where studentid = ?"; PreparedStatement prepStmt = con.prepareStatement(updateStatement); prepStmt.setString(1, name); prepStmt.setString(2, studentId); int rowCount = prepStmt.executeUpdate(); prepStmt.close(); if (rowCount == 0) { throw new EJBException("Storing row for studentId " + studentId + " failed."); } } } // StudentEJB