1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbcapi; 23 24 import org.apache.derby.tools.ij; 25 import java.sql.Connection ; 26 import java.sql.Statement ; 27 import java.sql.PreparedStatement ; 28 import java.sql.CallableStatement ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 import java.sql.Date ; 32 import java.sql.Types ; 33 import java.util.GregorianCalendar ; 34 import org.apache.derby.iapi.reference.JDBC20Translation; 35 import org.apache.derbyTesting.functionTests.util.BigDecimalHandler; 36 import org.apache.derbyTesting.functionTests.util.TestUtil; 37 38 public class StmtCloseFunTest { 39 40 private static boolean HAVE_BIG_DECIMAL; 41 private static String CLASS_NAME; 42 43 static{ 46 if(BigDecimalHandler.representation != BigDecimalHandler.BIGDECIMAL_REPRESENTATION) 47 HAVE_BIG_DECIMAL = false; 48 else 49 HAVE_BIG_DECIMAL = true; 50 if(HAVE_BIG_DECIMAL) 51 CLASS_NAME = "org.apache.derbyTesting.functionTests.tests.lang.outparams30."; 52 else 53 CLASS_NAME = "org.apache.derbyTesting.functionTests.tests.lang.outparams."; 54 } 55 56 static private boolean isDerbyNet = false; 57 58 public static void main(String [] args) { 59 60 System.out.println("Statement Close Fun Test starting "); 61 isDerbyNet = TestUtil.isJCCFramework(); 62 63 64 65 try { 66 ij.getPropertyArg(args); 69 Connection conn = ij.startJBMS(); 70 cleanUp(conn); 71 test1(conn); 72 test2(conn); 73 test3(conn); 74 75 cleanUp(conn); 76 conn.close(); 77 78 } catch (SQLException e) { 79 dumpSQLExceptions(e); 80 } catch (Throwable e) { 81 System.out.println("FAIL -- unexpected exception:" + e.toString()); 82 e.printStackTrace(System.out); 83 } 84 85 System.out.println("Statement Close Fun Test finished"); 86 } 87 88 89 private static void cleanUp(Connection conn) throws SQLException { 90 Statement s = conn.createStatement(); 91 String [] testObjects = {"PROCEDURE TAKESSTRING", "TABLE TAB1", "TABLE TAB2", "TABLE TAB3"}; 92 try { 93 TestUtil.cleanUpTest(s, testObjects); 94 } catch (SQLException se){ } 96 } 97 98 private static void test1(Connection conn) { 99 Statement s; 100 try { 101 System.out.println("Statement test begin"); 102 s = conn.createStatement(); 103 106 s.execute("create table tab1(num int, addr varchar(40))"); 107 s.execute("insert into tab1 values (1910,'Union St.')"); 108 s.close(); 109 110 try { 111 s.executeQuery("create table tab2(num int)"); 112 System.out.println("Statement Test failed (1)"); 113 } 114 catch(SQLException e) { } 115 116 try { 117 s.executeUpdate("update tab1 set num=180, addr='Grand Ave.' where num=1910"); 118 System.out.println("Statement Test failed"); 119 } 120 catch(SQLException e) { } 121 122 126 try { 127 s.close(); 128 } 129 catch(SQLException e) { 130 System.out.println("Statement Test failed (2) " + e.toString()); 131 } 132 133 try { 134 s.execute("insert into tab1 values (300,'Lakeside Dr.')"); 135 System.out.println("Statement Test failed (3)"); 136 } 137 catch(SQLException e) { } 138 139 try { 140 s.getMaxFieldSize(); 141 System.out.println("Statement Test failed (4)"); 142 } 143 catch(SQLException e) { } 144 145 try { 146 s.setMaxFieldSize(100); 147 System.out.println("Statement Test failed (5)"); 148 } 149 catch(SQLException e) { } 150 151 try { 152 s.getMaxRows(); 153 System.out.println("Statement Test failed (6)"); 154 } 155 catch(SQLException e) { } 156 157 try { 158 s.setMaxRows(1000); 159 System.out.println("Statement Test failed (7)"); 160 } 161 catch(SQLException e) { } 162 163 try { 164 s.setEscapeProcessing(true); 165 System.out.println("Statement Test failed (8)"); 166 } 167 catch(SQLException e) { } 168 169 if (! isDerbyNet) 170 { 171 try { 173 s.getQueryTimeout(); 174 System.out.println("Statement Test failed (9)"); 175 } 176 catch(SQLException e) { } 177 } 178 try { 179 s.setQueryTimeout(20); 180 System.out.println("Statement Test failed (10)"); 181 } 182 catch(SQLException e) { } 183 184 try { 185 s.cancel(); 186 System.out.println("Statement Test failed (11)"); 187 } 188 catch(SQLException e) { } 189 190 if (isDerbyNet) 191 System.out.println("beetle 5524"); 192 try { 193 s.getWarnings(); 194 System.out.println("Statement Test failed (12)"); 195 } 196 catch(SQLException e) { } 197 198 if (isDerbyNet) 199 System.out.println("beetle 5524"); 200 try { 201 s.clearWarnings(); 202 System.out.println("Statement Test failed (13)"); 203 } 204 catch(SQLException e) { } 205 206 try { 207 s.setCursorName("ABC"); 208 System.out.println("Statement Test failed (14)"); 209 } 210 catch(SQLException e) { } 211 212 try { 213 s.execute("create table tab3(num int)"); 214 System.out.println("Statement Test failed (15)"); 215 } 216 catch(SQLException e) { } 217 218 try { 219 s.getResultSet(); 220 System.out.println("Statement Test failed (16)"); 221 } 222 catch(SQLException e) { } 223 224 try { 225 s.getUpdateCount(); 226 System.out.println("Statement Test failed (17)"); 227 } 228 catch(SQLException e) { } 229 230 try { 231 s.getMoreResults(); 232 System.out.println("Statement Test failed (18) "); 233 } 234 catch(SQLException e) { } 235 236 try { 237 s.getResultSetType(); 238 System.out.println("Statement Test failed (19)"); 239 } 240 catch(SQLException e) { } 241 242 try { 243 s.setFetchDirection(JDBC20Translation.FETCH_FORWARD); 244 System.out.println("Statement Test failed (20)"); 245 } 246 catch(SQLException e) { } 247 248 try { 249 s.setFetchSize(100); 250 System.out.println("Statement Test failed (21)"); 251 } 252 catch(SQLException e) { } 253 254 try { 255 s.getFetchSize(); 256 System.out.println("Statement Test failed (22)"); 257 } 258 catch(SQLException e) { } 259 260 try { 261 s.getResultSetConcurrency(); 262 System.out.println("Statement Test failed (23)"); 263 } 264 catch(SQLException e) { } 265 266 try { 267 s.addBatch("create table tab3(age int)"); 268 System.out.println("Statement Test failed (24)"); 269 } 270 catch(SQLException e) { } 271 272 try { 273 s.clearBatch(); 274 System.out.println("Statement Test failed"); 275 } 276 catch(SQLException e) { } 277 278 try { 279 s.executeBatch(); 280 System.out.println("Statement Test failed"); 281 } 282 catch(SQLException e) { } 283 284 try { 285 s.getConnection(); 286 System.out.println("Statement Test failed"); 287 } 288 catch(SQLException e) { } 289 } 290 catch (Throwable e) { 291 System.out.println("FAIL -- unexpected exception:" + e.toString()); 292 e.printStackTrace(System.out); 293 return; 294 } 295 System.out.println("Statement test end"); 296 } 297 298 private static void test2(Connection conn) { 299 PreparedStatement ps; 300 try { 301 System.out.println("Prepared Statement test begin"); 302 ps = conn.prepareStatement("create table tab2(a int, b float, c date, d varchar(100))"); 303 ps.execute(); 304 ps.close(); 305 306 ps = conn.prepareStatement("select a from tab2"); 308 ps.execute(); 309 ps.close(); 310 try { 311 ps.execute(); 312 System.out.println("Prepared Statement Test failed"); 313 } 314 catch(SQLException e) { } 315 316 try { 317 ps.executeQuery(); 318 System.out.println("Prepared Statement Test failed"); 319 } 320 catch(SQLException e) { } 321 322 ps = conn.prepareStatement("insert into tab2 values(?, ?, ?, ?)"); 323 326 ps.setInt(1, 420); 327 ps.setFloat(2, (float)12.21); 328 ps.setDate(3, new Date (870505200000L)); 329 ps.setString(4, "China"); 330 ps.executeUpdate(); 331 ps.close(); 332 333 try { 335 ps.setInt(1, 530); 336 System.out.println("Prepared Statement Test failed"); 337 } 338 catch(SQLException e) { } 339 340 try { 341 ps.setFloat(2, (float)3.14); 342 System.out.println("Prepared Statement Test failed"); 343 } 344 catch(SQLException e) { } 345 346 try { 347 ps.setDate(3, new Date (870505200000L)); 348 System.out.println("Prepared Statement Test failed"); 349 } 350 catch(SQLException e) { } 351 352 try { 353 ps.setDate(3, new Date (870505200000L), new GregorianCalendar ()); 354 System.out.println("Prepared Statement Test failed"); 355 } 356 catch(SQLException e) { } 357 358 try { 359 ps.setString(4, "HongKong"); 360 System.out.println("Prepared Statement Test failed"); 361 } 362 catch(SQLException e) { } 363 364 try { 365 ps.executeUpdate(); 366 System.out.println("Prepared Statement Test failed"); 367 } 368 catch(SQLException e) { } 369 370 374 try { 375 ps.close(); 376 } 377 catch(SQLException e) { 378 System.out.println("Prepared Statement Test failed"); 379 } 380 381 try { 382 ps.clearParameters(); 383 System.out.println("Prepared Statement Test failed"); 384 } 385 catch(SQLException e) { } 386 387 try { 388 ps.getMetaData(); 389 System.out.println("Prepared Statement Test failed"); 390 } 391 catch(SQLException e) { } 392 393 try { 394 ps.getMaxFieldSize(); 395 System.out.println("Prepared Statement Test failed"); 396 } 397 catch(SQLException e) { } 398 399 try { 400 ps.setMaxFieldSize(100); 401 System.out.println("Prepared Statement Test failed"); 402 } 403 catch(SQLException e) { } 404 405 try { 406 ps.getMaxRows(); 407 System.out.println("Prepared Statement Test failed"); 408 } 409 catch(SQLException e) { } 410 411 try { 412 ps.setMaxRows(1000); 413 System.out.println("Prepared Statement Test failed"); 414 } 415 catch(SQLException e) { } 416 417 try { 418 ps.setEscapeProcessing(true); 419 System.out.println("Prepared Statement Test failed"); 420 } 421 catch(SQLException e) { } 422 423 if (! isDerbyNet) 424 { 425 try { 427 ps.getQueryTimeout(); 428 System.out.println("Prepared Statement Test failed"); 429 } 430 catch(SQLException e) { } 431 } 432 433 try { 434 ps.setQueryTimeout(20); 435 System.out.println("Prepared Statement Test failed"); 436 } 437 catch(SQLException e) { } 438 439 try { 440 ps.cancel(); 441 System.out.println("Prepared Statement Test failed"); 442 } 443 catch(SQLException e) { } 444 445 if (isDerbyNet) 446 System.out.println("beetle 5524"); 447 try { 448 ps.getWarnings(); 449 System.out.println("Prepared Statement Test failed"); 450 } 451 catch(SQLException e) { } 452 453 if (isDerbyNet) 454 System.out.println("beetle 5524"); 455 try { 456 ps.clearWarnings(); 457 System.out.println("Prepared Statement Test failed"); 458 } 459 catch(SQLException e) { } 460 461 try { 462 ps.setCursorName("ABC"); 463 System.out.println("Prepared Statement Test failed"); 464 } 465 catch(SQLException e) { } 466 467 try { 468 ps.execute("create table tab3(num int)"); 469 System.out.println("Prepared Statement Test failed"); 470 } 471 catch(SQLException e) { } 472 473 try { 474 ps.getResultSet(); 475 System.out.println("Prepared Statement Test failed"); 476 } 477 catch(SQLException e) { } 478 479 try { 480 ps.getUpdateCount(); 481 System.out.println("Prepared Statement Test failed"); 482 } 483 catch(SQLException e) { } 484 485 try { 486 ps.getMoreResults(); 487 System.out.println("Prepared Statement Test failed"); 488 } 489 catch(SQLException e) { } 490 491 try { 492 ps.getResultSetType(); 493 System.out.println("Prepared Statement Test failed"); 494 } 495 catch(SQLException e) { } 496 497 try { 498 ps.setFetchDirection(JDBC20Translation.FETCH_FORWARD); 499 System.out.println("Prepared Statement Test failed"); 500 } 501 catch(SQLException e) { } 502 503 try { 504 ps.setFetchSize(100); 505 System.out.println("Prepared Statement Test failed"); 506 } 507 catch(SQLException e) { } 508 509 try { 510 ps.getFetchSize(); 511 System.out.println("Prepared Statement Test failed"); 512 } 513 catch(SQLException e) { } 514 515 try { 516 ps.getResultSetConcurrency(); 517 System.out.println("Prepared Statement Test failed"); 518 } 519 catch(SQLException e) { } 520 521 try { 522 ps.addBatch(); 523 System.out.println("Prepared Statement Test failed"); 524 } 525 catch(SQLException e) { } 526 527 try { 528 ps.clearBatch(); 529 System.out.println("Prepared Statement Test failed"); 530 } 531 catch(SQLException e) { } 532 533 try { 534 ps.executeBatch(); 535 System.out.println("Prepared Statement Test failed"); 536 } 537 catch(SQLException e) { } 538 539 try { 540 ps.getConnection(); 541 System.out.println("Prepared Statement Test failed"); 542 } 543 catch(SQLException e) { } 544 } 545 catch (Throwable e) { 546 System.out.println("FAIL -- unexpected exception:" + e.toString()); 547 e.printStackTrace(System.out); 548 return; 549 } 550 System.out.println("Prepared Statement test end"); 551 } 552 553 private static void test3(Connection conn) { 554 CallableStatement cs; 555 try { 556 System.out.println("Callable Statement test begin"); 557 try { 558 559 Statement s = conn.createStatement(); 560 561 s.execute("CREATE PROCEDURE takesString(OUT P1 VARCHAR(40), IN P2 INT) " + 562 "EXTERNAL NAME '" + CLASS_NAME + "takesString'" + 563 " NO SQL LANGUAGE JAVA PARAMETER STYLE JAVA"); 564 565 566 cs = conn.prepareCall("call takesString(?,?)"); 567 568 cs.registerOutParameter(1, Types.CHAR); 569 cs.setInt(2, Types.INTEGER); 570 571 cs.execute(); 572 System.out.println("The result is " + cs.getString(1)); 573 574 cs.close(); 575 576 try { 577 cs.setString(1, "ABC"); 578 System.out.println("Callable Statement Test failed"); 579 } 580 catch(SQLException e) { } 581 582 try { 583 cs.registerOutParameter(1, Types.CHAR); 584 System.out.println("Callable Statement Test failed"); 585 } 586 catch(SQLException e) { } 587 588 s.close(); 589 } 590 591 catch(SQLException e) { 592 dumpSQLExceptions(e); 593 } 594 595 cs = conn.prepareCall("create table tab3(a int, b float, c varchar(100))"); 597 cs.execute(); 598 cs.close(); 599 600 cs = conn.prepareCall("select a from tab3"); 602 ResultSet rs = cs.executeQuery(); 603 cs.close(); 604 try { 605 cs.execute(); 606 System.out.println("Callable Statement Test failed"); 607 } 608 catch(SQLException e) { } 609 610 try { 611 cs.executeQuery(); 612 System.out.println("Callable Statement Test failed"); 613 } 614 catch(SQLException e) { } 615 616 cs = conn.prepareCall("insert into tab3 values(?, ?, ?)"); 617 620 cs.setInt(1, 420); 621 cs.setFloat(2, (float)12.21); 622 cs.setString(3, "China"); 623 624 cs.executeUpdate(); 625 cs.close(); 626 627 try { 629 cs.setInt(1, 530); 630 System.out.println("Callable Statement Test failed"); 631 } 632 catch(SQLException e) { } 633 634 try { 635 cs.setFloat(2, (float)3.14); 636 System.out.println("Callable Statement Test failed"); 637 } 638 catch(SQLException e) { } 639 640 try { 641 cs.setString(3, "HongKong"); 642 System.out.println("Callable Statement Test failed"); 643 } 644 catch(SQLException e) { } 645 646 try { 647 cs.executeUpdate(); 648 System.out.println("Callable Statement Test failed"); 649 } 650 catch(SQLException e) { } 651 652 656 try { 657 cs.close(); 658 } 659 catch(SQLException e) { 660 System.out.println("Callable Statement Test failed"); 661 } 662 663 try { 664 cs.clearParameters(); 665 System.out.println("Callable Statement Test failed"); 666 } 667 catch(SQLException e) { } 668 669 try { 670 cs.getMetaData(); 671 System.out.println("Callable Statement Test failed"); 672 } 673 catch(SQLException e) { } 674 675 try { 676 cs.getMaxFieldSize(); 677 System.out.println("Callable Statement Test failed"); 678 } 679 catch(SQLException e) { } 680 681 try { 682 cs.setMaxFieldSize(100); 683 System.out.println("Callable Statement Test failed"); 684 } 685 catch(SQLException e) { } 686 687 try { 688 cs.getMaxRows(); 689 System.out.println("Callable Statement Test failed"); 690 } 691 catch(SQLException e) { } 692 693 try { 694 cs.setMaxRows(1000); 695 System.out.println("Callable Statement Test failed"); 696 } 697 catch(SQLException e) { } 698 699 try { 700 cs.setEscapeProcessing(true); 701 System.out.println("Callable Statement Test failed"); 702 } 703 catch(SQLException e) { } 704 705 if (! isDerbyNet) 706 { 707 try { 709 cs.getQueryTimeout(); 710 System.out.println("Callable Statement Test failed"); 711 } 712 catch(SQLException e) { } 713 } 714 715 try { 716 cs.setQueryTimeout(20); 717 System.out.println("Callable Statement Test failed"); 718 } 719 catch(SQLException e) { } 720 721 try { 722 cs.cancel(); 723 System.out.println("Callable Statement Test failed"); 724 } 725 catch(SQLException e) { } 726 727 if (isDerbyNet) 728 System.out.println("beetle 5524"); 729 try { 730 cs.getWarnings(); 731 System.out.println("Callable Statement Test failed"); 732 } 733 catch(SQLException e) { } 734 735 if (isDerbyNet) 736 System.out.println("beetle 5524"); 737 try { 738 cs.clearWarnings(); 739 System.out.println("Callable Statement Test failed"); 740 } 741 catch(SQLException e) { } 742 743 try { 744 cs.setCursorName("ABC"); 745 System.out.println("Callable Statement Test failed"); 746 } 747 catch(SQLException e) { } 748 749 try { 750 cs.execute("create table tab3(num int)"); 751 System.out.println("Callable Statement Test failed"); 752 } 753 catch(SQLException e) { } 754 755 try { 756 cs.getResultSet(); 757 System.out.println("Callable Statement Test failed"); 758 } 759 catch(SQLException e) { } 760 761 try { 762 cs.getUpdateCount(); 763 System.out.println("Callable Statement Test failed"); 764 } 765 catch(SQLException e) { } 766 767 try { 768 cs.getMoreResults(); 769 System.out.println("Callable Statement Test failed"); 770 } 771 catch(SQLException e) { } 772 773 try { 774 cs.getResultSetType(); 775 System.out.println("Callable Statement Test failed"); 776 } 777 catch(SQLException e) { } 778 779 try { 780 cs.setFetchDirection(JDBC20Translation.FETCH_FORWARD); 781 System.out.println("Callable Statement Test failed"); 782 } 783 catch(SQLException e) { } 784 785 try { 786 cs.setFetchSize(100); 787 System.out.println("Callable Statement Test failed"); 788 } 789 catch(SQLException e) { } 790 791 try { 792 cs.getFetchSize(); 793 System.out.println("Callable Statement Test failed"); 794 } 795 catch(SQLException e) { } 796 797 try { 798 cs.getResultSetConcurrency(); 799 System.out.println("Callable Statement Test failed"); 800 } 801 catch(SQLException e) { } 802 803 try { 804 cs.addBatch(); 805 System.out.println("Callable Statement Test failed"); 806 } 807 catch(SQLException e) { } 808 809 try { 810 cs.clearBatch(); 811 System.out.println("Callable Statement Test failed"); 812 } 813 catch(SQLException e) { } 814 815 try { 816 cs.executeBatch(); 817 System.out.println("Callable Statement Test failed"); 818 } 819 catch(SQLException e) { } 820 821 try { 822 cs.getConnection(); 823 System.out.println("Callable Statement Test failed"); 824 } 825 catch(SQLException e) { } 826 } 827 catch (Throwable e) { 828 System.out.println("FAIL -- unexpected exception:" + e.toString()); 829 e.printStackTrace(System.out); 830 return; 831 } 832 System.out.println("Callable Statement test end"); 833 } 834 835 836 static private void dumpSQLExceptions (SQLException se) { 837 System.out.println("FAIL -- unexpected exception: " + se.toString()); 838 while (se != null) { 839 System.out.print("SQLSTATE("+se.getSQLState()+"):"); 840 se = se.getNextException(); 841 } 842 } 843 844 } 845 846 847 848 849 850 851 | Popular Tags |