1 27 28 package enroller; 29 30 import java.sql.*; 31 import javax.sql.*; 32 import java.util.*; 33 import javax.ejb.*; 34 import javax.naming.*; 35 import javax.rmi.PortableRemoteObject ; 36 37 38 public class StudentBean implements EntityBean, StudentRemoteBusiness { 39 private static final String dbName = "jdbc/pointbase"; 40 private String studentId; 41 private String name; 42 private ArrayList courseIds; 43 private Connection con; 44 private EntityContext context; 45 private EnrollerRemoteHome enrollerHome; 46 47 public ArrayList getCourseIds() { 48 System.out.println("in getCourseIds"); 49 50 return courseIds; 51 } 52 53 public String getName() { 54 return name; 55 } 56 57 public void setName(String name) { 58 this.name = name; 59 } 60 61 public String ejbCreate(String studentId, String name) 62 throws CreateException { 63 System.out.println("in ejbCreate"); 64 65 try { 66 insertStudent(studentId, name); 67 } catch (Exception ex) { 68 throw new EJBException("ejbCreate: " + ex.getMessage()); 69 } 70 71 this.studentId = studentId; 72 this.name = name; 73 System.out.println("about to leave ejbCreate"); 74 75 return studentId; 76 } 77 78 public String ejbFindByPrimaryKey(String primaryKey) 79 throws FinderException { 80 boolean result; 81 82 try { 83 result = selectByPrimaryKey(primaryKey); 84 } catch (Exception ex) { 85 throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage()); 86 } 87 88 if (result) { 89 return primaryKey; 90 } else { 91 throw new ObjectNotFoundException("Row for id " + primaryKey + 92 " not found."); 93 } 94 } 95 96 public void ejbRemove() { 97 try { 98 deleteStudent(studentId); 99 } catch (Exception ex) { 100 throw new EJBException("ejbRemove: " + ex.getMessage()); 101 } 102 } 103 104 public void setEntityContext(EntityContext context) { 105 System.out.println("in setEntityContext"); 106 this.context = context; 107 courseIds = new ArrayList(); 108 109 try { 110 111 enrollerHome = lookupEnrollerBean(); 112 } catch (Exception ex) { 113 throw new EJBException("setEntityContext: " + ex.getMessage()); 114 } 115 } 116 117 public void unsetEntityContext() { 118 } 119 120 public void ejbActivate() { 121 studentId = (String ) context.getPrimaryKey(); 122 } 123 124 public void ejbPassivate() { 125 studentId = null; 126 } 127 128 public void ejbLoad() { 129 System.out.println("in ejbLoad"); 130 131 try { 132 loadStudent(); 133 loadCourseIds(); 134 } catch (Exception ex) { 135 throw new EJBException("ejbLoad: " + ex.getMessage()); 136 } 137 138 System.out.println("leaving ejbLoad"); 139 } 140 141 private void loadCourseIds() { 142 System.out.println("in loadCourseIds"); 143 courseIds.clear(); 144 System.out.println("in loadCourseIds, about to try"); 145 146 try { 147 EnrollerRemote enroller = enrollerHome.create(); 148 ArrayList a = enroller.getCourseIds(studentId); 149 150 courseIds.addAll(a); 151 } catch (Exception ex) { 152 throw new EJBException("Exception in loadCourseIds: " + 153 ex.getMessage()); 154 } 155 156 System.out.println("leaving loadCourseIds"); 157 } 158 159 public void ejbStore() { 160 System.out.println("in ejbStore"); 161 162 try { 163 storeStudent(); 164 } catch (Exception ex) { 165 throw new EJBException("ejbStore: " + ex.getMessage()); 166 } 167 168 System.out.println("leaving ejbStore"); 169 } 170 171 public void ejbPostCreate(String studentId, String name) { 172 } 173 174 175 private void makeConnection() { 176 try { 177 InitialContext ic = new InitialContext(); 178 DataSource ds = (DataSource) ic.lookup(dbName); 179 180 con = ds.getConnection(); 181 } catch (Exception ex) { 182 throw new EJBException("Unable to connect to database. " + 183 ex.getMessage()); 184 } 185 } 186 187 private void releaseConnection() { 188 try { 189 con.close(); 190 } catch (SQLException ex) { 191 throw new EJBException("releaseConnection: " + ex.getMessage()); 192 } 193 } 194 195 private void insertStudent(String studentId, String name) 196 throws SQLException { 197 makeConnection(); 198 199 String insertStatement = "insert into student values ( ? , ? )"; 200 PreparedStatement prepStmt = con.prepareStatement(insertStatement); 201 202 prepStmt.setString(1, studentId); 203 prepStmt.setString(2, name); 204 205 prepStmt.executeUpdate(); 206 prepStmt.close(); 207 releaseConnection(); 208 } 209 210 private boolean selectByPrimaryKey(String primaryKey) 211 throws SQLException { 212 makeConnection(); 213 214 String selectStatement = 215 "select studentid " + "from student where studentid = ? "; 216 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 217 218 prepStmt.setString(1, primaryKey); 219 220 ResultSet rs = prepStmt.executeQuery(); 221 boolean result = rs.next(); 222 223 prepStmt.close(); 224 releaseConnection(); 225 226 return result; 227 } 228 229 private void deleteStudent(String studentId) throws SQLException { 230 makeConnection(); 231 232 String deleteStatement = 233 "delete from student " + "where studentid = ?"; 234 PreparedStatement prepStmt = con.prepareStatement(deleteStatement); 235 236 prepStmt.setString(1, studentId); 237 prepStmt.executeUpdate(); 238 prepStmt.close(); 239 releaseConnection(); 240 } 241 242 private void loadStudent() throws SQLException { 243 makeConnection(); 244 245 String selectStatement = 246 "select name " + "from student where studentid = ? "; 247 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 248 249 prepStmt.setString(1, studentId); 250 251 ResultSet rs = prepStmt.executeQuery(); 252 253 if (rs.next()) { 254 name = rs.getString(1); 255 prepStmt.close(); 256 } else { 257 prepStmt.close(); 258 throw new NoSuchEntityException("Row for studentId " + studentId + 259 " not found in database."); 260 } 261 262 releaseConnection(); 263 } 264 265 private void storeStudent() throws SQLException { 266 makeConnection(); 267 268 String updateStatement = 269 "update student set name = ? " + "where studentid = ?"; 270 PreparedStatement prepStmt = con.prepareStatement(updateStatement); 271 272 prepStmt.setString(1, name); 273 prepStmt.setString(2, studentId); 274 275 int rowCount = prepStmt.executeUpdate(); 276 277 prepStmt.close(); 278 279 if (rowCount == 0) { 280 throw new EJBException("Storing row for studentId " + studentId + 281 " failed."); 282 } 283 284 releaseConnection(); 285 } 286 287 private enroller.EnrollerRemoteHome lookupEnrollerBean() { 288 try { 289 javax.naming.Context c = new javax.naming.InitialContext (); 290 Object remote = c.lookup("ejb/SimpleEnroller"); 291 enroller.EnrollerRemoteHome rv = (enroller.EnrollerRemoteHome) javax.rmi.PortableRemoteObject.narrow(remote, enroller.EnrollerRemoteHome.class); 292 return rv; 293 } 294 catch(javax.naming.NamingException ne) { 295 java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne); 296 throw new RuntimeException (ne); 297 } 298 299 } 300 } 301 | Popular Tags |