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