1 25 26 package org.objectweb.jonas.jtests.beans.beanexc; 27 28 import java.sql.Connection ; 29 import java.sql.PreparedStatement ; 30 import java.sql.ResultSet ; 31 import java.sql.SQLException ; 32 import java.util.Enumeration ; 33 import java.util.Vector ; 34 35 import javax.ejb.EntityBean ; 36 import javax.ejb.FinderException ; 37 import javax.ejb.ObjectNotFoundException ; 38 import javax.ejb.RemoveException ; 39 import javax.naming.Context ; 40 import javax.naming.InitialContext ; 41 import javax.sql.DataSource ; 42 43 import org.objectweb.util.monolog.api.BasicLevel; 44 45 46 52 public class AccountEB extends AccountEC implements EntityBean { 53 54 static final String tableName = "beanexcAccountEB"; 55 56 private DataSource dataSource = null; 58 private static String dataSourceName; 59 60 61 public void ejbLoad() { 62 logger.log(BasicLevel.DEBUG, ""); 63 AccountPK pk = (AccountPK) entityContext.getPrimaryKey(); 64 PreparedStatement loadPstmt = null; 65 Connection conn = null; 66 try { 67 conn = getConnection(); 68 loadPstmt = conn.prepareStatement("select c_number, c_customer, c_balance from " + tableName + " where c_number=?"); 69 70 loadPstmt.setInt(1, pk.number); 72 ResultSet rs = loadPstmt.executeQuery(); 73 if (!rs.next()) { 74 System.err.println("Fails to load bean from database"); 75 throw new javax.ejb.EJBException ("Failed to load bean from database"); 76 } 77 number = rs.getInt("c_number"); 78 customer = rs.getString("c_customer"); 79 balance = rs.getLong("c_balance"); 80 } catch (SQLException e) { 81 System.err.println("Fails to load bean from database"); 82 throw new javax.ejb.EJBException ("Failed to load bean from database" + e); 83 } finally { 84 try { 85 loadPstmt.close(); 86 conn.close(); 87 } catch (Exception e) { 88 System.err.println("ejbLoad: Ignore exception:" + e); 89 } 90 } 91 92 } 93 94 public void ejbStore() { 95 AccountPK pk = (AccountPK) entityContext.getPrimaryKey(); 96 logger.log(BasicLevel.DEBUG, "pk=" + pk.number); 97 PreparedStatement storePstmt = null; 98 Connection conn = null; 99 if (forceToFailEjbStore) { 100 forceToFailEjbStore = false; 101 throw new javax.ejb.EJBException ("Failed to store bean to database"); 102 } 103 104 try { 105 conn = getConnection(); 106 storePstmt = conn.prepareStatement("update " + tableName + " set c_customer=?,c_balance=? where c_number=?"); 107 108 if (customer == null) { 110 storePstmt.setNull(1, java.sql.Types.VARCHAR); 111 } else { 112 storePstmt.setString(1, customer); 113 } 114 storePstmt.setLong(2, balance); 115 storePstmt.setInt(3, number); 117 storePstmt.executeUpdate(); 118 } catch (SQLException e) { 119 throw new javax.ejb.EJBException ("Failed to store bean to database"); 120 } finally { 121 try { 122 storePstmt.close(); 123 conn.close(); 124 } catch (Exception e) { 125 System.err.println("ejbStore: Ignore exception:" + e); 126 } 127 } 128 } 129 130 public void ejbRemove() throws RemoveException { 131 logger.log(BasicLevel.DEBUG, ""); 132 133 super.ejbRemove(); 135 136 AccountPK pk = (AccountPK) entityContext.getPrimaryKey(); 137 PreparedStatement removePstmt = null; 138 Connection conn = null; 139 try { 140 conn = getConnection(); 141 removePstmt = conn.prepareStatement("delete from " + tableName + " where c_number=?"); 142 removePstmt.setInt(1, pk.number); 144 removePstmt.executeUpdate(); 145 } catch (SQLException e) { 146 throw new javax.ejb.EJBException ("Failed to delete bean from database"); 147 } finally { 148 try { 149 removePstmt.close(); 150 conn.close(); 151 } catch (Exception e) { 152 System.err.println("ejbRemove: Ignore exception:" + e); 153 } 154 } 155 } 156 157 158 public AccountPK ejbCreate(int val_number, String val_customer, long val_balance) throws javax.ejb.CreateException { 159 logger.log(BasicLevel.DEBUG, ""); 160 161 super.ejbCreate(val_number, val_customer, val_balance); 162 163 PreparedStatement createPstmt = null; 164 Connection conn = null; 165 AccountPK pk = new AccountPK(val_number); 166 167 try { 169 conn = getConnection(); 170 createPstmt = conn.prepareStatement("insert into " + tableName + " values (?, ?, ?)"); 171 createPstmt.setInt(1, pk.number); 173 if (customer == null) { 174 createPstmt.setNull(2, java.sql.Types.VARCHAR); 175 } else { 176 createPstmt.setString(2, customer); 177 } 178 createPstmt.setLong(3, balance); 179 createPstmt.executeUpdate(); 180 } catch (SQLException e) { 181 throw new javax.ejb.EJBException ("Failed to create bean in database" + e); 182 } finally { 183 try { 184 createPstmt.close(); 185 conn.close(); 186 } catch (Exception e) { 187 logger.log(BasicLevel.ERROR, "ejbCreate: Ignore exception:" + e); 188 } 189 } 190 return pk; 191 } 192 193 196 public AccountPK ejbCreate(int flag) throws javax.ejb.CreateException , AppException { 197 logger.log(BasicLevel.DEBUG, ""); 198 AccountPK pk = super.ejbCreate(flag); 199 return pk; 200 } 201 202 public AccountPK ejbCreate(boolean flag) throws javax.ejb.CreateException , AppException { 203 logger.log(BasicLevel.DEBUG, ""); 204 AccountPK pk = super.ejbCreate(flag); 205 return pk; 206 } 207 208 public void ejbPostCreate(int val_number, String val_customer, long val_balance) { 209 logger.log(BasicLevel.DEBUG, ""); 210 } 211 212 public AccountPK ejbFindByPrimaryKey(AccountPK pk) throws ObjectNotFoundException , FinderException { 213 logger.log(BasicLevel.DEBUG, ""); 214 PreparedStatement loadPstmt = null; 215 Connection conn = null; 216 try { 217 conn = getConnection(); 218 loadPstmt = conn.prepareStatement("select c_customer, c_balance from " + tableName + " where c_number=?"); 219 loadPstmt.setInt(1, pk.number); 220 ResultSet rs = loadPstmt.executeQuery(); 221 if (!rs.next()) { 222 throw new javax.ejb.ObjectNotFoundException ("primary key"); 223 } 224 } catch (SQLException e) { 225 System.err.println("Fails to executeQuery " + e); 226 throw new javax.ejb.FinderException ("Failed to executeQuery " + e); 227 } finally { 228 try { 229 loadPstmt.close(); 230 conn.close(); 231 } catch (Exception e) { 232 logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:" + e); 233 } 234 } 235 return pk; 236 } 237 238 public AccountPK ejbFindByNoAccount(int number) 239 throws ObjectNotFoundException , FinderException { 240 logger.log(BasicLevel.DEBUG, ""); 241 PreparedStatement loadPstmt = null; 242 Connection conn = null; 243 AccountPK pk = new AccountPK(number); 244 try { 245 conn = getConnection(); 246 loadPstmt = conn.prepareStatement("select c_customer,c_balance from " + tableName + " where c_number=?"); 247 loadPstmt.setInt(1, pk.number); 248 ResultSet rs = loadPstmt.executeQuery(); 249 if (!rs.next()) { 250 logger.log(BasicLevel.ERROR, "Object Not Found primary key : " + pk.number); 251 throw new javax.ejb.ObjectNotFoundException ("primary key : " + pk.number); 252 } 253 } catch (SQLException e) { 254 System.err.println("Fails to executeQuery " + e); 255 throw new javax.ejb.FinderException ("Failed to executeQuery " + e); 256 } finally { 257 try { 258 loadPstmt.close(); 259 conn.close(); 260 } catch (Exception e) { 261 logger.log(BasicLevel.ERROR, "ejbFindByNoAccount: Ignore exception:" + e); 262 } 263 } 264 return pk; 265 } 266 267 public Enumeration ejbFindCustomerAccounts(String name) throws FinderException { 268 logger.log(BasicLevel.DEBUG, ""); 269 PreparedStatement loadPstmt = null; 270 Connection conn = null; 271 Vector pkV = new Vector (); 272 try { 273 conn = getConnection(); 274 loadPstmt = conn.prepareStatement("select c_number from " + tableName + " where c_customer=?"); 275 loadPstmt.setString(1, name); 276 ResultSet rs = loadPstmt.executeQuery(); 277 while (rs.next()) { 278 int b = rs.getInt("c_number"); 279 AccountPK pk = new AccountPK(b); 280 pkV.addElement((Object ) pk); 281 } 282 283 } catch (SQLException e) { 284 System.err.println("Fails to executeQuery " + e); 285 throw new javax.ejb.FinderException ("Failed to executeQuery " + e); 286 } finally { 287 try { 288 loadPstmt.close(); 289 conn.close(); 290 } catch (Exception e) { 291 logger.log(BasicLevel.ERROR, "ejbFindCustomerAccounts: Ignore exception:" + e); 292 } 293 } 294 return (pkV.elements()); 295 } 296 297 public Enumeration ejbFindBigAccounts(long minBalance) throws FinderException { 298 logger.log(BasicLevel.DEBUG, ""); 299 PreparedStatement loadPstmt = null; 300 Connection conn = null; 301 Vector pkV = new Vector (); 302 try { 303 conn = getConnection(); 304 loadPstmt = conn.prepareStatement("select c_number from " + tableName + " where c_balance>?"); 305 loadPstmt.setLong(1, minBalance); 306 ResultSet rs = loadPstmt.executeQuery(); 307 while (rs.next()) { 308 int b = rs.getInt("c_number"); 309 AccountPK pk = new AccountPK(b); 310 pkV.addElement((Object ) pk); 311 } 312 } catch (SQLException e) { 313 System.err.println("Fails to executeQuery " + e); 314 throw new javax.ejb.FinderException ("Failed to executeQuery " + e); 315 } finally { 316 try { 317 loadPstmt.close(); 318 conn.close(); 319 } catch (Exception e) { 320 logger.log(BasicLevel.ERROR, "ejbFindBigAccounts: Ignore exception:" + e); 321 } 322 } 323 return (pkV.elements()); 324 } 325 326 public Enumeration ejbFindAllAccounts() throws FinderException { 327 logger.log(BasicLevel.DEBUG, ""); 328 PreparedStatement loadPstmt = null; 329 Connection conn = null; 330 Vector pkV = new Vector (); 331 try { 332 conn = getConnection(); 333 loadPstmt = conn.prepareStatement("select c_number from " + tableName); 334 ResultSet rs = loadPstmt.executeQuery(); 335 while (rs.next()) { 336 int b = rs.getInt("c_number"); 337 AccountPK pk = new AccountPK(b); 338 pkV.addElement((Object ) pk); 339 } 340 } catch (SQLException e) { 341 System.err.println("Fails to executeQuery " + e); 342 throw new javax.ejb.FinderException ("Failed to executeQuery " + e); 343 } finally { 344 try { 345 loadPstmt.close(); 346 conn.close(); 347 } catch (Exception e) { 348 logger.log(BasicLevel.ERROR, "ejbFindAllAccounts: Ignore exception:" + e); 349 } 350 } 351 return (pkV.elements()); 352 } 353 354 private Connection getConnection() throws java.sql.SQLException { 355 logger.log(BasicLevel.DEBUG, ""); 356 if (dataSource == null) { 357 Context initialContext = null; 359 try { 360 initialContext = new InitialContext (); 361 dataSource = (DataSource ) initialContext.lookup("java:comp/env/jdbc/AccountEB"); 362 } catch (Exception e) { 363 logger.log(BasicLevel.ERROR, "Pb with naming context"); 364 throw new javax.ejb.EJBException ("Pb with naming context "); 365 } 366 } 367 Connection ret = dataSource.getConnection(); 368 if (ret == null) { 369 throw new javax.ejb.EJBException ("dataSource.getConnection() returned null"); 370 } 371 return ret; 372 } 373 } 374 | Popular Tags |