| 1 27 28 package storagebin; 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 StorageBinBean implements EntityBean, StorageBinRemoteBusiness { 38 private static final String dbName = "jdbc/pointbase"; 39 private String storageBinId; 40 private String widgetId; 41 private int quantity; 42 private EntityContext context; 43 private Connection con; 44 45 public String getWidgetId() { 46 return widgetId; 47 } 48 49 public int getQuantity() { 50 return quantity; 51 } 52 53 public String ejbCreate(String storageBinId, String widgetId, int quantity) 54 throws CreateException { 55 try { 56 insertRow(storageBinId, widgetId, quantity); 57 } catch (Exception ex) { 58 throw new EJBException("ejbCreate: " + ex.getMessage()); 59 } 60 61 this.storageBinId = storageBinId; 62 this.widgetId = widgetId; 63 this.quantity = quantity; 64 65 return storageBinId; 66 } 67 68 public String ejbFindByPrimaryKey(String primaryKey) 69 throws FinderException { 70 boolean result; 71 72 try { 73 result = selectByPrimaryKey(primaryKey); 74 } catch (Exception ex) { 75 throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage()); 76 } 77 78 if (result) { 79 return primaryKey; 80 } else { 81 throw new ObjectNotFoundException("Row for id " + primaryKey + 82 " not found."); 83 } 84 } 85 86 public String ejbFindByWidgetId(String widgetId) throws FinderException { 87 String storageBinId; 88 89 try { 90 storageBinId = selectByWidgetId(widgetId); 91 } catch (Exception ex) { 92 throw new EJBException("ejbFindByWidgetId: " + ex.getMessage()); 93 } 94 95 return storageBinId; 96 } 97 98 public void ejbRemove() { 99 try { 100 deleteRow(storageBinId); 101 } catch (Exception ex) { 102 throw new EJBException("ejbRemove: " + ex.getMessage()); 103 } 104 } 105 106 public void setEntityContext(EntityContext context) { 107 this.context = context; 108 } 109 110 public void unsetEntityContext() { 111 } 112 113 public void ejbActivate() { 114 storageBinId = (String ) context.getPrimaryKey(); 115 } 116 117 public void ejbPassivate() { 118 storageBinId = null; 119 } 120 121 public void ejbLoad() { 122 try { 123 loadRow(); 124 } catch (Exception ex) { 125 throw new EJBException("ejbLoad: " + ex.getMessage()); 126 } 127 } 128 129 public void ejbStore() { 130 try { 131 storeRow(); 132 } catch (Exception ex) { 133 throw new EJBException("ejbLoad: " + ex.getMessage()); 134 } 135 } 136 137 public void ejbPostCreate(String storageBinId, String widgetId, int quantity) { 138 } 139 140 141 private void makeConnection() { 142 try { 143 InitialContext ic = new InitialContext(); 144 DataSource ds = (DataSource) ic.lookup(dbName); 145 146 con = ds.getConnection(); 147 } catch (Exception ex) { 148 throw new EJBException("Unable to connect to database. " + 149 ex.getMessage()); 150 } 151 } 152 153 private void releaseConnection() { 154 try { 155 con.close(); 156 } catch (SQLException ex) { 157 throw new EJBException("releaseConnection: " + ex.getMessage()); 158 } 159 } 160 161 private void insertRow(String storageBinId, String widgetId, int quantity) 162 throws SQLException { 163 makeConnection(); 164 165 String insertStatement = "insert into storagebin values ( ? , ? , ? )"; 166 PreparedStatement prepStmt = con.prepareStatement(insertStatement); 167 168 prepStmt.setString(1, storageBinId); 169 prepStmt.setString(2, widgetId); 170 prepStmt.setInt(3, quantity); 171 172 prepStmt.executeUpdate(); 173 prepStmt.close(); 174 releaseConnection(); 175 } 176 177 private void deleteRow(String storageBinId) throws SQLException { 178 makeConnection(); 179 180 String deleteStatement = 181 "delete from storagebin where storagebinid = ? "; 182 PreparedStatement prepStmt = con.prepareStatement(deleteStatement); 183 184 prepStmt.setString(1, storageBinId); 185 prepStmt.executeUpdate(); 186 prepStmt.close(); 187 releaseConnection(); 188 } 189 190 private boolean selectByPrimaryKey(String primaryKey) 191 throws SQLException { 192 makeConnection(); 193 194 String selectStatement = 195 "select storagebinid " + "from storagebin where storagebinid = ? "; 196 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 197 198 prepStmt.setString(1, primaryKey); 199 200 ResultSet rs = prepStmt.executeQuery(); 201 boolean result = rs.next(); 202 203 prepStmt.close(); 204 releaseConnection(); 205 206 return result; 207 } 208 209 private String selectByWidgetId(String widgetId) throws SQLException { 210 makeConnection(); 211 212 String storageBinId; 213 214 String selectStatement = 215 "select storagebinid " + "from storagebin where widgetid = ? "; 216 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 217 218 prepStmt.setString(1, widgetId); 219 220 ResultSet rs = prepStmt.executeQuery(); 221 222 if (rs.next()) { 223 storageBinId = rs.getString(1); 224 } else { 225 storageBinId = null; 226 } 227 228 prepStmt.close(); 229 releaseConnection(); 230 231 return storageBinId; 232 } 233 234 private void loadRow() throws SQLException { 235 makeConnection(); 236 237 String selectStatement = 238 "select widgetId, quantity " + 239 "from storagebin where storagebinid = ? "; 240 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 241 242 prepStmt.setString(1, this.storageBinId); 243 244 ResultSet rs = prepStmt.executeQuery(); 245 246 if (rs.next()) { 247 this.widgetId = rs.getString(1); 248 this.quantity = rs.getInt(2); 249 prepStmt.close(); 250 } else { 251 prepStmt.close(); 252 throw new NoSuchEntityException("Row for storageBinId " + 253 storageBinId + " not found in database."); 254 } 255 256 releaseConnection(); 257 } 258 259 private void storeRow() throws SQLException { 260 makeConnection(); 261 262 String updateStatement = 263 "update storagebin set widgetId = ? , " + "quantity = ? " + 264 "where storagebinid = ?"; 265 PreparedStatement prepStmt = con.prepareStatement(updateStatement); 266 267 prepStmt.setString(1, widgetId); 268 prepStmt.setInt(2, quantity); 269 prepStmt.setString(3, storageBinId); 270 271 int rowCount = prepStmt.executeUpdate(); 272 273 prepStmt.close(); 274 275 if (rowCount == 0) { 276 throw new EJBException("Storing row for storageBinId " + 277 storageBinId + " failed."); 278 } 279 280 releaseConnection(); 281 } 282 } 283 | Popular Tags |