1 25 26 package org.objectweb.jonas.jtests.beans.transacted; 27 28 import java.sql.Connection ; 29 import java.sql.PreparedStatement ; 30 import java.sql.ResultSet ; 31 import java.sql.Types ; 32 33 import javax.ejb.CreateException ; 34 import javax.ejb.EJBException ; 35 import javax.ejb.EntityBean ; 36 import javax.ejb.EntityContext ; 37 import javax.ejb.FinderException ; 38 import javax.ejb.ObjectNotFoundException ; 39 import javax.ejb.RemoveException ; 40 import javax.naming.Context ; 41 import javax.naming.InitialContext ; 42 import javax.sql.DataSource ; 43 44 48 public class SimpleEB extends SimpleEC implements EntityBean { 49 50 51 private DataSource dataSource = null; 53 static final String tableName = "transactedSimpleEB"; 54 55 56 59 public java.lang.String ejbCreate() 60 throws javax.ejb.CreateException { 61 62 super.ejbCreate(); 63 64 java.lang.String pk; 65 pk = this.accno; 67 68 Connection conn = null; 69 PreparedStatement pStmt = null; 70 try { 71 conn = getConnection(); 72 pStmt = conn.prepareStatement("insert into "+tableName+" (c_accno, c_customer, c_balance) values (?, ?, ?)"); 73 if ( this.accno == null ) { 74 pStmt.setNull(1, Types.VARCHAR); 75 } else { 76 pStmt.setString(1, this.accno); 77 } 78 if ( this.customer == null ) { 79 pStmt.setNull(2, Types.VARCHAR); 80 } else { 81 pStmt.setString(2, this.customer); 82 } 83 pStmt.setLong(3, this.balance); 84 pStmt.executeUpdate(); 85 } catch (Exception e) { 86 throw new CreateException (e.toString()); 87 } finally { 88 if (pStmt != null) { 89 try { 90 pStmt.close(); 91 } catch (Exception ignore) { 92 } 94 } 95 if (conn != null) { 96 try { 97 conn.close(); 98 } catch (Exception ignore) { 99 } 101 } 102 } 103 104 return pk; 105 106 } 107 108 109 110 113 public java.lang.String ejbCreate(int p1) 114 throws javax.ejb.CreateException { 115 116 117 this.accno = null; 119 this.customer = null; 120 this.balance = 0L; 121 122 super.ejbCreate(p1); 123 124 java.lang.String pk; 125 pk = this.accno; 127 128 Connection conn = null; 129 PreparedStatement pStmt = null; 130 try { 131 conn = getConnection(); 132 pStmt = conn.prepareStatement("insert into "+tableName+" (c_accno, c_customer, c_balance) values (?, ?, ?)"); 133 if ( this.accno == null ) { 134 pStmt.setNull(1, Types.VARCHAR); 135 } else { 136 pStmt.setString(1, this.accno); 137 } 138 if ( this.customer == null ) { 139 pStmt.setNull(2, Types.VARCHAR); 140 } else { 141 pStmt.setString(2, this.customer); 142 } 143 pStmt.setLong(3, this.balance); 144 pStmt.executeUpdate(); 145 } catch (Exception e) { 146 throw new CreateException (e.toString()); 147 } finally { 148 if (pStmt != null) { 149 try { 150 pStmt.close(); 151 } catch (Exception ignore) { 152 } 154 } 155 if (conn != null) { 156 try { 157 conn.close(); 158 } catch (Exception ignore) { 159 } 161 } 162 } 163 164 return pk; 165 166 } 167 168 169 172 public java.lang.String ejbCreateForMandatory(char p1) 173 throws javax.ejb.CreateException { 174 175 176 this.accno = null; 178 this.customer = null; 179 this.balance = 0L; 180 181 super.ejbCreateForMandatory(p1); 182 183 java.lang.String pk; 184 pk = this.accno; 186 187 Connection conn = null; 188 PreparedStatement pStmt = null; 189 try { 190 conn = getConnection(); 191 pStmt = conn.prepareStatement("insert into "+tableName+" (c_accno, c_customer, c_balance) values (?, ?, ?)"); 192 if ( this.accno == null ) { 193 pStmt.setNull(1, Types.VARCHAR); 194 } else { 195 pStmt.setString(1, this.accno); 196 } 197 if ( this.customer == null ) { 198 pStmt.setNull(2, Types.VARCHAR); 199 } else { 200 pStmt.setString(2, this.customer); 201 } 202 pStmt.setLong(3, this.balance); 203 pStmt.executeUpdate(); 204 } catch (Exception e) { 205 throw new CreateException (e.toString()); 206 } finally { 207 if (pStmt != null) { 208 try { 209 pStmt.close(); 210 } catch (Exception ignore) { 211 } 213 } 214 if (conn != null) { 215 try { 216 conn.close(); 217 } catch (Exception ignore) { 218 } 220 } 221 } 222 223 return pk; 224 225 } 226 227 228 231 public java.lang.String ejbCreateForNever(short p1) 232 throws javax.ejb.CreateException { 233 234 235 this.accno = null; 237 this.customer = null; 238 this.balance = 0L; 239 240 super.ejbCreateForNever(p1); 241 242 java.lang.String pk; 243 pk = this.accno; 245 246 Connection conn = null; 247 PreparedStatement pStmt = null; 248 try { 249 conn = getConnection(); 250 pStmt = conn.prepareStatement("insert into "+tableName+" (c_accno, c_customer, c_balance) values (?, ?, ?)"); 251 if ( this.accno == null ) { 252 pStmt.setNull(1, Types.VARCHAR); 253 } else { 254 pStmt.setString(1, this.accno); 255 } 256 if ( this.customer == null ) { 257 pStmt.setNull(2, Types.VARCHAR); 258 } else { 259 pStmt.setString(2, this.customer); 260 } 261 pStmt.setLong(3, this.balance); 262 pStmt.executeUpdate(); 263 } catch (Exception e) { 264 throw new CreateException (e.toString()); 265 } finally { 266 if (pStmt != null) { 267 try { 268 pStmt.close(); 269 } catch (Exception ignore) { 270 } 272 } 273 if (conn != null) { 274 try { 275 conn.close(); 276 } catch (Exception ignore) { 277 } 279 } 280 } 281 282 return pk; 283 284 } 285 286 287 290 public java.lang.String ejbCreateForRequired(long p1) 291 throws javax.ejb.CreateException { 292 293 294 this.accno = null; 296 this.customer = null; 297 this.balance = 0L; 298 299 super.ejbCreateForRequired(p1); 300 301 java.lang.String pk; 302 pk = this.accno; 304 305 Connection conn = null; 306 PreparedStatement pStmt = null; 307 try { 308 conn = getConnection(); 309 pStmt = conn.prepareStatement("insert into "+tableName+" (c_accno, c_customer, c_balance) values (?, ?, ?)"); 310 if ( this.accno == null ) { 311 pStmt.setNull(1, Types.VARCHAR); 312 } else { 313 pStmt.setString(1, this.accno); 314 } 315 if ( this.customer == null ) { 316 pStmt.setNull(2, Types.VARCHAR); 317 } else { 318 pStmt.setString(2, this.customer); 319 } 320 pStmt.setLong(3, this.balance); 321 pStmt.executeUpdate(); 322 } catch (Exception e) { 323 throw new CreateException (e.toString()); 324 } finally { 325 if (pStmt != null) { 326 try { 327 pStmt.close(); 328 } catch (Exception ignore) { 329 } 331 } 332 if (conn != null) { 333 try { 334 conn.close(); 335 } catch (Exception ignore) { 336 } 338 } 339 } 340 341 return pk; 342 343 } 344 345 346 349 public java.lang.String ejbCreateForRequiresNew(java.lang.String p1) 350 throws javax.ejb.CreateException { 351 352 353 this.accno = null; 355 this.customer = null; 356 this.balance = 0L; 357 358 super.ejbCreateForRequiresNew(p1); 359 360 java.lang.String pk; 361 pk = this.accno; 363 364 Connection conn = null; 365 PreparedStatement pStmt = null; 366 try { 367 conn = getConnection(); 368 pStmt = conn.prepareStatement("insert into "+tableName+" (c_accno, c_customer, c_balance) values (?, ?, ?)"); 369 if ( this.accno == null ) { 370 pStmt.setNull(1, Types.VARCHAR); 371 } else { 372 pStmt.setString(1, this.accno); 373 } 374 if ( this.customer == null ) { 375 pStmt.setNull(2, Types.VARCHAR); 376 } else { 377 pStmt.setString(2, this.customer); 378 } 379 pStmt.setLong(3, this.balance); 380 pStmt.executeUpdate(); 381 } catch (Exception e) { 382 throw new CreateException (e.toString()); 383 } finally { 384 if (pStmt != null) { 385 try { 386 pStmt.close(); 387 } catch (Exception ignore) { 388 } 390 } 391 if (conn != null) { 392 try { 393 conn.close(); 394 } catch (Exception ignore) { 395 } 397 } 398 } 399 400 return pk; 401 402 } 403 404 405 408 public java.lang.String ejbCreateForSupports(boolean p1) 409 throws javax.ejb.CreateException { 410 411 412 this.accno = null; 414 this.customer = null; 415 this.balance = 0L; 416 417 super.ejbCreateForSupports(p1); 418 419 java.lang.String pk; 420 pk = this.accno; 422 423 Connection conn = null; 424 PreparedStatement pStmt = null; 425 try { 426 conn = getConnection(); 427 pStmt = conn.prepareStatement("insert into "+tableName+" (c_accno, c_customer, c_balance) values (?, ?, ?)"); 428 if ( this.accno == null ) { 429 pStmt.setNull(1, Types.VARCHAR); 430 } else { 431 pStmt.setString(1, this.accno); 432 } 433 if ( this.customer == null ) { 434 pStmt.setNull(2, Types.VARCHAR); 435 } else { 436 pStmt.setString(2, this.customer); 437 } 438 pStmt.setLong(3, this.balance); 439 pStmt.executeUpdate(); 440 } catch (Exception e) { 441 throw new CreateException (e.toString()); 442 } finally { 443 if (pStmt != null) { 444 try { 445 pStmt.close(); 446 } catch (Exception ignore) { 447 } 449 } 450 if (conn != null) { 451 try { 452 conn.close(); 453 } catch (Exception ignore) { 454 } 456 } 457 } 458 459 return pk; 460 461 } 462 463 464 467 public java.lang.String ejbFindByPrimaryKey(java.lang.String pk) 468 throws FinderException { 469 470 Connection conn = null; 471 PreparedStatement pStmt = null; 472 try { 473 conn = getConnection(); 474 pStmt = conn.prepareStatement("select c_accno from "+tableName+" where c_accno=?"); 475 pStmt.setString(1, pk); 476 ResultSet rs = pStmt.executeQuery(); 477 if (rs.next() == false) { 478 throw new ObjectNotFoundException ("Object not found in database (ejbFindByPrimaryKey("+pk.toString()+"))"); 479 } 480 } catch (ObjectNotFoundException oe) { 481 throw oe; 482 } catch (Exception e) { 483 throw new FinderException ("Failed to find bean from database in ejbFindByPrimaryKey"); 484 } finally { 485 if (pStmt != null) { 486 try { 487 pStmt.close(); 488 } catch (Exception ignore) { 489 } 491 } 492 if (conn != null) { 493 try { 494 conn.close(); 495 } catch (Exception ignore) { 496 } 498 } 499 } 500 return pk; 501 } 502 503 504 507 public java.lang.String ejbFinder_mandatory() 508 throws FinderException { 509 510 511 if (!isAssociated()) { 513 throw new EJBException ("ejbFinder_mandatory: should not be in a transaction"); 514 } 515 516 return commonFind("Mandatory"); 517 } 518 519 520 523 public java.lang.String ejbFinder_never() 524 throws FinderException { 525 526 if (isAssociated()) { 527 throw new EJBException ("ejbFinder_never in a transaction"); 528 } 529 530 return commonFind("Never"); 531 } 532 533 534 537 public java.lang.String ejbFinder_notsupported() 538 throws FinderException { 539 if (isAssociated()) { 541 throw new EJBException ("ejbFinder_notsupported: should not be in a transaction"); 542 } 543 544 return commonFind("NotSupported"); 545 } 546 547 548 551 public java.lang.String ejbFinder_required() 552 throws FinderException { 553 554 if (!isAssociated()) { 556 throw new EJBException ("ejbFinder_notsupported: should not be in a transaction"); 557 } 558 559 return commonFind("Required"); 560 561 } 562 563 564 567 public java.lang.String ejbFinder_requiresnew() 568 throws FinderException { 569 570 if (!isAssociated()) { 572 throw new EJBException ("ejbFinder_requiresnew not in a transaction"); 573 } 574 575 return commonFind("RequiresNew"); 576 } 577 578 579 582 public java.lang.String ejbFinder_supports(boolean p1) 583 throws FinderException { 584 585 586 if (p1 == true) { 587 if (!isAssociated()) { 588 throw new EJBException ("ejbFinder_supports: should be in a transaction"); 589 } 590 } else { 591 if (isAssociated()) { 592 throw new EJBException ("ejbFinder_supports: should not be in a transaction"); 593 } 594 } 595 return commonFind("Supports"); 596 } 597 598 599 602 public void ejbLoad() 603 { 604 Connection conn = null; 605 PreparedStatement pStmt = null; 606 try { 607 String pk = (String )entityContext.getPrimaryKey(); 608 conn = getConnection(); 609 pStmt = conn.prepareStatement("select c_accno, c_customer, c_balance from "+tableName+" where c_accno=?"); 610 pStmt.setString(1, pk); 611 ResultSet rs = pStmt.executeQuery(); 612 if (rs.next() == false) { 613 throw new EJBException ("Failed to load bean from database pk= "+pk.toString()); 614 } 615 this.accno = rs.getString(1); 616 this.customer = rs.getString(2); 617 this.balance = rs.getLong(3); 618 } catch (Exception e) { 619 throw new EJBException (e); 620 } finally { 621 if (pStmt != null) { 622 try { 623 pStmt.close(); 624 } catch (Exception ignore) { 625 } 627 } 628 if (conn != null) { 629 try { 630 conn.close(); 631 } catch (Exception ignore) { 632 } 634 } 635 } 636 super.ejbLoad(); 637 } 638 639 640 643 public void ejbStore() 644 { 645 super.ejbStore(); 646 Connection conn = null; 647 PreparedStatement pStmt = null; 648 try { 649 String pk = (String )entityContext.getPrimaryKey();; 650 conn = getConnection(); 651 pStmt = conn.prepareStatement("update "+tableName+" set c_customer=?, c_balance=? where c_accno=?"); 652 if ( this.customer == null ) { 653 pStmt.setNull(1, Types.VARCHAR); 654 } else { 655 pStmt.setString(1, this.customer); 656 } 657 pStmt.setLong(2, this.balance); 658 pStmt.setString(3, pk); 659 pStmt.executeUpdate(); 660 } catch (Exception e) { 661 throw new EJBException (e); 662 } finally { 663 if (pStmt != null) { 664 try { 665 pStmt.close(); 666 } catch (Exception ignore) { 667 } 669 } 670 if (conn != null) { 671 try { 672 conn.close(); 673 } catch (Exception ignore) { 674 } 676 } 677 } 678 } 679 680 681 684 public void ejbRemove() 685 throws javax.ejb.RemoveException { 686 687 super.ejbRemove(); 688 Connection conn = null; 689 PreparedStatement pStmt = null; 690 try { 691 String pk = (String )entityContext.getPrimaryKey();; 692 conn = getConnection(); 693 pStmt = conn.prepareStatement("delete from "+tableName+" where c_accno=?"); 694 pStmt.setString(1, pk); 695 pStmt.executeUpdate(); 696 } catch (Exception e) { 697 throw new RemoveException (e.toString()); 698 } finally { 699 if (pStmt != null) { 700 try { 701 pStmt.close(); 702 } catch (Exception ignore) { 703 } 705 } 706 if (conn != null) { 707 try { 708 conn.close(); 709 } catch (Exception ignore) { 710 } 712 } 713 } 714 } 715 716 717 718 public void setEntityContext(EntityContext ctx) { 719 this.entityContext = ctx; 720 } 721 722 public void unsetEntityContext() { 723 this.entityContext = null; 724 } 725 726 727 731 732 private java.lang.String commonFind(String p1) throws FinderException { 733 java.lang.String pk; 734 Connection conn = null; 735 PreparedStatement pStmt = null; 736 try { 737 conn = getConnection(); 738 pStmt = conn.prepareStatement("select c_accno from "+tableName+" where c_customer = "+"'"+p1+"'"); 739 ResultSet rs = pStmt.executeQuery(); 740 if (rs.next() == false) { 741 throw new ObjectNotFoundException ("Object not found in database "); 742 } 743 pk = rs.getString(1); 744 } catch (ObjectNotFoundException oe) { 745 throw oe; 746 } catch (Exception e) { 747 throw new FinderException (e.toString()); 748 } finally { 749 if (pStmt != null) { 750 try { 751 pStmt.close(); 752 } catch (Exception ignore) { 753 } 755 } 756 if (conn != null) { 757 try { 758 conn.close(); 759 } catch (Exception ignore) { 760 } 762 } 763 } 764 return pk; 765 } 766 767 768 private Connection getConnection() throws java.sql.SQLException { 769 if (dataSource == null) { 770 Context initialContext = null; 772 try { 773 initialContext = new InitialContext (); 774 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/BMP"); 775 } catch (Exception e) { 776 throw new javax.ejb.EJBException ("Pb with naming context "+e); 777 } 778 } 779 Connection ret = dataSource.getConnection(); 780 if (ret == null) { 781 throw new javax.ejb.EJBException ("dataSource.getConnection() returned null"); 782 } 783 return ret; 784 } 785 786 } 787 | Popular Tags |