1 25 26 package org.objectweb.jonas.jtests.beans.jdbcra; 27 28 import java.sql.Connection ; 29 import java.sql.PreparedStatement ; 30 import java.sql.ResultSet ; 31 import java.sql.SQLException ; 32 import java.sql.Statement ; 33 import java.util.Enumeration ; 34 import java.util.Vector ; 35 36 import javax.ejb.CreateException ; 37 import javax.ejb.EJBException ; 38 import javax.ejb.EntityBean ; 39 import javax.ejb.EntityContext ; 40 import javax.ejb.FinderException ; 41 import javax.ejb.ObjectNotFoundException ; 42 import javax.ejb.RemoveException ; 43 import javax.naming.Context ; 44 import javax.naming.InitialContext ; 45 import javax.sql.DataSource ; 46 47 import org.objectweb.jonas.common.Log; 48 import org.objectweb.util.monolog.api.BasicLevel; 49 import org.objectweb.util.monolog.api.Logger; 50 51 54 public class JdbcRA1EBRBean implements EntityBean { 55 56 private DataSource dataSource1 = null; 57 58 static private Logger logger = null; 59 EntityContext ejbContext; 60 61 66 public Integer accno; 67 public String customer; 68 public double balance; 69 70 74 public void setEntityContext(EntityContext ctx) { 75 if (logger == null) { 76 logger = Log.getLogger("org.objectweb.jonas_tests"); 77 } 78 logger.log(BasicLevel.DEBUG, ""); 79 ejbContext = ctx; 80 } 81 82 public void unsetEntityContext() { 83 logger.log(BasicLevel.DEBUG, ""); 84 ejbContext = null; 85 } 86 87 public void ejbRemove() throws RemoveException { 88 logger.log(BasicLevel.DEBUG, ""); 89 90 92 Connection conn = null; 93 PreparedStatement stmt = null; 94 95 try { 96 98 conn = getConnection(); 99 100 102 stmt = conn.prepareStatement("delete from jdbc_xa1 where xa_accno=?"); 103 Integer pk = (Integer ) ejbContext.getPrimaryKey(); 104 stmt.setInt(1, pk.intValue()); 105 stmt.executeUpdate(); 106 107 } catch (SQLException e) { 108 throw new RemoveException ("Failed to delete bean from database " +e); 109 } finally { 110 try { 111 if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (Exception ignore) { 114 } 115 } 116 117 } 118 119 public void ejbLoad() { 120 logger.log(BasicLevel.DEBUG, ""); 121 122 124 Connection conn = null; 125 PreparedStatement stmt = null; 126 127 try { 128 130 conn = getConnection(); 131 132 134 stmt = conn.prepareStatement("select xa_customer,xa_balance from jdbc_xa1 where xa_accno=?"); 135 Integer pk = (Integer ) ejbContext.getPrimaryKey(); 136 stmt.setInt(1, pk.intValue()); 137 ResultSet rs = stmt.executeQuery(); 138 139 if (rs.next() == false) { 140 throw new EJBException ("Failed to load bean from database"); 141 } 142 143 145 accno = pk; 146 customer = rs.getString("xa_customer"); 147 balance = rs.getDouble("xa_balance"); 148 149 } catch (SQLException e) { 150 throw new EJBException ("Failed to load bean from database " +e); 151 } finally { 152 try { 153 if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (Exception ignore) { 156 } 157 } 158 } 159 160 public void ejbStore() { 161 logger.log(BasicLevel.DEBUG, ""); 162 163 165 Connection conn = null; 166 PreparedStatement stmt = null; 167 168 try { 169 171 conn = getConnection(); 172 173 175 stmt = conn.prepareStatement("update jdbc_xa1 set xa_customer=?,xa_balance=? where xa_accno=?"); 176 stmt.setString(1, customer); 177 stmt.setDouble(2, balance); 178 Integer pk = (Integer ) ejbContext.getPrimaryKey(); 179 stmt.setInt(3, pk.intValue()); 180 stmt.executeUpdate(); 181 182 } catch (SQLException e) { 183 throw new EJBException ("Failed to store bean to database " +e); 184 } finally { 185 try { 186 if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (Exception ignore) { 189 } 190 } 191 } 192 193 public void ejbPassivate() { 194 logger.log(BasicLevel.DEBUG, ""); 195 } 196 197 public void ejbActivate() { 198 logger.log(BasicLevel.DEBUG, ""); 199 } 200 201 public void ejbPostCreate(int val_accno, String val_customer, double val_balance){ 202 logger.log(BasicLevel.DEBUG, ""); 203 } 204 205 public void ejbPostCreate() { 206 logger.log(BasicLevel.DEBUG, ""); 207 } 208 209 public java.lang.Integer ejbCreate(int val_accno, String val_customer, double val_balance) 210 throws CreateException { 211 212 logger.log(BasicLevel.DEBUG, ""); 213 214 217 accno = new Integer (val_accno); 218 customer = val_customer; 219 balance = val_balance; 220 221 Connection conn = null; 222 PreparedStatement stmt = null; 223 224 try { 225 227 conn = getConnection(); 228 229 231 stmt = conn.prepareStatement("insert into jdbc_xa1 (xa_accno, xa_customer, xa_balance) values (?, ?, ?)"); 232 stmt.setInt(1, accno.intValue()); 233 stmt.setString(2, customer); 234 stmt.setDouble(3, balance); 235 stmt.executeUpdate(); 236 237 } catch (SQLException e) { 238 throw new CreateException ("Failed to create bean in database: "+e); 239 } finally { 240 try { 241 if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (Exception ignore) { 244 } 245 } 246 247 return accno; 249 250 } 251 252 public java.lang.Integer ejbFindByPrimaryKey(Integer pk) 253 throws ObjectNotFoundException , FinderException { 254 logger.log(BasicLevel.DEBUG, ""); 255 256 258 Connection conn = null; 259 PreparedStatement stmt = null; 260 261 try { 262 264 conn = getConnection(); 265 266 stmt = conn.prepareStatement("select xa_accno from jdbc_xa1 where xa_accno=?"); 268 stmt.setInt(1, pk.intValue()); 269 ResultSet rs = stmt.executeQuery(); 270 271 if (rs.next() == false) { 272 throw new javax.ejb.ObjectNotFoundException (); 273 } 274 275 } catch (SQLException e) { 276 throw new javax.ejb.FinderException ("Failed to executeQuery " +e); 277 } finally { 278 try { 279 if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (Exception ignore) { 282 } 283 } 284 285 return pk; 286 } 287 288 289 297 298 public Integer ejbFindByNumber(int accno) 299 throws ObjectNotFoundException , FinderException { 300 301 Connection conn = null; 302 PreparedStatement stmt = null; 303 304 try { 305 307 conn = getConnection(); 308 309 311 stmt = conn.prepareStatement("select xa_accno from jdbc_xa1 where xa_accno=?"); 312 stmt.setInt(1, accno); 313 ResultSet rs = stmt.executeQuery(); 314 315 if (rs.next() == false) { 316 throw new javax.ejb.ObjectNotFoundException (); 317 } 318 319 } catch (SQLException e) { 320 throw new javax.ejb.FinderException ("Failed to executeQuery " +e); 321 } finally { 322 try { 323 if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (Exception ignore) { 326 } 327 } 328 329 return new Integer (accno); 331 } 332 333 340 341 public Enumeration ejbFindAllAccounts() throws FinderException { 342 343 Connection conn = null; 344 PreparedStatement stmt = null; 345 346 Vector pkv = new Vector (); 347 348 try { 349 351 conn = getConnection(); 352 353 355 stmt = conn.prepareStatement("select xa_accno from jdbc_xa1"); 356 ResultSet rs = stmt.executeQuery(); 357 358 360 while (rs.next()) { 361 Integer pk = new Integer (rs.getInt("xa_accno")); 362 pkv.addElement((Object )pk); 363 }; 364 365 } catch (SQLException e) { 366 e.printStackTrace(); 367 throw new javax.ejb.FinderException ("Failed to executeQuery " +e); 368 } finally { 369 try { 370 if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (Exception ignore) { 373 } 374 } 375 376 return pkv.elements(); 378 } 379 380 public java.lang.Integer ejbCreate() 381 throws CreateException { 382 383 logger.log(BasicLevel.DEBUG, ""); 384 385 Integer tempint = new Integer (0); 387 Connection conn = null; 388 Statement stmt = null; 389 390 try { 391 393 conn = getConnection(); 394 395 397 stmt = conn.createStatement(); 398 stmt.addBatch("insert into jdbc_xa1 (xa_accno, xa_customer, xa_balance) values (201, 'Albert Smith', 500)"); 399 stmt.addBatch("insert into jdbc_xa1 (xa_accno, xa_customer, xa_balance) values (202, 'Bob Smith', 500)"); 400 stmt.addBatch("insert into jdbc_xa1 (xa_accno, xa_customer, xa_balance) values (203, 'Carl Smith', 500)"); 401 stmt.addBatch("insert into jdbc_xa1 (xa_accno, xa_customer, xa_balance) values (204, 'David Smith', 500)"); 402 stmt.addBatch("insert into jdbc_xa1 (xa_accno, xa_customer, xa_balance) values (205, 'Edward Smith', 500)"); 403 int[] upCounts = stmt.executeBatch(); 404 405 } catch (SQLException e) { 406 throw new CreateException ("Failed to create bean in database: "+e); 407 } finally { 408 try { 409 if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (Exception ignore) { 412 } 413 } 414 return tempint; 415 } 416 417 423 424 private Connection getConnection() throws EJBException , SQLException { 425 426 Connection myconn1 = null; 427 428 if (dataSource1 == null) { 429 430 432 Context initialContext = null; 433 434 try { 435 initialContext = new InitialContext (); 436 dataSource1 = (DataSource )initialContext.lookup("java:comp/env/jdbc/JdbcRA1Ds"); 437 } catch (Exception e) { 438 throw new javax.ejb.EJBException ("Cannot lookup dataSource1 "+e); 439 } 440 } 441 442 try { 443 myconn1 = dataSource1.getConnection(); 444 } catch (Exception e) { 445 throw new javax.ejb.EJBException ("Cannot getConnection dataSource1 "+e); 446 } 447 448 return myconn1; 449 } 450 451 452 453 459 460 public double getBalance(){ 461 return balance; 462 } 463 464 470 471 public void setBalance(double d){ 472 balance = balance + d; 473 } 474 475 481 482 public String getCustomer(){ 483 return customer; 484 } 485 486 492 493 public void setCustomer(String c) { 494 customer = c; 495 } 496 497 500 501 public int getNumber() { 502 return accno.intValue(); 503 } 504 505 } 506 507 508 509 | Popular Tags |