1 21 22 package org.apache.derbyTesting.functionTests.tests.derbynet; 23 24 import java.sql.Connection ; 25 import java.sql.DriverManager ; 26 import java.sql.PreparedStatement ; 27 import java.sql.Statement ; 28 import java.sql.ResultSet ; 29 import java.math.BigDecimal ; 30 import java.sql.Date ; 31 import java.sql.Time ; 32 import java.sql.Timestamp ; 33 import java.sql.Types ; 34 import java.sql.SQLException ; 35 import java.sql.BatchUpdateException ; 36 import java.io.ByteArrayInputStream ; 37 import java.io.InputStreamReader ; 38 import org.apache.derbyTesting.functionTests.util.TestUtil; 39 import org.apache.derby.tools.ij; 40 import org.apache.derby.tools.JDBCDisplayUtil; 41 42 45 46 public class prepStmt 47 { 48 private static Connection conn = null; 49 50 private static String [] testObjects = {"table t1", "table tab1", "table t2", "table bigtab", "table tstab", 52 "table doubletab", "table numtab", "table Numeric_Tab", "table jira614", 53 "table jira614_a", "table jira428", "table jira125", "table varcharclobtab", 54 "table jira125125125125125125125125125125125125125125125125125125125125125125125125125125125125125125125", 55 "table jira1533_a", "table jira1533_b", 56 "table jira1454"}; 57 58 public static void main (String args[]) 59 { 60 try 61 { 62 System.out.println("prepStmt Test Starts"); 63 ij.getPropertyArg(args); 64 conn = ij.startJBMS(); 65 66 if (conn == null) 67 { 68 System.out.println("conn didn't work"); 69 return; 70 } 71 72 Statement cleanstmt = conn.createStatement(); 73 TestUtil.cleanUpTest(cleanstmt, testObjects); 74 75 PreparedStatement ps; 76 ResultSet rs; 77 boolean hasResultSet; 78 int uc; 79 80 System.out.println("executeUpdate() without parameters"); 82 ps = conn.prepareStatement("create table t1(c1 int, c2 int, c3 int)"); 83 uc = ps.executeUpdate(); 84 System.out.println("Update count is: " + uc); 85 86 System.out.println("executeUpdate() with parameters"); 88 ps = conn.prepareStatement("insert into t1 values (?, 5, ?)"); 89 ps.setInt(1, 99); 90 ps.setInt(2, 9); 91 uc = ps.executeUpdate(); 92 System.out.println("Update count is: " + uc); 93 94 System.out.println("execute() with parameters, no result set returned"); 96 ps = conn.prepareStatement("insert into t1 values (2, 6, ?), (?, 5, 8)"); 97 ps.setInt(1, 10); 98 ps.setInt(2, 7); 99 hasResultSet = ps.execute(); 100 while (hasResultSet) 101 { 102 rs = ps.getResultSet(); 103 while (rs.next()) 104 System.out.println("ERROR: should not get here!"); 105 hasResultSet = ps.getMoreResults(); 106 } 107 uc = ps.getUpdateCount(); 108 if (uc != -1) 109 System.out.println("Update count is: " + uc); 110 111 System.out.println("executQuery() without parameters"); 113 ps = conn.prepareStatement("select * from t1"); 114 rs = ps.executeQuery(); 115 while (rs.next()) 116 System.out.println("got row: "+" "+rs.getInt(1)+" "+rs.getInt(2)+" "+rs.getInt(3)); 117 System.out.println("end of rows"); 118 119 System.out.println("executQuery() with parameters"); 121 ps = conn.prepareStatement("select * from t1 where c2 = ?"); 122 ps.setInt(1, 5); 123 rs = ps.executeQuery(); 124 while (rs.next()) 125 System.out.println("got row: "+" "+rs.getInt(1)+" "+rs.getInt(2)+" "+rs.getInt(3)); 126 System.out.println("end of rows"); 127 128 System.out.println("execute() with parameters with result set returned"); 130 ps = conn.prepareStatement("select * from t1 where c2 = ?"); 131 ps.setInt(1, 5); 132 hasResultSet = ps.execute(); 133 while (hasResultSet) 134 { 135 rs = ps.getResultSet(); 136 while (rs.next()) 137 System.out.println("got row: "+" "+rs.getInt(1)+" "+rs.getInt(2)+" "+rs.getInt(3)); 138 hasResultSet = ps.getMoreResults(); 139 } 140 System.out.println("end of rows"); 141 uc = ps.getUpdateCount(); 142 if (uc != -1) 143 System.out.println("Update count is: " + uc); 144 145 System.out.println("test different data types for input parameters of a Prepared Statement"); 147 ps = conn.prepareStatement("create table t2(si smallint,i int, bi bigint, r real, f float, d double precision, n5_2 numeric(5,2), dec10_3 decimal(10,3), ch20 char(20),vc varchar(20), lvc long varchar,b20 char(23) for bit data, vb varchar(23) for bit data, lvb long varchar for bit data, dt date, tm time, ts timestamp not null)"); 148 uc = ps.executeUpdate(); 149 System.out.println("Update count is: " + uc); 150 151 byte[] ba = new byte[] {0x00,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0xa,0xb,0xc, 153 0xd,0xe,0xf,0x10,0x11,0x12,0x13 }; 154 155 ps = conn.prepareStatement("insert into t2 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,? , ?)"); 156 ps.setShort(1, (short) 1); 157 ps.setInt(2, 2); 158 ps.setLong(3, 3); 159 ps.setFloat(4, (float) 4.0); 160 ps.setDouble(5, 5.0); 161 ps.setDouble(6, 6.0); 162 ps.setBigDecimal(7, new BigDecimal ("77.77")); 163 ps.setBigDecimal(8, new BigDecimal ("8.1")); 164 ps.setString(9, "column9string"); 165 byte[] c10ba = new String ("column10vcstring").getBytes("UTF-8"); 166 int len = c10ba.length; 167 ps.setAsciiStream(10, new ByteArrayInputStream (c10ba), len); 168 byte[] c11ba = new String ("column11lvcstring").getBytes("UTF-8"); 169 len = c11ba.length; 170 ps.setCharacterStream(11, new InputStreamReader (new ByteArrayInputStream (c11ba),"UTF-8"),len); 171 ps.setBytes(12,ba); 172 ps.setBytes(13,ba); 176 ps.setBytes(14,ba); 177 ps.setDate(15, Date.valueOf("2002-04-12")); 178 ps.setTime(16, Time.valueOf("11:44:30")); 179 ps.setTimestamp(17, Timestamp.valueOf("2002-04-12 11:44:30.000000000")); 180 uc = ps.executeUpdate(); 181 System.out.println("Update count is: " + uc); 182 183 System.out.println("test setObject on different data types for input parameters of a Prepared Statement"); 186 ps = conn.prepareStatement("insert into t2 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,? , ?)"); 187 ps.setObject(1, new Integer (1)); 188 ps.setObject(2, new Integer (2)); 189 ps.setObject(3, new Long (3)); 190 ps.setObject(4, new Float (4.0)); 191 ps.setObject(5, new Double (5.0)); 192 ps.setObject(6, new Double (6.0)); 193 ps.setObject(7, new BigDecimal ("77.77")); 194 ps.setObject(8, new BigDecimal ("8.1")); 195 ps.setObject(9, "column11string"); 196 ps.setObject(10, "column10vcstring"); 197 ps.setObject(11, "column11lvcstring"); 198 ps.setObject(12,ba); 199 ps.setObject(13,ba); 200 ps.setObject(14,ba); 201 ps.setObject(15, Date.valueOf("2002-04-12")); 202 ps.setObject(16, Time.valueOf("11:44:30")); 203 ps.setObject(17, Timestamp.valueOf("2002-04-12 11:44:30.000000000")); 204 uc = ps.executeUpdate(); 205 System.out.println("Update count is: " + uc); 206 207 System.out.println("test setNull on different data types for input parameters of a Prepared Statement"); 209 ps = conn.prepareStatement("insert into t2 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,? , ?)"); 210 ps.setNull(1, java.sql.Types.SMALLINT); 211 ps.setNull(2, java.sql.Types.INTEGER); 212 ps.setNull(3, java.sql.Types.BIGINT); 213 ps.setNull(4, java.sql.Types.REAL); 214 ps.setNull(5, java.sql.Types.FLOAT); 215 ps.setNull(6, java.sql.Types.DOUBLE); 216 ps.setNull(7, java.sql.Types.NUMERIC); 217 ps.setNull(8, java.sql.Types.DECIMAL); 218 ps.setNull(9, java.sql.Types.CHAR); 219 ps.setNull(10, java.sql.Types.VARCHAR); 220 ps.setNull(11, java.sql.Types.LONGVARCHAR); 221 ps.setNull(12, java.sql.Types.BINARY); 222 ps.setNull(13, java.sql.Types.VARBINARY); 223 ps.setNull(14, java.sql.Types.LONGVARBINARY); 224 ps.setNull(15, java.sql.Types.DATE); 225 ps.setNull(16, java.sql.Types.TIME); 226 227 ps.setTimestamp(17, Timestamp.valueOf("2002-04-12 11:44:31.000000000")); hasResultSet = ps.execute(); 229 uc = ps.getUpdateCount(); 230 if (uc != -1) 231 System.out.println("Update count is: " + uc); 232 233 ps = conn.prepareStatement("select * from t2"); 234 rs = ps.executeQuery(); 235 while (rs.next()) 236 { 237 System.out.println("got row: "+" "+rs.getShort(1)+ 238 " "+rs.getInt(2)+" "+rs.getLong(3)+ 239 " "+rs.getFloat(4)+" "+rs.getDouble(5)+ 240 " "+rs.getDouble(6)+" "+rs.getBigDecimal(7)+ 241 " "+rs.getBigDecimal(8)+" "+rs.getString(9)+ 242 " "+rs.getString(10)+" "+rs.getString(11)+ 243 " "+bytesToString(rs.getBytes(12)) + 244 " "+bytesToString(rs.getBytes(13)) + 245 " "+bytesToString(rs.getBytes(14)) + 246 " "+rs.getDate(15)+ 247 " "+rs.getTime(16)+" "+rs.getTimestamp(17)); 248 Timestamp ts = rs.getTimestamp(17); 249 Timestamp temp = Timestamp.valueOf("2002-04-12 11:44:30.000000000"); 250 if (ts.after(temp)) 251 System.out.println("After first Timestamp!"); 252 else if (ts.before(temp)) 253 System.out.println("Before first Timestamp!"); 254 else 255 System.out.println("Timestamp match!"); 256 } 257 System.out.println("end of rows"); 258 259 try { 260 ps = conn.prepareStatement("select * from t2 where i = ?"); 261 rs = ps.executeQuery(); 262 } 263 catch (SQLException e) { 264 System.out.println("SQLState: " + e.getSQLState() + " message: " + e.getMessage()); 265 } 266 try { 267 ps = conn.prepareStatement("insert into t2 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); 268 ps.executeUpdate(); 269 } 270 catch (SQLException e) { 271 System.out.println("SQLState: " + e.getSQLState() + " message: " + e.getMessage()); 272 } 273 try { 274 int tabSize = 1000; 275 String createBigTabSql = "create table bigtab ("; 276 for (int i = 1; i <= tabSize; i++) 277 { 278 createBigTabSql += "c"+ i + " int"; 279 if (i != tabSize) 280 createBigTabSql += ", "; 281 else 282 createBigTabSql += " )"; 283 } 284 ps = conn.prepareStatement(createBigTabSql); 286 uc = ps.executeUpdate(); 287 288 insertTab(conn, "bigtab",50); 289 insertTab(conn, "bigtab",200); 290 insertTab(conn, "bigtab", 300); 291 insertTab(conn, "bigtab",500); 292 insertTab(conn, "bigtab", 1000); 294 selectFromBigTab(conn); 295 System.out.println("Insert wrong column name"); 297 insertTab(conn, "bigtab", 1001); 298 System.out.println("Expected Syntax error "); 300 insertTab(conn, "bigtab", 0); 301 System.out.println("Expected Table does not exist "); 303 insertTab(conn, "wrongtab",1000); 304 } 305 catch (SQLException e) { 306 System.out.println("SQLState: " + e.getSQLState() + 307 " message: " + e.getMessage()); 308 } 309 rs.close(); 310 ps.close(); 311 312 testBigDecimalSetObject(conn); 313 testBigDecimalSetObjectWithScale(conn); 314 315 if (!TestUtil.isJCCFramework()) { 316 testVaryingClientParameterTypeBatch(conn); 317 } 318 319 test4975(conn); 320 test5130(conn); 321 test5172(conn); 322 jira614Test(conn); 323 jira614Test_a(conn); 324 jira170Test(conn); 325 jira125Test(conn); 326 jira428Test(conn); 327 jira1454Test(conn); 328 jira1533Test_a(conn); 329 jira1533Test_b(conn); 330 conn.close(); 331 conn = ij.startJBMS(); 333 cleanstmt = conn.createStatement(); 334 TestUtil.cleanUpTest(cleanstmt, testObjects); 335 cleanstmt.close(); 336 conn.close(); 337 System.out.println("prepStmt Test Ends"); 338 } 339 catch (Exception e) 340 { 341 e.printStackTrace(); 342 } 343 } 344 345 private static void testVaryingClientParameterTypeBatch(Connection conn) throws Exception 348 { 349 Statement stmt = conn.createStatement(); 350 351 try { stmt.execute("drop table varcharclobtab"); } catch (Throwable t) { } 352 stmt.execute("create table varcharclobtab (c1 varchar(100), c2 clob)"); 353 stmt.close(); 354 355 PreparedStatement pStmt = conn.prepareStatement("insert into varcharclobtab VALUES(?,?)"); 356 357 pStmt.setNull(1, Types.VARCHAR); 358 pStmt.setString(2, "clob"); 359 pStmt.addBatch(); 360 361 pStmt.setString(1, "varchar"); 362 pStmt.setNull(2, Types.CLOB); 363 pStmt.addBatch(); 364 365 try { 367 pStmt.executeBatch(); 368 } catch (ClassCastException e) { 369 System.out.println("FAIL: ClassCastException thrown by testVaryingClientParameterTypeBatch test."); 370 throw e; 371 } 372 pStmt.close(); 373 } 374 375 private static void test5130 (Connection conn) throws Exception 378 { 379 int numOfPreparedStatement = 500; 380 381 PreparedStatement [] tempPreparedStatement = new 382 PreparedStatement [numOfPreparedStatement]; 383 ResultSet rs; 384 String [] tableName = new String [numOfPreparedStatement]; 385 for (int i = 0; i < numOfPreparedStatement; i++) 386 { 387 tempPreparedStatement[i] = conn.prepareStatement( 388 "SELECT COUNT(*) from SYS.SYSTABLES", 389 ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); 390 rs = tempPreparedStatement[i].executeQuery(); 391 rs.close(); 392 } 393 for (int i = 0; i < numOfPreparedStatement; i++) 394 tempPreparedStatement[i].close(); 395 396 } 397 398 private static void test5172(Connection conn) throws Exception 399 { 400 401 Statement stmt = conn.createStatement(); 402 403 try { 404 stmt.executeUpdate("drop table tab1"); 405 } 406 catch (SQLException se) 407 { 408 } 409 410 stmt.executeUpdate( "CREATE TABLE TSTAB (I int, STATUS_TS Timestamp, PROPERTY_TS Timestamp)" ); 411 stmt.executeUpdate("INSERT INTO TSTAB VALUES(1 , '2003-08-15 21:20:00','2003-08-15 21:20:00')"); 412 stmt.executeUpdate("INSERT INTO TSTAB VALUES(2 , '1969-12-31 16:00:00.0', '2003-08-15 21:20:00')"); 413 414 stmt.close(); 415 416 String timestamp = "20"; 417 String query = "select STATUS_TS " + 418 "from TSTAB " + 419 "where (STATUS_TS >= ? or " + 420 " PROPERTY_TS<?)"; 421 422 System.out.println("Negative test setString with Invalid Timestamp:" + timestamp); 423 424 PreparedStatement ps = conn.prepareStatement(query); 425 ps.setString(1,timestamp); 426 ps.setString(2, timestamp ); 427 try { 428 ResultSet rs = ps.executeQuery(); 429 } 430 catch (SQLException e) { 431 System.out.println("SQLState: " + e.getSQLState() + " message: " + e.getMessage()); 432 } 433 434 } 435 436 437 private static void test4975(Connection conn) throws Exception 438 { 439 BigDecimal minBigDecimalVal = null; 440 BigDecimal rBigDecimalVal = null; 441 String sminBigDecimalVal = null; 442 443 PreparedStatement pstmt = null; 444 ResultSet rs = null; 445 Statement stmt = null; 446 447 try 448 { 449 stmt = conn.createStatement(); 450 String createTableSQL = "create table Numeric_Tab (MAX_VAL NUMERIC(30,15), MIN_VAL NUMERIC(30,15), NULL_VAL NUMERIC(30,15) DEFAULT NULL)"; 451 stmt.executeUpdate(createTableSQL); 453 454 String insertSQL = "insert into Numeric_Tab values(999999999999999,0.000000000000001, null)"; 455 stmt.executeUpdate(insertSQL); 456 457 sminBigDecimalVal = "0.000000000000001"; 459 minBigDecimalVal = new BigDecimal (sminBigDecimalVal); 460 logMsg("Minimum BigDecimal Value: " + minBigDecimalVal); 461 462 String sPrepStmt = "update Numeric_Tab set NULL_VAL=?"; 464 465 logMsg("Prepared Statement String: " + sPrepStmt); 468 469 pstmt = conn.prepareStatement(sPrepStmt); 471 pstmt.setObject(1,minBigDecimalVal); 472 pstmt.executeUpdate(); 473 474 String Null_Val_Query = "Select NULL_VAL from Numeric_Tab"; 477 logMsg(Null_Val_Query); 478 rs = stmt.executeQuery(Null_Val_Query); 479 rs.next(); 480 481 rBigDecimalVal = (BigDecimal ) rs.getObject(1); 482 logMsg("Returned BigDecimal Value after Updation: " + rBigDecimalVal); 483 logMsg("Value returned from stmt: " + minBigDecimalVal); 484 485 if(rBigDecimalVal.compareTo(minBigDecimalVal) == 0) 486 { 487 logMsg("setObject Method sets the designated parameter with the Object"); 488 } 489 else 490 { 491 logErr("setObject Method does not set the designated parameter with the Object"); 492 throw new Exception ("Call to setObject Method is Failed!"); 493 } 494 } 495 catch(SQLException sqle) 496 { 497 logErr("SQL Exception: " + sqle.getMessage()); 498 throw new Exception ("Call to setObject is Failed!"); 499 } 500 catch(Exception e) 501 { 502 logErr("Unexpected Exception: " + e.getMessage()); 503 throw new Exception ("Call to setObject is Failed!"); 504 } 505 506 finally 507 { 508 try 509 { 510 if(rs != null) 511 { 512 rs.close(); 513 rs = null; 514 } 515 if(pstmt != null) 516 { 517 pstmt.close(); 518 pstmt = null; 519 } 520 stmt.executeUpdate("drop table Numeric_Tab"); 521 if(stmt != null) 522 { 523 stmt.close(); 524 stmt = null; 525 } 526 } 527 catch(Exception e){ } 528 } 529 } 530 531 private static void logErr(String s) 532 { 533 System.err.println(s); 534 } 535 536 private static void logMsg(String s) 537 { 538 System.out.println(s); 539 } 540 541 private static void insertTab(Connection conn, String tabname , int numCols) throws SQLException 542 { 543 PreparedStatement ps = null; 544 System.out.println("insertTab ( " + tabname + "," + numCols + ")" ); 545 String insertSql = "insert into " + tabname + "("; 546 for (int i = 1; i <= numCols; i++) 547 { 548 insertSql += " c"+ i; 549 if (i != numCols) 550 insertSql += ", "; 551 else 552 insertSql += ")"; 553 } 554 insertSql += " values ("; 555 for (int i = 1; i <= numCols; i++) 556 { 557 insertSql += "?"; 558 if (i != numCols) 559 insertSql += ", "; 560 else 561 insertSql += " )"; 562 } 563 564 try { 565 ps = conn.prepareStatement(insertSql); 566 567 for (int i = 1; i <= numCols; i++) 568 ps.setInt(i,i); 569 ps.executeUpdate(); 570 } catch (SQLException e) 571 { 572 System.out.println("SQLState: " + e.getSQLState() + 573 " message: " + e.getMessage()); 574 } 575 576 } 577 578 private static void selectFromBigTab(Connection conn) throws SQLException 579 { 580 PreparedStatement ps = null; 581 ResultSet rs = null; 582 583 String selectSQL = "select * from bigtab"; 584 System.out.println(selectSQL); 585 ps = conn.prepareStatement(selectSQL); 586 rs = ps.executeQuery(); 587 while (rs.next()) 588 { 589 System.out.println("Col # 500 = " + rs.getObject(500) + 590 " Col 1000 = " + rs.getObject(1000)); 591 } 592 593 rs.close(); 594 ps.close(); 595 596 } 597 private static void testBigDecimalSetObject(Connection conn) throws SQLException 598 { 599 setupDoubleTab(conn); 600 testBigDecimalToDoubleConversion(conn); 601 } 602 603 604 605 private static void setupDoubleTab(Connection conn) throws SQLException 606 { 607 String sql; 608 Statement stmt = conn.createStatement(); 609 try { 610 stmt.executeUpdate("DROP TABLE doubletab"); 611 } 612 catch (SQLException se) 613 { 614 616 } 617 618 sql = "CREATE TABLE doubletab (i int, doubleVal DOUBLE)"; 619 620 System.out.println(sql); 621 stmt.executeUpdate(sql); 622 conn.commit(); 623 624 } 625 626 private static void testBigDecimalToDoubleConversion(Connection conn) throws SQLException 627 { 628 System.out.println("\n\ntestBigDecimalToDoubleConversion()."); 629 System.out.println(" Check that values are preserved when BigDecimal \n values which have more than 31 digits are converted \n to Double with setObject"); 630 ResultSet rs = null; 631 double[] doubleVals = {1.0E-130,1.0E125, 0, -1.0E124}; 633 BigDecimal [] bigDecimalVals = { new BigDecimal (1.0E-130), 635 new BigDecimal (1.0E125), 636 new BigDecimal (-1.0E124) , 637 new 638 BigDecimal ("12345678901234567890123456789012"), 639 new BigDecimal ("1.2345678901234567890123456789012") 640 }; 641 642 String isql = "INSERT INTO doubletab VALUES (?, ?)"; 643 PreparedStatement insPs = conn.prepareStatement(isql); 645 String ssql = "SELECT doubleVal FROM doubletab"; 646 PreparedStatement selPs = conn.prepareStatement(ssql); 647 String dsql = "DELETE FROM doubletab"; 648 PreparedStatement delPs = conn.prepareStatement(dsql); 649 for (int i = 0; i < bigDecimalVals.length; i ++) 650 { 651 BigDecimal bd = bigDecimalVals[i]; 652 insPs.setInt(1,i); 655 insPs.setObject(2,bd,java.sql.Types.DOUBLE); 656 insPs.executeUpdate(); 657 rs = selPs.executeQuery(); 659 rs.next(); 660 checkDoubleMatch(bd.doubleValue() , rs.getDouble(1)); 661 delPs.executeUpdate(); 663 } 664 insPs.close(); 665 selPs.close(); 666 delPs.close(); 667 rs.close(); 668 conn.commit(); 669 } 670 671 static void testBigDecimalSetObjectWithScale(Connection conn) throws Exception 672 { 673 Statement stmt = conn.createStatement(); 674 String sql = null; 675 676 System.out.println("\n\ntestBigDecimalSetObjectWithScale(). \nPass scale parameter of setObject"); 677 678 try { 679 stmt.executeUpdate("DROP TABLE numtab"); 680 } 681 catch (SQLException se) 682 { 683 } 685 sql = "CREATE TABLE numtab (num NUMERIC(10,6))"; 686 stmt.executeUpdate(sql); 687 688 BigDecimal bdFromString = new BigDecimal ("2.33333333"); 690 691 sql = "INSERT INTO numtab VALUES(?)"; 694 PreparedStatement ps = conn.prepareStatement(sql); 695 int scale = 2; 698 ps.setObject(1,bdFromString,java.sql.Types.DECIMAL,scale); 699 ps.executeUpdate(); 700 sql = "SELECT num FROM numtab"; 702 ResultSet rs = stmt.executeQuery(sql); 703 rs.next(); 704 checkBigDecimalMatch(bdFromString.setScale(scale, 706 BigDecimal.ROUND_DOWN), 707 (BigDecimal )rs.getObject(1)); 708 rs.close(); 709 ps.close(); 710 stmt.close(); 711 712 conn.commit(); 713 } 714 715 private static void checkDoubleMatch(double expectedValue, double 716 actualValue) { 717 if (actualValue == expectedValue) 718 System.out.println("PASS: Actual value " + actualValue + " matches expected value: " + expectedValue); 719 else 720 new Exception ("FAIL: Actual value: " + actualValue + 721 " does not match expected value:" + 722 expectedValue).printStackTrace(); 723 724 } 725 726 private static void checkBigDecimalMatch(BigDecimal expectedValue, 727 BigDecimal actualValue) { 728 if (actualValue == expectedValue || 729 (actualValue.compareTo(expectedValue) == 0)) 730 System.out.println("PASS: Actual value " + actualValue + " matches expected value: " + expectedValue); 731 else 732 new Exception ("FAIL: Actual value: " + actualValue + 733 " does not match expected value:" + 734 expectedValue).printStackTrace(); 735 736 } 737 738 private static String bytesToString(byte[] ba) 739 { 740 String s = null; 741 if (ba == null) 742 return s; 743 s = new String (); 744 for (int i = 0; i < ba.length; i++) 745 s += (Integer.toHexString(ba[i] & 0x00ff)); 746 return s; 747 } 748 749 750 private static void jira614Test(Connection conn) 757 throws Exception 758 { 759 Statement stmt = conn.createStatement(); 760 PreparedStatement ps ; 761 try { 762 stmt.execute("drop table jira614"); 763 } catch (Throwable t) { } 764 ps = conn.prepareStatement( 765 "create table jira614 (c1 varchar(10000))"); 766 ps.executeUpdate(); 767 String workString = genString("a", 150); 768 ps = conn.prepareStatement("insert into jira614 values (?)"); 769 ps.setString(1, workString); 770 for (int row = 0; row < 210; row++) 771 ps.executeUpdate(); 772 workString = genString("b", 10000); 773 ps.setString(1, workString); 774 ps.executeUpdate(); 775 ps = conn.prepareStatement("select * from jira614"); 776 ResultSet rs = ps.executeQuery(); 777 778 int rowNum = 0; 779 while (rs.next()) 780 { 781 rowNum++; 782 if (rowNum == 26) 783 break; 784 } 785 rs.close(); System.out.println("Test jira614 completed successfully -- no Distributed Protocol Exception occurred"); 787 } 788 private static String genString(String c, int howMany) 789 { 790 StringBuffer buf = new StringBuffer (); 791 for (int i = 0; i < howMany; i++) 792 buf.append(c); 793 return buf.toString(); 794 } 795 private static void jira614Test_a(Connection conn) 803 throws Exception 804 { 805 Statement stmt = conn.createStatement(); 807 PreparedStatement ps ; 808 try { stmt.execute("drop table jira614_a"); } catch (Throwable t) { } 809 stmt.execute("create table jira614_a (c1 int)"); 810 ps = conn.prepareStatement("insert into jira614_a values (?)"); 811 for (int row = 1; row <= 5; row++) 812 { 813 ps.setInt(1, row); 814 ps.executeUpdate(); 815 } 816 817 ResultSet rs = stmt.executeQuery("select * from jira614"); 819 int rowNum = 0; 820 while (rs.next()) 821 { 822 rowNum++; 823 if (rowNum == 26) 824 break; 825 } 826 stmt.close(); 828 stmt = conn.createStatement(); 829 rs = stmt.executeQuery("select * from jira614_a"); 830 while (rs.next()); 831 ps.close(); 832 rs.close(); 833 stmt.close(); 834 } 835 private static void jira170Test(Connection conn) 840 throws Exception 841 { 842 Statement stmt = conn.createStatement(); 843 PreparedStatement ps = null ; 844 try { 845 stmt.execute("drop table jira170"); 846 } catch (Throwable t) { } 847 char []cData = new char[1000000]; 849 for (int i = 0; i < cData.length; i++) 850 cData[i] = Character.forDigit(i%10, 10); 851 try { 864 ps = conn.prepareStatement("insert into jira170 values (?)"); 865 ps.setString(1, new String (cData)); 866 ps.execute(); 867 System.out.println("Test Jira170 failed: no exception when trying to execute a failed prepare with an enormous parameter"); 868 } 869 catch (SQLException e) 870 { 871 if (e.getSQLState().equals("42X05")) 872 System.out.println("Jira170: caught expected table not found"); 873 else 874 e.printStackTrace(); 875 } 876 } 877 890 private static void jira1454Test(Connection conn) 891 throws Exception 892 { 893 tickleDSSLength(conn, 12748); 894 tickleDSSLength(conn, 12750); 895 tickleDSSLength(conn, 12749); 896 } 897 private static void tickleDSSLength(Connection conn, int c2Len) 898 throws Exception 899 { 900 System.out.println("JIRA-1454 repro with c2 len=" + c2Len); 901 Statement st = conn.createStatement(); 902 903 try { 904 st.execute("drop table jira1454"); 905 } catch (SQLException se) {} 906 st.execute( 907 "create table jira1454(c1 varchar(20000),c2 varchar(30000))"); 908 909 char [] c1 = new char[20000]; 910 for (int i = 0; i < c1.length; i++) 911 c1[i] = Character.forDigit(i%10, 10); 912 char [] c2 = new char[30000]; 913 for (int i = 0; i < c2Len; i++) 914 c2[i] = Character.forDigit(i%10, 10); 915 916 PreparedStatement pSt = 917 conn.prepareStatement("insert into jira1454 values (?,?)"); 918 pSt.setString(1, new String (c1)); 919 pSt.setString(2, new String (c2,0, c2Len)); 920 921 pSt.execute(); 922 pSt.close(); 923 ResultSet rs = st.executeQuery("select * from jira1454"); 924 while (rs.next()) 925 System.out.println("Fetched a row, c2.length=" + 926 rs.getString("c2").length()); 927 rs.close(); 928 st.close(); 929 } 930 955 956 private static void jira125Test(Connection conn) 957 throws Exception 958 { 959 jira125Test_a(conn); 960 jira125Test_b(conn); 961 } 962 963 private static void jira125Test_b(Connection conn) 964 throws Exception 965 { 966 Statement stmt = conn.createStatement(); 967 PreparedStatement ps ; 968 try { 969 stmt.execute("drop table jira125"); 970 } catch (Throwable t) { } 971 try { 972 stmt.execute("create table jira125 (id integer)"); 973 stmt.execute("insert into jira125 values 1, 2, 3"); 974 } catch (Throwable t) { } 975 StringBuffer buf = new StringBuffer (); 976 buf.append("SELECT id FROM jira125 WHERE id IN ( "); 977 978 int nCols = 556; 983 for (int i = 0; i < nCols; i++) buf.append("?,"); 984 buf.append("?)"); 985 ps = conn.prepareStatement(buf.toString()); 986 for (int i = 0; i <= nCols; i++) ps.setInt(i+1, 1); 988 ResultSet rs = ps.executeQuery(); 989 while (rs.next()); 990 System.out.println("Test jira125 successful: " + (nCols + 1) + 991 " parameter markers successfully prepared and executed."); 992 } 993 994 private static void jira125Test_a(Connection conn) 995 throws Exception 996 { 997 Statement stmt = conn.createStatement(); 998 999 StringBuffer id = new StringBuffer (); 1005 for (int i = 0; i < 49; i++) 1006 id.append("id"); 1007 id.append("i"); 1008 1009 StringBuffer tabName = new StringBuffer ("jira"); 1015 for (int i = 0; i < 31; i++) 1016 tabName.append("125"); 1017 1018 try { 1019 stmt.execute("drop table " + tabName.toString()); 1020 } catch (Throwable t) { } 1021 try { 1022 stmt.execute("create table " + tabName.toString() + " (" + 1023 id.toString() + " integer)"); 1024 stmt.execute("insert into " + tabName.toString() + " values 1, 2, 3"); 1025 } catch (Throwable t) { } 1026 1027 PreparedStatement ps; 1028 StringBuffer buf = new StringBuffer (); 1029 buf.append("SELECT " + id.toString() + " FROM " + 1030 tabName.toString() + " WHERE " + id.toString() + " IN ( "); 1031 1032 int nCols = 554; 1037 for (int i = 0; i < nCols; i++) buf.append("?,"); 1038 buf.append("?)"); 1039 ps = conn.prepareStatement(buf.toString()); 1040 for (int i = 0; i <= nCols; i++) ps.setInt(i+1, 1); 1042 ResultSet rs = ps.executeQuery(); 1043 while (rs.next()); 1044 System.out.println("Iteration 1 successful: " + (nCols + 1) + 1045 " parameter markers successfully prepared and executed."); 1046 } 1047 private static void jira1533Test_a(Connection conn) 1056 throws Exception 1057 { 1058 Statement stmt = conn.createStatement(); 1059 PreparedStatement ps ; 1060 try { stmt.execute("drop table jira1533_a"); } catch (Throwable t) { } 1061 stmt.execute("create table jira1533_a (aa BIGINT NOT NULL, "+ 1062 "bbbbbb BIGINT DEFAULT 0 NOT NULL,"+ 1063 " cccc VARCHAR(40), ddddddddddd BIGINT, eeeeee VARCHAR(128),"+ 1064 " ffffffffffffffffff VARCHAR(128),"+ 1065 "ggggggggg BLOB(2G), hhhhhhhhh VARCHAR(128), "+ 1066 "iiiiiiii VARCHAR(128), jjjjjjjjjjjjjj BIGINT,"+ 1067 "kkkkkkkk CHAR(1) DEFAULT 'f', "+ 1068 "llllllll CHAR(1) DEFAULT 'f', "+ 1069 "mmmmmmmmmmmmm CHAR(1) DEFAULT 'f')"); 1070 stmt.close(); 1071 ps = conn.prepareStatement( 1072 "INSERT INTO jira1533_a (aa,bbbbbb,cccc,ddddddddddd,eeeeee,"+ 1073 " ffffffffffffffffff,"+ 1074 "ggggggggg,hhhhhhhhh,iiiiiiii,jjjjjjjjjjjjjj,kkkkkkkk,"+ 1075 "llllllll,mmmmmmmmmmmmm)"+ 1076 " VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)"); 1077 String blobStr = makeString(32584); 1078 ps.setLong(1,5); 1079 ps.setLong(2,1); 1080 ps.setString(3,"AAAAAAAAAAA"); 1081 ps.setLong(4,30000); 1082 ps.setString(5,"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); 1083 ps.setString(6,"AAAAAAAAAAA"); 1084 ps.setBytes(7,blobStr.getBytes()); 1085 ps.setString(8,"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); 1086 ps.setString(9,"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); 1087 ps.setLong(10,1); 1088 ps.setString(11,"1"); 1089 ps.setString(12,"1"); 1090 ps.setString(13,"1"); 1091 ps.execute(); 1092 ps.close(); 1093 System.out.println("JIRA Test 1533(a) successful (no exception)"); 1094 } 1095 private static void jira1533Test_b(Connection conn) 1096 throws Exception 1097 { 1098 Statement stmt = conn.createStatement(); 1099 PreparedStatement ps ; 1100 try { stmt.execute("drop table jira1533_b"); } catch (Throwable t) { } 1101 stmt.execute("create table jira1533_b (aa BIGINT NOT NULL, "+ 1102 "bbbbbb BIGINT DEFAULT 0 NOT NULL,"+ 1103 " cccc VARCHAR(40), ddddddddddd BIGINT, eeeeee VARCHAR(128),"+ 1104 " ffffffffffffffffff VARCHAR(128),"+ 1105 "g1 BLOB(2G),g2 BLOB(2G),g3 BLOB(2G),g4 BLOB(2G),"+ 1106 "ggggggggg BLOB(2G), hhhhhhhhh VARCHAR(128), "+ 1107 "iiiiiiii VARCHAR(128), jjjjjjjjjjjjjj BIGINT,"+ 1108 "kkkkkkkk CHAR(1) DEFAULT 'f', "+ 1109 "llllllll CHAR(1) DEFAULT 'f', "+ 1110 "mmmmmmmmmmmmm CHAR(1) DEFAULT 'f')"); 1111 stmt.close(); 1112 ps = conn.prepareStatement( 1113 "INSERT INTO jira1533_b (aa,bbbbbb,cccc,ddddddddddd,eeeeee,"+ 1114 " ffffffffffffffffff,"+ 1115 "g1,g2,g3,g4,"+ 1116 "ggggggggg,hhhhhhhhh,iiiiiiii,jjjjjjjjjjjjjj,kkkkkkkk,"+ 1117 "llllllll,mmmmmmmmmmmmm)"+ 1118 " VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); 1119 String blobStr = makeString(32584); 1120 ps.setLong(1,5); 1121 ps.setLong(2,1); 1122 ps.setString(3,"AAAAAAAAAAA"); 1123 ps.setLong(4,30000); 1124 ps.setString(5,"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); 1125 ps.setString(6,"AAAAAAAAAAA"); 1126 ps.setBytes(7,blobStr.getBytes()); 1127 ps.setBytes(8,blobStr.getBytes()); 1128 ps.setBytes(9,blobStr.getBytes()); 1129 ps.setBytes(10,blobStr.getBytes()); 1130 ps.setBytes(11,blobStr.getBytes()); 1131 ps.setString(12,"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); 1132 ps.setString(13,"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); 1133 ps.setLong(14,1); 1134 ps.setString(15,"1"); 1135 ps.setString(16,"1"); 1136 ps.setString(17,"1"); 1137 ps.execute(); 1138 ps.close(); 1139 System.out.println("JIRA Test 1533(b) successful (no exception)"); 1140 } 1141 private static String makeString(int length) 1142 { 1143 StringBuffer buf = new StringBuffer (); 1144 for (int i = 0; i < length; i++) 1145 buf.append("X"); 1146 return buf.toString(); 1147 } 1148 private static void jira428Test(Connection conn) 1159 throws Exception 1160 { 1161 Statement stmt = conn.createStatement(); 1162 PreparedStatement ps ; 1163 try { stmt.execute("drop table jira428"); } catch (Throwable t) { } 1164 stmt.execute("create table jira428 (i integer)"); 1165 boolean savedAutoCommit = conn.getAutoCommit(); 1166 conn.setAutoCommit(false); 1167 ps = conn.prepareStatement("insert into jira428 values (?)"); 1168 for (int i = 0; i < 32765; i++) 1169 { 1170 ps.setInt(1, i); 1171 ps.addBatch(); 1172 } 1173 ps.executeBatch(); 1174 conn.commit(); 1175 if (TestUtil.isDerbyNetClientFramework()) 1180 { 1181 ps = conn.prepareStatement("insert into jira428 values (?)"); 1182 for (int i = 0; i < 100000; i++) 1183 { 1184 ps.setInt(1, i); 1185 ps.addBatch(); 1186 } 1187 try { 1188 ps.executeBatch(); 1189 System.out.println("JIRA428 FAILURE: expected an exception saying no more than 65534 statements in a single batch"); 1190 } 1191 catch (BatchUpdateException bue) 1192 { 1193 } 1198 conn.commit(); 1199 } 1200 conn.setAutoCommit(savedAutoCommit); 1201 } 1202} 1203 | Popular Tags |