1 27 28 package enroller; 29 30 import java.rmi.RemoteException ; 31 import javax.ejb.*; 32 import java.sql.*; 33 import javax.sql.*; 34 import java.util.*; 35 import javax.naming.*; 36 37 38 public class EnrollerBean implements SessionBean, EnrollerRemoteBusiness { 39 private static final String dbName = "jdbc/pointbase"; 40 private Connection con; 41 private SessionContext context; 42 43 public EnrollerBean() { 44 } 45 46 public void enroll(String studentId, String courseId) { 47 try { 48 insertEntry(studentId, courseId); 49 } catch (Exception ex) { 50 throw new EJBException("enroll: " + ex.getMessage()); 51 } 52 } 53 54 public void unEnroll(String studentId, String courseId) { 55 try { 56 deleteEntry(studentId, courseId); 57 } catch (Exception ex) { 58 throw new EJBException("unEnroll: " + ex.getMessage()); 59 } 60 } 61 62 public void deleteStudent(String studentId) { 63 try { 64 deleteStudentEntries(studentId); 65 } catch (Exception ex) { 66 throw new EJBException("deleteStudent: " + ex.getMessage()); 67 } 68 } 69 70 public void deleteCourse(String courseId) { 71 try { 72 deleteCourseEntries(courseId); 73 } catch (Exception ex) { 74 throw new EJBException("deleteCourse: " + ex.getMessage()); 75 } 76 } 77 78 public ArrayList getStudentIds(String courseId) { 79 try { 80 return selectStudent(courseId); 81 } catch (Exception ex) { 82 throw new EJBException("getStudentIds: " + ex.getMessage()); 83 } 84 } 85 86 public ArrayList getCourseIds(String studentId) { 87 try { 88 return selectCourse(studentId); 89 } catch (Exception ex) { 90 throw new EJBException("getCourseIds: " + ex.getMessage()); 91 } 92 } 93 94 public void ejbCreate() { 95 } 96 97 public void ejbRemove() { 98 } 99 100 public void ejbActivate() { 101 } 102 103 public void ejbPassivate() { 104 } 105 106 public void setSessionContext(SessionContext context) { 107 this.context = context; 108 } 109 110 111 private void makeConnection() { 112 try { 113 InitialContext ic = new InitialContext(); 114 DataSource ds = (DataSource) ic.lookup(dbName); 115 116 con = ds.getConnection(); 117 } catch (Exception ex) { 118 throw new EJBException("Unable to connect to database. " + 119 ex.getMessage()); 120 } 121 } 122 123 private void releaseConnection() { 124 try { 125 con.close(); 126 } catch (SQLException ex) { 127 throw new EJBException("releaseConnection: " + ex.getMessage()); 128 } 129 } 130 131 private void insertEntry(String studentId, String courseId) 132 throws SQLException { 133 makeConnection(); 134 135 String insertStatement = "insert into enrollment values ( ? , ? )"; 136 PreparedStatement prepStmt = con.prepareStatement(insertStatement); 137 138 prepStmt.setString(1, studentId); 139 prepStmt.setString(2, courseId); 140 141 prepStmt.executeUpdate(); 142 prepStmt.close(); 143 releaseConnection(); 144 } 145 146 private void deleteEntry(String studentId, String courseId) 147 throws SQLException { 148 makeConnection(); 149 150 String deleteStatement = 151 "delete from enrollment " + "where studentid = ? and courseid = ?"; 152 PreparedStatement prepStmt = con.prepareStatement(deleteStatement); 153 154 prepStmt.setString(1, studentId); 155 prepStmt.setString(2, courseId); 156 prepStmt.executeUpdate(); 157 prepStmt.close(); 158 releaseConnection(); 159 } 160 161 private void deleteStudentEntries(String studentId) 162 throws SQLException { 163 makeConnection(); 164 165 String deleteStatement = 166 "delete from enrollment " + "where studentid = ?"; 167 PreparedStatement prepStmt = con.prepareStatement(deleteStatement); 168 169 prepStmt.setString(1, studentId); 170 prepStmt.executeUpdate(); 171 prepStmt.close(); 172 releaseConnection(); 173 } 174 175 private void deleteCourseEntries(String courseId) throws SQLException { 176 makeConnection(); 177 178 String deleteStatement = 179 "delete from enrollment " + "where courseid = ?"; 180 PreparedStatement prepStmt = con.prepareStatement(deleteStatement); 181 182 prepStmt.setString(1, courseId); 183 prepStmt.executeUpdate(); 184 prepStmt.close(); 185 releaseConnection(); 186 } 187 188 private ArrayList selectStudent(String courseId) throws SQLException { 189 makeConnection(); 190 191 String selectStatement = 192 "select studentid " + "from enrollment where courseid = ? "; 193 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 194 195 prepStmt.setString(1, courseId); 196 197 ResultSet rs = prepStmt.executeQuery(); 198 ArrayList a = new ArrayList(); 199 200 while (rs.next()) { 201 String id = rs.getString(1); 202 203 a.add(id); 204 } 205 206 prepStmt.close(); 207 releaseConnection(); 208 209 return a; 210 } 211 212 private ArrayList selectCourse(String studentId) throws SQLException { 213 makeConnection(); 214 215 String selectStatement = 216 "select courseid " + "from enrollment where studentid = ? "; 217 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 218 219 prepStmt.setString(1, studentId); 220 221 ResultSet rs = prepStmt.executeQuery(); 222 ArrayList a = new ArrayList(); 223 224 while (rs.next()) { 225 String id = rs.getString(1); 226 227 a.add(id); 228 } 229 230 prepStmt.close(); 231 releaseConnection(); 232 233 return a; 234 } 235 } 236 | Popular Tags |