# BMPItemBean.ejb Sample

This topic inludes the source code for the BMPItemBean.ejb Sample.

## Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/EJBs/beanManagedPersistence/

## Sample Source Code

`001 package beanManagedPersistence; 002 003 import java.sql.Connection; 004 import java.sql.PreparedStatement; 005 import java.sql.ResultSet; 006 import java.sql.SQLException; 007 import java.util.ArrayList; 008 import java.util.Collection; 009 import javax.ejb.*; 010 import javax.naming.Context; 011 import javax.naming.InitialContext; 012 import javax.naming.NamingException; 013 import javax.sql.DataSource; 014 import weblogic.ejb.*; 015 016 /** 017 * @ejbgen:entity default-transaction="Supports" 018 * ejb-name = "BMPItem" 019 * persistence-type="bmp" 020 * prim-key-class="java.lang.Integer" 021 * 022 * @ejbgen:jndi-name 023 * local = "ejb.BMPItemLocalHome" 024 * 025 * @ejbgen:file-generation local-class = "True" local-class-name = "BMPItem" local-home = "True" local-home-name = "BMPItemHome" remote-class = "False" remote-home = "False" remote-home-name = "BMPItemRemoteHome" remote-class-name = "BMPItemRemote" value-class = "False" value-class-name = "BMPItemValue" pk-class = "True" 026 * 027 * @ejbgen:resource-ref jndi-name="cgSampleDataSource" sharing-scope="Shareable" auth="Container" type="javax.sql.DataSource" name="jdbc/cgSampleDataSource" 028 */ 029 public class BMPItemBean 030 extends GenericEntityBean 031 implements EntityBean 032 { 033 private Integer itemNumber; 034 private String itemName; 035 private double price; 036 private EntityContext theContext; 037 038 public Integer ejbCreate(Integer number, String name, double pr) 039 throws CreateException { 040 041 itemNumber = number; 042 itemName = name; 043 price = pr; 044 045 Connection con = null; 046 PreparedStatement ps = null; 047 try { 048 Context ic = new InitialContext(); 049 DataSource ds = (DataSource)ic.lookup("java:comp/env/jdbc/cgSampleDataSource"); 050 con = ds.getConnection(); 051 ps = con.prepareStatement("INSERT INTO WebLogic.EJB_BMPItem (number, name, price) values (?,?,?)"); 052 ps.setInt(1, number.intValue()); 053 ps.setString(2, name); 054 ps.setDouble(3, pr); 055 if (ps.executeUpdate() != 1) { 056 throw new CreateException ("Failure to add item the database"); 057 } 058 return number; 059 } 060 catch(NamingException ne) { 061 throw new EJBException (ne); 062 } 063 catch(SQLException se) { 064 throw new EJBException (se); 065 } 066 finally { 067 try { 068 if(ps != null) 069 ps.close(); 070 if(con!= null) 071 con.close(); 072 } 073 catch(SQLException se) { 074 se.printStackTrace(); 075 } 076 } 077 } 078 079 public void ejbPostCreate(Integer number, String name, double pr) { } 080 081 public Integer ejbFindByPrimaryKey(Integer pk) throws FinderException { 082 Connection con = null; 083 PreparedStatement ps = null; 084 ResultSet result = null; 085 try { 086 Context ic = new InitialContext(); 087 DataSource ds = (DataSource)ic.lookup("java:comp/env/jdbc/cgSampleDataSource"); 088 con = ds.getConnection(); 089 ps = con.prepareStatement("SELECT number FROM EJB_BMPItem WHERE number = ?"); 090 ps.setInt(1, pk.intValue()); 091 result = ps.executeQuery(); 092 if(!result.next()) { 093 throw new ObjectNotFoundException("Cannot find item with number " + pk); 094 } 095 } 096 catch(NamingException ne) { 097 throw new EJBException (ne); 098 } 099 catch (SQLException se) { 100 throw new EJBException(se); 101 } 102 finally { 103 try { 104 if(result != null) 105 result.close(); 106 if(ps != null) 107 ps.close(); 108 if(con!= null) 109 con.close(); 110 } 111 catch(SQLException se){ 112 se.printStackTrace(); 113 } 114 } 115 return pk; 116 } 117 118 /** 119 * @ejbgen:local-home-method 120 */ 121 public Collection ejbFindByPrice(double pr) throws FinderException { 122 Connection con = null; 123 PreparedStatement ps = null; 124 ResultSet result = null; 125 try { 126 Context ic = new InitialContext(); 127 DataSource ds = (DataSource)ic.lookup("java:comp/env/jdbc/cgSampleDataSource"); 128 con = ds.getConnection(); 129 ps = con.prepareStatement("SELECT number from EJB_BMPItem WHERE name = ?"); 130 ps.setDouble(1,pr); 131 result = ps.executeQuery(); 132 ArrayList aList = new ArrayList(); 133 while(result.next()) { 134 aList.add(result.getObject("number")); 135 } 136 return aList; 137 } 138 catch(NamingException ne) { 139 throw new EJBException (ne); 140 } 141 catch (SQLException se) { 142 throw new EJBException (se); 143 } 144 finally { 145 try { 146 if(result != null) 147 result.close(); 148 if(ps != null) 149 ps.close(); 150 if(con!= null) 151 con.close(); 152 } 153 catch(SQLException se) { 154 se.printStackTrace(); 155 } 156 } 157 } 158 159 /** 160 * @ejbgen:local-method 161 */ 162 public Integer getItemNumber() { 163 return itemNumber; 164 } 165 /** 166 * @ejbgen:local-method 167 */ 168 public void setItemNumber(Integer number) { 169 itemNumber = number; 170 } 171 172 /** 173 * @ejbgen:local-method 174 */ 175 public String getItemName() { 176 return itemName; 177 } 178 /** 179 * @ejbgen:local-method 180 */ 181 public void setItemName(String name) { 182 itemName = name; 183 } 184 185 /** 186 * @ejbgen:local-method 187 */ 188 public double getPrice() { 189 return price; 190 } 191 /** 192 * @ejbgen:local-method 193 */ 194 public void setPrice(double pr) { 195 price = pr; 196 } 197 198 public void setEntityContext(EntityContext ctx) { 199 theContext = ctx; 200 } 201 202 public void unsetEntityContext() { 203 theContext = null; 204 } 205 206 public void ejbLoad() { 207 Integer thePK = (Integer)theContext.getPrimaryKey(); 208 Connection con = null; 209 PreparedStatement ps = null; 210 ResultSet result = null; 211 try { 212 Context ic = new InitialContext(); 213 DataSource ds = (DataSource)ic.lookup("java:comp/env/jdbc/cgSampleDataSource"); 214 con = ds.getConnection(); 215 ps = con.prepareStatement("SELECT name, price FROM EJB_BMPItem WHERE number = ?"); 216 ps.setInt(1, thePK.intValue()); 217 result = ps.executeQuery(); 218 if(result.next()){ 219 itemNumber = thePK; 220 itemName = result.getString("name"); 221 price = result.getDouble("price"); 222 } 223 else { 224 throw new EJBException(); 225 } 226 } 227 catch(NamingException ne) { 228 throw new EJBException (ne); 229 } 230 catch(SQLException se) { 231 throw new EJBException(se); 232 } 233 finally { 234 try { 235 if (result != null) 236 result.close(); 237 if (ps != null) 238 ps.close(); 239 if (con!= null) 240 con.close(); 241 } 242 catch(SQLException se) { 243 se.printStackTrace(); 244 } 245 } 246 } 247 248 public void ejbStore() { 249 Connection con = null; 250 PreparedStatement ps = null; 251 try { 252 Context ic = new InitialContext(); 253 DataSource ds = (DataSource)ic.lookup("java:comp/env/jdbc/cgSampleDataSource"); 254 con = ds.getConnection(); 255 ps = con.prepareStatement("UPDATE EJB_BMPItem SET name = ?, price = ? WHERE number = ?"); 256 ps.setString(1,itemName); 257 ps.setDouble(2,price); 258 ps.setInt(3,itemNumber.intValue()); 259 if(ps.executeUpdate() != 1) { 260 throw new EJBException("ejbStore unable to store latest settings in table."); 261 } 262 } 263 catch(NamingException ne) { 264 throw new EJBException (ne); 265 } 266 catch(SQLException se) { 267 throw new EJBException(se); 268 } 269 finally { 270 try { 271 if(ps != null) 272 ps.close(); 273 if(con!= null) 274 con.close(); 275 } 276 catch(SQLException se) { 277 se.printStackTrace(); 278 } 279 } 280 } 281 282 public void ejbRemove() { 283 Connection con = null; 284 PreparedStatement ps = null; 285 try { 286 Context ic = new InitialContext(); 287 DataSource ds = (DataSource)ic.lookup("java:comp/env/jdbc/cgSampleDataSource"); 288 con = ds.getConnection(); 289 ps = con.prepareStatement("DELETE FROM EJB_BMPItem WHERE number = ?"); 290 ps.setInt(1, itemNumber.intValue()); 291 if(ps.executeUpdate() != 1) { 292 throw new EJBException("ejbRemove unable to remove the entity bean"); 293 } 294 } 295 catch(NamingException ne) { 296 throw new EJBException (ne); 297 } 298 catch(SQLException se) { 299 throw new EJBException (se); 300 } 301 finally { 302 try { 303 if(ps != null) 304 ps.close(); 305 if(con!= null) 306 con.close(); 307 } 308 catch(SQLException se) { 309 se.printStackTrace(); 310 } 311 } 312 } 313 314 } 315`