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 WidgetBean implements EntityBean, WidgetRemoteBusiness { 38 private String widgetId; 39 private String description; 40 private double price; 41 private EntityContext context; 42 private Connection con; 43 private String dbName = "jdbc/pointbase"; 44 45 public String getDescription() { 46 return description; 47 } 48 49 public double getPrice() { 50 return price; 51 } 52 53 public String ejbCreate(String widgetId, String description, double price) 54 throws CreateException { 55 try { 56 insertRow(widgetId, description, price); 57 } catch (Exception ex) { 58 throw new EJBException("ejbCreate: " + ex.getMessage()); 59 } 60 61 this.widgetId = widgetId; 62 this.description = description; 63 this.price = price; 64 65 return widgetId; 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 void ejbRemove() { 87 try { 88 deleteRow(widgetId); 89 } catch (Exception ex) { 90 throw new EJBException("ejbRemove: " + ex.getMessage()); 91 } 92 } 93 94 public void setEntityContext(EntityContext context) { 95 this.context = context; 96 } 97 98 public void unsetEntityContext() { 99 } 100 101 public void ejbActivate() { 102 widgetId = (String ) context.getPrimaryKey(); 103 } 104 105 public void ejbPassivate() { 106 widgetId = null; 107 } 108 109 public void ejbLoad() { 110 try { 111 loadRow(); 112 } catch (Exception ex) { 113 throw new EJBException("ejbLoad: " + ex.getMessage()); 114 } 115 } 116 117 public void ejbStore() { 118 try { 119 storeRow(); 120 } catch (Exception ex) { 121 throw new EJBException("ejbLoad: " + ex.getMessage()); 122 } 123 } 124 125 public void ejbPostCreate(String widgetId, String description, double price) { 126 } 127 128 129 private void makeConnection() { 130 try { 131 InitialContext ic = new InitialContext(); 132 DataSource ds = (DataSource) ic.lookup(dbName); 133 134 con = ds.getConnection(); 135 } catch (Exception ex) { 136 throw new EJBException("Unable to connect to database. " + 137 ex.getMessage()); 138 } 139 } 140 141 private void releaseConnection() { 142 try { 143 con.close(); 144 } catch (SQLException ex) { 145 throw new EJBException("releaseConnection: " + ex.getMessage()); 146 } 147 } 148 149 private void insertRow(String widgetId, String description, double price) 150 throws SQLException { 151 makeConnection(); 152 153 String insertStatement = "insert into widget values ( ? , ? , ? )"; 154 PreparedStatement prepStmt = con.prepareStatement(insertStatement); 155 156 prepStmt.setString(1, widgetId); 157 prepStmt.setString(2, description); 158 prepStmt.setDouble(3, price); 159 160 prepStmt.executeUpdate(); 161 prepStmt.close(); 162 releaseConnection(); 163 } 164 165 private void deleteRow(String widgetId) throws SQLException { 166 makeConnection(); 167 168 String deleteStatement = "delete from widget where widgetid = ? "; 169 PreparedStatement prepStmt = con.prepareStatement(deleteStatement); 170 171 prepStmt.setString(1, widgetId); 172 prepStmt.executeUpdate(); 173 prepStmt.close(); 174 releaseConnection(); 175 } 176 177 private boolean selectByPrimaryKey(String primaryKey) 178 throws SQLException { 179 makeConnection(); 180 181 String selectStatement = 182 "select widgetid " + "from widget where widgetid = ? "; 183 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 184 185 prepStmt.setString(1, primaryKey); 186 187 ResultSet rs = prepStmt.executeQuery(); 188 boolean result = rs.next(); 189 190 prepStmt.close(); 191 releaseConnection(); 192 193 return result; 194 } 195 196 private void loadRow() throws SQLException { 197 makeConnection(); 198 199 String selectStatement = 200 "select description, price " + "from widget where widgetid = ? "; 201 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 202 203 prepStmt.setString(1, this.widgetId); 204 205 ResultSet rs = prepStmt.executeQuery(); 206 207 if (rs.next()) { 208 this.description = rs.getString(1); 209 this.price = rs.getDouble(2); 210 prepStmt.close(); 211 } else { 212 prepStmt.close(); 213 throw new NoSuchEntityException("Row for widgetId " + widgetId + 214 " not found in database."); 215 } 216 217 releaseConnection(); 218 } 219 220 private void storeRow() throws SQLException { 221 makeConnection(); 222 223 String updateStatement = 224 "update widget set description = ? , " + "price = ? " + 225 "where widgetid = ?"; 226 PreparedStatement prepStmt = con.prepareStatement(updateStatement); 227 228 prepStmt.setString(1, description); 229 prepStmt.setDouble(2, price); 230 prepStmt.setString(3, widgetId); 231 232 int rowCount = prepStmt.executeUpdate(); 233 234 prepStmt.close(); 235 236 if (rowCount == 0) { 237 throw new EJBException("Storing row for widgetId " + widgetId + 238 " failed."); 239 } 240 241 releaseConnection(); 242 } 243 } 244
| Popular Tags
|