KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbcapi > resultset


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.resultset
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.jdbcapi;
23
24 import java.sql.CallableStatement JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.Date JavaDoc;
27 import java.sql.DriverManager JavaDoc;
28 import java.sql.ResultSetMetaData JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31 import java.sql.Statement JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import java.sql.Time JavaDoc;
34 import java.sql.Timestamp JavaDoc;
35 import java.sql.Types JavaDoc;
36
37
38 import java.lang.reflect.*;
39
40 import org.apache.derby.tools.ij;
41 import org.apache.derbyTesting.functionTests.util.SecurityCheck;
42 import org.apache.derbyTesting.functionTests.util.TestUtil;
43 import org.apache.derbyTesting.functionTests.util.JDBCTestDisplayUtil;
44 import org.apache.derby.iapi.reference.JDBC30Translation;
45 import org.apache.derby.iapi.reference.SQLState;
46 import org.apache.derbyTesting.functionTests.util.BigDecimalHandler;
47
48 /**
49  * Test of JDBC result set and result set meta-data.
50  * This program simply calls each of the result set and result set meta-data
51  * methods, one by one, and prints the results. The test passes if the printed
52  * results match a previously stored "master". Thus this test cannot actually
53  * discern whether it passes or not.
54  *
55  * Test is only touching on known result set hot-spots at present.
56
57  * Performs SecurityCheck analysis on the JDBC ResultSet and
58  * ResultSetMetaData objects returned.
59  *
60  * @see org.apache.derbyTesting.functionTests.util.SecurityCheck
61  * @author ames
62  */

63
64 public class resultset {
65
66   private static Class JavaDoc[] CONN_PARAM = { Integer.TYPE };
67   private static Object JavaDoc[] CONN_ARG = { new Integer JavaDoc(JDBC30Translation.CLOSE_CURSORS_AT_COMMIT)};
68
69     static private boolean isDerbyNet = false;
70
71     private static String JavaDoc VALID_DATE_STRING = "'2000-01-01'";
72     private static String JavaDoc VALID_TIME_STRING = "'15:30:20'";
73     private static String JavaDoc VALID_TIMESTAMP_STRING = "'2000-01-01 15:30:20'";
74     private static String JavaDoc NULL_VALUE="NULL";
75
76     private static String JavaDoc[] SQLTypes =
77     {
78         "SMALLINT",
79         "INTEGER",
80         "BIGINT",
81         "DECIMAL(10,5)",
82         "REAL",
83         "DOUBLE",
84         "CHAR(60)",
85         "VARCHAR(60)",
86         "LONG VARCHAR",
87         "CHAR(60) FOR BIT DATA",
88         "VARCHAR(60) FOR BIT DATA",
89         "LONG VARCHAR FOR BIT DATA",
90         "CLOB(1k)",
91         "DATE",
92         "TIME",
93         "TIMESTAMP",
94         "BLOB(1k)",
95
96     };
97
98     private static String JavaDoc[] ColumnNames =
99     {
100         "SMALLINTCOL",
101         "INTEGERCOL",
102         "BIGINTCOL",
103         "DECIMALCOL",
104         "REALCOL",
105         "DOUBLECOL",
106         "CHARCOL",
107         "VARCHARCOL",
108         "LONGVARCHARCOL",
109         "CHARFORBITCOL",
110         "VARCHARFORBITCOL",
111         "LVARCHARFORBITCOL",
112         "CLOBCOL",
113         "DATECOL",
114         "TIMECOL",
115         "TIMESTAMPCOL",
116         "BLOBCOL",
117
118     };
119
120  private static String JavaDoc[][]SQLData =
121     {
122         {NULL_VALUE, "0","1","2"}, // SMALLINT
123
{NULL_VALUE,"0","1","21"}, // INTEGER
124
{NULL_VALUE,"0","1","22"}, // BIGINT
125
{NULL_VALUE,"0.0","1.0","23.0"}, // DECIMAL(10,5)
126
{NULL_VALUE,"0.0","1.0","24.0"}, // REAL,
127
{NULL_VALUE,"0.0","1.0","25.0"}, // DOUBLE
128
{NULL_VALUE,"'0'","'aa'","'2.0'"}, // CHAR(60)
129
{NULL_VALUE,"'0'","'aa'",VALID_TIME_STRING}, //VARCHAR(60)",
130
{NULL_VALUE,"'0'","'aa'",VALID_TIMESTAMP_STRING}, // LONG VARCHAR
131
{NULL_VALUE,"X'10aa'",NULL_VALUE,"X'10aaaa'"}, // CHAR(60) FOR BIT DATA
132
{NULL_VALUE,"X'10aa'",NULL_VALUE,"X'10aaba'"}, // VARCHAR(60) FOR BIT DATA
133
{NULL_VALUE,"X'10aa'",NULL_VALUE,"X'10aaca'"}, //LONG VARCHAR FOR BIT DATA
134
{NULL_VALUE,"'13'","'14'",NULL_VALUE}, //CLOB(1k)
135
{NULL_VALUE,VALID_DATE_STRING,VALID_DATE_STRING,NULL_VALUE}, // DATE
136
{NULL_VALUE,VALID_TIME_STRING,VALID_TIME_STRING,VALID_TIME_STRING}, // TIME
137
{NULL_VALUE,VALID_TIMESTAMP_STRING,VALID_TIMESTAMP_STRING,VALID_TIMESTAMP_STRING}, // TIMESTAMP
138
{NULL_VALUE,NULL_VALUE,NULL_VALUE,NULL_VALUE} // BLOB
139
};
140
141  /**
142      * Hang onto the SecurityCheck class while running the
143      * tests so that it is not garbage collected during the
144      * test and lose the information it has collected.
145      */

146     private final Object JavaDoc nogc = SecurityCheck.class;
147
148     public static void main(String JavaDoc[] args) throws Throwable JavaDoc {
149
150         isDerbyNet = TestUtil.isNetFramework();
151
152         Connection JavaDoc con;
153         ResultSetMetaData JavaDoc met;
154         ResultSet JavaDoc rs;
155         Statement JavaDoc stmt;
156         String JavaDoc[] columnNames = {"i", "s", "r", "d", "dt", "t", "ts", "c", "v",
157                              "dc", "bi", "cbd", "vbd", "lvbd", "cl", "bl"};
158
159
160         System.out.println("Test resultset starting");
161
162         try
163         {
164             // use the ij utility to read the property file and
165
// make the initial connection.
166
ij.getPropertyArg(args);
167             con = ij.startJBMS();
168             // Test setCatalog/getCatalog for beetle 5504
169
con.setCatalog("mycatalog");
170             String JavaDoc cat = con.getCatalog();
171             if (cat != null )
172                 System.out.println("ERROR: getCatalog did not return null");
173             //Use reflection to set the holdability to false so that the test can run in jdk14 and lower jdks as well
174
try {
175                 Method sh = con.getClass().getMethod("setHoldability", CONN_PARAM);
176                 sh.invoke(con, CONN_ARG);
177             } catch (Exception JavaDoc e) {System.out.println("shouldn't get that error " + e.getMessage());}//for jdks prior to jdk14
178

179             stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
180                                        ResultSet.CONCUR_UPDATABLE);
181
182             try {
183                 stmt.execute("drop table t");
184             } catch (SQLException JavaDoc se) {
185             } // ignore, assume it is because table does not exist
186

187             stmt.execute("create table t (i int, s smallint, r real, "+
188                 "d double precision, dt date, t time, ts timestamp, "+
189                 "c char(10), v varchar(40) not null, dc dec(10,2),"+
190                 "bi bigint, cbd char(10) for bit data," +
191                 "vbd varchar(10) for bit data,lvbd long varchar for bit data,"+
192                 "cl clob(2G), bl blob(1G) )");
193             stmt.execute("insert into t values(1,2,3.3,4.4,date('1990-05-05'),"+
194                          "time('12:06:06'),timestamp('1990-07-07 07:07:07.000007'),"+
195                          "'eight','nine', 10.1, 11," +
196                          TestUtil.stringToHexLiteral("twelv") + "," +
197                          TestUtil.stringToHexLiteral("3teen") + "," +
198                          TestUtil.stringToHexLiteral("4teen") + ", null, null)" );
199
200
201             rs = stmt.executeQuery("select i, s, r, d, dt, t, ts, c, v, dc, bi, cbd, vbd, lvbd, cl, bl from t");
202             met = rs.getMetaData();
203
204             int colCount;
205             System.out.println("getColumnCount(): "+(colCount=met.getColumnCount()));
206
207             // JDBC columns use 1-based counting
208
for (int i=1;i<=colCount;i++) {
209                 System.out.println("isAutoIncrement("+i+"): "+met.isAutoIncrement(i));
210                 System.out.println("isCaseSensitive("+i+"): "+met.isCaseSensitive(i));
211                 System.out.println("isSearchable("+i+"): "+met.isSearchable(i));
212                 System.out.println("isCurrency("+i+"): "+met.isCurrency(i));
213                 System.out.println("isNullable("+i+"): "+met.isNullable(i));
214                 System.out.println("isSigned("+i+"): "+met.isSigned(i));
215                 System.out.println("getColumnDisplaySize("+i+"): "+met.getColumnDisplaySize(i));
216                 System.out.println("getColumnLabel("+i+"): "+met.getColumnLabel(i));
217                 System.out.println("getColumnName("+i+"): "+met.getColumnName(i));
218                 // beetle 5323
219
System.out.println("getTableName("+i+"): "+met.getTableName(i));
220                 System.out.println("getSchemaName("+i+"): "+met.getSchemaName(i));
221                 System.out.println("getCatalogName("+i+"): "+met.getCatalogName(i));
222                 System.out.println("getColumnType("+i+"): "+met.getColumnType(i));
223                 System.out.println("getPrecision("+i+"): "+met.getPrecision(i));
224                 System.out.println("getScale("+i+"): "+met.getScale(i));
225                 System.out.println("getColumnTypeName("+i+"): "+met.getColumnTypeName(i));
226                 System.out.println("isReadOnly("+i+"): "+met.isReadOnly(i));
227                 boolean writable = met.isWritable(i);
228                 // JCC & Embedded driver support updatable resultsets so isWritable is true
229
if (writable == true)
230                     System.out.println("isWritable("+i+"): Expected isWritable value");
231                 System.out.println("isDefinitelyWritable("+i+"): "+met.isDefinitelyWritable(i));
232             }
233
234             /* Try the various get methods on each column */
235             while (rs.next())
236             {
237                 // JDBC columns use 1-based counting
238
for (int i=1;i<=colCount;i++) {
239                     try {
240                         System.out.println("getBigDecimal("+i+",1): "+
241                                             BigDecimalHandler.getBigDecimalString(rs,i));
242                     }
243                     catch (Throwable JavaDoc e) {
244                         System.out.println(
245                             "getBigDecimal("+i+",1) got exception ");
246                         if (e instanceof SQLException JavaDoc)
247                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
248                     }
249
250                     try {
251                         if (isDerbyNet)
252                             System.out.println("beetle 5328 - JCC returns incorrect scale for getBigDecimal(String,int)");
253                         System.out.println("getBigDecimal("+
254                                         columnNames[i-1]+ ",1): "+
255                                         BigDecimalHandler.getBigDecimalString(rs,columnNames[i-1],i));
256                     }
257                     catch (Throwable JavaDoc e) {
258                         System.out.println(
259                             "getBigDecimal("+
260                             columnNames[i-1]+",1) got exception ");
261                         if (e instanceof SQLException JavaDoc)
262                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
263                     }
264
265                     try {
266                         System.out.println("getBoolean("+i+"): "+
267                                                     rs.getBoolean(i));
268                     }
269                     catch (Throwable JavaDoc e) {
270                         System.out.println(
271                             "getBoolean("+i+") got exception ");
272                         if (e instanceof SQLException JavaDoc)
273                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
274                     }
275
276                     try {
277                         System.out.println("getBoolean("+
278                                         columnNames[i-1]+ "): "+
279                                         rs.getBoolean(columnNames[i-1]));
280                     }
281                     catch (Throwable JavaDoc e) {
282                         System.out.println(
283                             "getBoolean("+
284                             columnNames[i-1]+") got exception " );
285                         if (e instanceof SQLException JavaDoc)
286                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
287                     }
288
289                     try {
290                         System.out.println("getByte("+i+"): "+
291                                                     rs.getByte(i));
292                     }
293                     catch (Throwable JavaDoc e) {
294                         System.out.println(
295                             "getByte("+i+") got exception " );
296                         if (e instanceof SQLException JavaDoc)
297                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
298                     }
299
300                     try {
301                         System.out.println("getByte("+
302                                         columnNames[i-1]+ "): "+
303                                         rs.getByte(columnNames[i-1]));
304                     }
305                     catch (Throwable JavaDoc e) {
306                         System.out.println(
307                             "getByte("+
308                             columnNames[i-1]+") got exception " );
309                         if (e instanceof SQLException JavaDoc)
310                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
311                     }
312
313                     try {
314                         System.out.println("getBytes("+i+"): "+
315                                                     showBytes(rs.getBytes(i)));
316                     }
317                     catch (SQLException JavaDoc e) {
318                         System.out.println(
319                             "getBytes("+i+") got exception " );
320                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
321                     }
322
323                     try {
324                         System.out.println("getBytes("+
325                                         columnNames[i-1]+ "): "+
326                                         showBytes(rs.getBytes(columnNames[i-1])));
327                     }
328                     catch (SQLException JavaDoc e) {
329                         System.out.println(
330                             "getBytes("+
331                             columnNames[i-1]+") got exception " );
332                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
333                     }
334
335                     try {
336                         System.out.println("getDate("+i+"): "+
337                                                     rs.getDate(i));
338                     }
339                     catch (SQLException JavaDoc e) {
340                         System.out.println(
341                             "getDate("+i+") got exception " );
342                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
343                     }
344
345                     try {
346                         System.out.println("getDate("+
347                                         columnNames[i-1]+ "): "+
348                                         rs.getDate(columnNames[i-1]));
349                     }
350                     catch (SQLException JavaDoc e) {
351                         System.out.println(
352                             "getDate("+
353                             columnNames[i-1]+") got exception " );
354                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
355                     }
356
357                     try {
358                         System.out.println("getDouble("+i+"): "+
359                                                     rs.getDouble(i));
360                     }
361                     catch (Throwable JavaDoc e) {
362                         System.out.println(
363                             "getDouble("+i+") got exception " );
364                         if (e instanceof SQLException JavaDoc)
365                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
366                     }
367
368                     try {
369                         System.out.println("getDouble("+
370                                         columnNames[i-1]+ "): "+
371                                         rs.getDouble(columnNames[i-1]));
372                     }
373                     catch (Throwable JavaDoc e) {
374                         System.out.println(
375                             "getDouble("+
376                             columnNames[i-1]+") got exception " );
377                         if (e instanceof SQLException JavaDoc)
378                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
379                     }
380
381                     try {
382                         System.out.println("getFloat("+i+"): "+
383                                                     rs.getFloat(i));
384                     }
385                     catch (Throwable JavaDoc e) {
386                         System.out.println(
387                             "getFloat("+i+") got exception " );
388                         if (e instanceof SQLException JavaDoc)
389                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
390                     }
391
392                     try {
393                         System.out.println("getFloat("+
394                                         columnNames[i-1]+ "): "+
395                                         rs.getFloat(columnNames[i-1]));
396                     }
397                     catch (Throwable JavaDoc e) {
398                         System.out.println(
399                             "getFloat("+
400                             columnNames[i-1]+") got exception " );
401                         if (e instanceof SQLException JavaDoc)
402                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
403                     }
404
405                     try {
406                         System.out.println("getInt("+i+"): "+
407                                                     rs.getInt(i));
408                     }
409                     catch (Throwable JavaDoc e) {
410                         System.out.println(
411                             "getInt("+i+") got exception " );
412                         if (e instanceof SQLException JavaDoc)
413                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
414                     }
415
416                     try {
417                         System.out.println("getInt("+
418                                         columnNames[i-1]+ "): "+
419                                         rs.getInt(columnNames[i-1]));
420                     }
421                     catch (Throwable JavaDoc e) {
422                         System.out.println(
423                             "getInt("+
424                             columnNames[i-1]+") got exception " );
425                         if (e instanceof SQLException JavaDoc)
426                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
427                     }
428
429                     try {
430                         System.out.println("getLong("+i+"): "+
431                                                     rs.getLong(i));
432                     }
433                     catch (Throwable JavaDoc e) {
434                         System.out.println(
435                             "getLong("+i+") got exception " );
436                         if (e instanceof SQLException JavaDoc)
437                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
438                     }
439
440                     try {
441                         System.out.println("getLong("+
442                                         columnNames[i-1]+ "): "+
443                                         rs.getLong(columnNames[i-1]));
444                     }
445                     catch (Throwable JavaDoc e) {
446                         System.out.println(
447                             "getLong("+
448                             columnNames[i-1]+") got exception " );
449                         if (e instanceof SQLException JavaDoc)
450                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
451                     }
452
453                     try {
454                         // with the bit datatypes the string output is not the same for every run,
455
// so we need to mask that to prevent false test failures
456
// this does not test the values returned, just whether it gives an exception.
457
if (i>11)
458                         {
459                             BigDecimalHandler.getObjectString(rs,i);
460                             System.out.println("getObject("+i+") is ok");
461                         }
462                         else
463                             System.out.println("getObject("+i+"): "+
464                                                 BigDecimalHandler.getObjectString(rs,i));
465                     }
466                     catch (SQLException JavaDoc e) {
467                         System.out.println(
468                             "getObject("+i+") got exception " );
469                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
470                     }
471
472                     try {
473                         // with the bit datatypes the string output is not the same for every run,
474
// so we need to mask that to prevent false test failures
475
// this does not test the values returned, just whether it gives an exception.
476
if (i>11)
477                         {
478                             BigDecimalHandler.getObjectString(rs,columnNames[i-1],i);
479                             System.out.println("getObject("+columnNames[i-1]+") is ok ");
480                         }
481                         else
482                             System.out.println("getObject("+
483                                         columnNames[i-1]+ "): "+
484                                         BigDecimalHandler.getObjectString(rs,columnNames[i-1],i));
485                     }
486                     catch (SQLException JavaDoc e) {
487                         System.out.println(
488                             "getObject("+
489                             columnNames[i-1]+") got exception " );
490                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
491                     }
492
493                     try {
494                         System.out.println("getShort("+i+"): "+
495                                                     rs.getShort(i));
496                     }
497                     catch (Throwable JavaDoc e) {
498                         System.out.println(
499                             "getShort("+i+") got exception " );
500                         if (e instanceof SQLException JavaDoc)
501                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
502                     }
503
504                     try {
505                         System.out.println("getShort("+
506                                         columnNames[i-1]+ "): "+
507                                         rs.getShort(columnNames[i-1]));
508                     }
509                     catch (Throwable JavaDoc e) {
510                         System.out.println(
511                             "getShort("+
512                             columnNames[i-1]+") got exception " );
513                         if (e instanceof SQLException JavaDoc)
514                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
515                     }
516
517                     try {
518                         System.out.println("getString("+i+"): "+
519                                                     rs.getString(i));
520                     }
521                     catch (SQLException JavaDoc e) {
522                         System.out.println(
523                             "getString("+i+") got exception " );
524                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
525                     }
526                     
527                     try {
528                         System.out.println("getString("+
529                                         columnNames[i-1]+ "): "+
530                                         rs.getString(columnNames[i-1]));
531                     }
532                     catch (SQLException JavaDoc e) {
533                         System.out.println(
534                             "getString("+
535                             columnNames[i-1]+") got exception " );
536                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
537                     }
538
539                     try {
540                         System.out.println("getTime("+i+"): "+
541                                                     rs.getTime(i));
542                     }
543                     catch (SQLException JavaDoc e) {
544                         System.out.println(
545                             "getTime("+i+") got exception " );
546                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
547                     }
548
549                     try {
550                         System.out.println("getTime("+
551                                         columnNames[i-1]+ "): "+
552                                         rs.getTime(columnNames[i-1]));
553                     }
554                     catch (SQLException JavaDoc e) {
555                         System.out.println(
556                             "getTime("+
557                             columnNames[i-1]+") got exception " );
558                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
559                     }
560
561                     try {
562                         System.out.println("getTimestamp("+i+"): "+
563                                                     rs.getTimestamp(i));
564                     }
565                     catch (SQLException JavaDoc e) {
566                         System.out.println(
567                             "getTimestamp("+i+") got exception " );
568                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
569                     }
570
571                     try {
572                         System.out.println("getTimestamp("+
573                                         columnNames[i-1]+ "): "+
574                                         rs.getTimestamp(columnNames[i-1]));
575                     }
576                     catch (SQLException JavaDoc e) {
577                         System.out.println(
578                             "getTimestamp("+
579                             columnNames[i-1]+") got exception " );
580                         JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException JavaDoc)e);
581                     }
582                 }
583             }
584
585             rs.close();
586             
587             // Try getting a row from the closed result set
588
try {
589                 rs.next();
590                 System.out.println(
591                     "FAIL - rs.next() allowed on closed result set.");
592             }
593             catch (SQLException JavaDoc e) {
594                 System.out.println(
595                     "rs.next() on closed result set got exception " );
596                 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, e);
597             }
598             catch (Throwable JavaDoc e) {
599                 System.out.println("rs.next() didn't fail with SQLException as "+
600                     "expected on closed result set. Got Throwable instead: "+e);
601             }
602
603             // Ensure commit or rollback in auto commit actually does something
604
stmt.executeUpdate("create table bug4810(i int, b int)");
605             stmt.executeUpdate("insert into bug4810 values (1,1), (1,2), (1,3), (1,4)");
606             stmt.executeUpdate("insert into bug4810 values (1,1), (1,2), (1,3), (1,4)");
607             con.commit();
608             con.setAutoCommit(true);
609             con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
610             System.out.println("just auto commit");
611             showLocksForAutoCommitSelect(con, stmt, 0);
612             System.out.println("commit with auto commit");
613             showLocksForAutoCommitSelect(con, stmt, 1);
614             System.out.println("rollback with auto commit");
615             showLocksForAutoCommitSelect(con, stmt, 2);
616
617             stmt.execute("drop table bug4810");
618             con.commit();
619             stmt.close();
620
621             testMutableValues(con);
622             testCorrelationNamesAndMetaDataCalls(con);
623             testNullIfAndMetaDataCalls(con);
624                         testFloatMAX_VALUE(con);
625             //We know that JCC behavior does not match
626
//DerbyNetClient or embedded
627
if (!TestUtil.isJCCFramework()) {
628                 runAutoCommitTests(con);
629             }
630             // final clean up
631
cleanUp(con);
632             con.close();
633
634         }
635         catch (SQLException JavaDoc e) {
636             System.out.println("FAIL -- unexpected exception: " + e.toString());
637             dumpSQLExceptions(e);
638             e.printStackTrace();
639         }
640         catch (Throwable JavaDoc e) {
641             System.out.println("FAIL -- unexpected exception: "+e);
642             e.printStackTrace();
643         }
644
645         // Print a report on System.out of the issues
646
// found with the security checks.
647
SecurityCheck.report();
648         System.out.println("Test resultset finished");
649     }
650
651
652     //test NULLIF(L,R) with and without parameter and check the return value's data type
653
static private void testNullIfAndMetaDataCalls(Connection JavaDoc conn) throws Throwable JavaDoc
654     {
655         System.out.println("Tests to check metadata information of nullif column");
656         tablesForTestingAllDatatypesCombinations(conn);
657         testAllDatatypesCombinations(conn);
658         testParameterForFirstOperandToNullIf(conn);
659     }
660           
661     public static void testParameterForFirstOperandToNullIf( Connection JavaDoc conn) throws Throwable JavaDoc
662     {
663         System.out.println("Start testing first operand as parameter to nullif");
664         PreparedStatement JavaDoc ps;
665         for (int secondColumnType = 0; secondColumnType < SQLTypes.length; secondColumnType++) {
666             System.out.println("Testing nullif(?,"+SQLTypes[secondColumnType]+")");
667             String JavaDoc nullIfString =
668                 new String JavaDoc("SELECT NULLIF(?," + ColumnNames[secondColumnType] +") from AllDataTypesTable");
669             try {
670                     ps = conn.prepareStatement(nullIfString);
671                     switch(secondColumnType) {
672                         case 0:
673                         case 1:
674                         case 2:
675                         case 3:
676                         case 4:
677                         case 5:
678                         case 6:
679                         case 7:
680                             System.out.println("Testing nullif(?,"+SQLTypes[secondColumnType]+") with setBoolean");
681                             ps.setBoolean(1, true);
682                             break;
683                         case 8: //'LONG VARCHAR'
684
case 11: //'LONG VARCHAR FOR BIT DATA'
685
case 12: //'CLOB'
686
case 16: //'BLOB'
687
//Take specific case of LONG VARCHAR. Prepare of nullif(?,long varchar)
688
//fails early on because at bind time, Derby tries to set ? to
689
//long varchar. But comparison between 2 long varchars is not
690
//supported and hence bind code in BinaryComparisonOperatorNode fails
691
//Similar thing happens for CLOB, BLOB and LONG VARCHAR FOR BIT DATA
692
case 9:
693                         case 10:
694                             System.out.println("Testing nullif(?,"+SQLTypes[secondColumnType]+") with setBinaryStream");
695                             ps.setBinaryStream(1, (java.io.InputStream JavaDoc)null, 1);
696                             break;
697                         case 13://DATE
698
System.out.println("Testing nullif(?,"+SQLTypes[secondColumnType]+") with setDate");
699                             ps.setDate(1, Date.valueOf("2000-01-01"));
700                             break;
701                         case 14://TIME
702
System.out.println("Testing nullif(?,"+SQLTypes[secondColumnType]+") with setTime");
703                             ps.setTime(1, Time.valueOf("15:30:20"));
704                             break;
705                         case 15://TIMESTAMP
706
System.out.println("Testing nullif(?,"+SQLTypes[secondColumnType]+") with setTimestamp");
707                             ps.setTimestamp(1, Timestamp.valueOf("2000-01-01 15:30:20"));
708                             break;
709                         default: break;
710                     }
711                     dumpRS(ps.executeQuery());
712             } catch (SQLException JavaDoc e)
713             {
714                 dumpSQLExceptions(e);
715             }
716         }
717     }
718
719     public static void testAllDatatypesCombinations( Connection JavaDoc conn) throws Throwable JavaDoc
720     {
721         System.out.println("Start testing all datatypes combinations in NULLIF function");
722         Statement JavaDoc s = conn.createStatement();
723         for (int firstColumnType = 0; firstColumnType < SQLTypes.length; firstColumnType++) {
724             StringBuffer JavaDoc nullIfString = new StringBuffer JavaDoc("SELECT NULLIF(" + ColumnNames[firstColumnType]);
725             for (int secondColumnType = 0; secondColumnType < SQLTypes.length; secondColumnType++) {
726                 try {
727                     StringBuffer JavaDoc completeNullIfString = new StringBuffer JavaDoc(nullIfString.toString() + "," + ColumnNames[secondColumnType]);
728                     System.out.println(completeNullIfString + ") from AllDataTypesTable");
729                     dumpRS(s.executeQuery(completeNullIfString + ") from AllDataTypesTable"));
730                 } catch (SQLException JavaDoc e)
731                 {
732                     dumpSQLExceptions(e);
733                 }
734             }
735         }
736     }
737
738     public static void tablesForTestingAllDatatypesCombinations( Connection JavaDoc conn) throws Throwable JavaDoc
739     {
740         System.out.println("Set up by creating table for testing all datatypes combinations");
741
742         Statement JavaDoc s = conn.createStatement();
743
744         try {
745             s.executeUpdate("DROP TABLE AllDataTypesTable");
746         }
747         catch(SQLException JavaDoc se) {}
748
749         StringBuffer JavaDoc createSQL = new StringBuffer JavaDoc("create table AllDataTypesTable (");
750         for (int type = 0; type < SQLTypes.length - 1; type++)
751         {
752             createSQL.append(ColumnNames[type] + " " + SQLTypes[type] + ",");
753         }
754         createSQL.append(ColumnNames[SQLTypes.length - 1] + " " + SQLTypes[SQLTypes.length - 1] + ")");
755         System.out.println(createSQL);
756         s.executeUpdate(createSQL.toString());
757
758         for (int row = 0; row < SQLData[0].length; row++)
759         {
760             createSQL = new StringBuffer JavaDoc("insert into AllDataTypesTable values(");
761             for (int type = 0; type < SQLTypes.length - 1; type++)
762             {
763                 createSQL.append(SQLData[type][row] + ",");
764             }
765             createSQL.append(SQLData[SQLTypes.length - 1][row]+")");
766             System.out.println(createSQL);
767             s.executeUpdate(createSQL.toString());
768         }
769
770         s.close();
771         conn.commit();
772     }
773
774     public static void dumpRS(ResultSet JavaDoc s) throws SQLException JavaDoc
775     {
776         SecurityCheck.inspect(s, "java.sql.ResultSet");
777         
778         if (s == null)
779         {
780             System.out.println("<NULL>");
781             return;
782         }
783
784         // This might look strange, but we want to know that the first
785
// call to next() is successful before we get meta data and
786
// display column headings. If a driver is pre-fetching data,
787
// an exception might be thrown in Statement.executeQuery(),
788
// but a driver without pre-fetching will not get the
789
// exception until ResultSet.next() is called (hence column
790
// headings are printed). With this early call to next(), we
791
// get the same output regardless of pre-fetching.
792
boolean hasData = s.next();
793
794         ResultSetMetaData JavaDoc rsmd = s.getMetaData();
795         SecurityCheck.inspect(rsmd, "java.sql.ResultSetMetaData");
796
797         // Get the number of columns in the result set
798
int numCols = rsmd.getColumnCount();
799
800         if (numCols <= 0)
801         {
802             System.out.println("(no columns!)");
803             return;
804         }
805
806         StringBuffer JavaDoc heading = new StringBuffer JavaDoc("\t ");
807         StringBuffer JavaDoc underline = new StringBuffer JavaDoc("\t ");
808
809         int len;
810         // Display column headings
811
for (int i=1; i<=numCols; i++)
812         {
813             if (i > 1)
814             {
815                 heading.append(",");
816                 underline.append(" ");
817             }
818             len = heading.length();
819             heading.append("COL"+i);
820             heading.append("(datatype : " + rsmd.getColumnTypeName(i));
821             heading.append(", precision : " + rsmd.getPrecision(i));
822             heading.append(", scale : " + rsmd.getScale(i) + ")");
823             len = heading.length() - len;
824             for (int j = len; j > 0; j--)
825             {
826                 underline.append("-");
827             }
828         }
829         System.out.println(heading.toString());
830         System.out.println(underline.toString());
831
832
833         StringBuffer JavaDoc row = new StringBuffer JavaDoc();
834         // Display data, fetching until end of the result set
835
while (hasData)
836         {
837             row.append("\t{");
838             // Loop through each column, getting the
839
// column data and displaying
840
for (int i=1; i<=numCols; i++)
841             {
842                 if (i > 1) row.append(",");
843                 try{
844                 row.append(s.getString(i));
845                 } catch(SQLException JavaDoc ex){
846                     if (ex.getSQLState().equals("22005"))
847                         row.append("Invalid Conversion Error\n");
848                     else throw ex;
849                 }
850             }
851             row.append("}\n");
852             hasData = s.next();
853         }
854         System.out.println(row.toString());
855         s.close();
856     }
857
858
859     static private void testCorrelationNamesAndMetaDataCalls(Connection JavaDoc conn) throws Exception JavaDoc
860     {
861         Statement JavaDoc stmt = conn.createStatement();
862         stmt.executeUpdate("create table s (a int, b int, c int, d int, e int, f int)");
863         stmt.executeUpdate("insert into s values (0,1,2,3,4,5)");
864         stmt.executeUpdate("insert into s values (10,11,12,13,14,15)");
865         System.out.println("Run select * from s ss (f, e, d, c, b, a) where f = 0 and then try getTableName and getSchemaName on columns");
866         ResultSet JavaDoc rs = stmt.executeQuery("select * from s ss (f, e, d, c, b, a) where f = 0");
867     rs.next();
868     ResultSetMetaData JavaDoc met = rs.getMetaData();
869         System.out.println("getTableName(1): "+met.getTableName(1));
870         System.out.println("getSchemaName(1): "+met.getSchemaName(1));
871
872         System.out.println("Run select * from (select * from s) a and then try getTableName and getSchemaName on columns");
873         rs = stmt.executeQuery("select * from (select * from s) a");
874     rs.next();
875     met = rs.getMetaData();
876         System.out.println("getTableName(1): "+met.getTableName(1));
877         System.out.println("getSchemaName(1): "+met.getSchemaName(1));
878
879         stmt.executeUpdate("create schema s1");
880         stmt.executeUpdate("create table s1.t1 (c11 int, c12 int)");
881         stmt.executeUpdate("insert into s1.t1 values (11, 12), (21, 22)");
882         System.out.println("Run select * from s1.t1 as abc and then try getTableName and getSchemaName on columns");
883         rs = stmt.executeQuery("select * from s1.t1 as abc");
884         met = rs.getMetaData();
885         System.out.println("Table name of first column is " + met.getTableName(1));
886         System.out.println("Schema name of first column is " + met.getSchemaName(1));
887         System.out.println("Table name of second column is " + met.getTableName(2));
888         System.out.println("Schema name of second column is " + met.getSchemaName(2));
889         System.out.println("Run select abc.c11 from s1.t1 as abc and then try getTableName and getSchemaName on columns");
890         rs = stmt.executeQuery("select abc.c11 from s1.t1 as abc");
891         met = rs.getMetaData();
892         System.out.println("Table name of first column is " + met.getTableName(1));
893         System.out.println("Schema name of first column is " + met.getSchemaName(1));
894         System.out.println("Run select bcd.a, abc.c11 from s1.t1 as abc, s as bcd and then try getTableName and getSchemaName on columns");
895         rs = stmt.executeQuery("select bcd.a, abc.c11 from s1.t1 as abc, s as bcd");
896         met = rs.getMetaData();
897         System.out.println("Table name of first column is " + met.getTableName(1));
898         System.out.println("Schema name of first column is " + met.getSchemaName(1));
899         System.out.println("Table name of second column is " + met.getTableName(2));
900         System.out.println("Schema name of second column is " + met.getSchemaName(2));
901
902         stmt.executeUpdate("create schema app1");
903         stmt.executeUpdate("create table app1.t1 (c11 int, c12 int)");
904         stmt.executeUpdate("insert into app1.t1 values (11, 12), (21, 22)");
905         stmt.executeUpdate("create schema app2");
906         stmt.executeUpdate("create table app2.t1 (c11 int, c12 int)");
907         stmt.executeUpdate("insert into app2.t1 values (11, 12), (21, 22)");
908         System.out.println("Run select app1.t1.c11, app2.t1.c11 from app1.t1, app2.t1 and then try getTableName and getSchemaName on columns");
909         rs = stmt.executeQuery("select app1.t1.c11, app2.t1.c11 from app1.t1, app2.t1");
910         met = rs.getMetaData();
911         System.out.println("Table name of first column is " + met.getTableName(1));
912         System.out.println("Schema name of first column is " + met.getSchemaName(1));
913         System.out.println("Table name of second column is " + met.getTableName(2));
914         System.out.println("Schema name of second column is " + met.getSchemaName(2));
915         stmt.execute("drop table s");
916         stmt.execute("drop table s1.t1");
917         stmt.execute("drop schema s1 restrict");
918         stmt.execute("drop table app1.t1");
919         stmt.execute("drop table app2.t1");
920         stmt.execute("drop schema app2 restrict");
921         stmt.execute("drop schema app1 restrict");
922     }
923
924     static private void doTheTests() throws Exception JavaDoc
925     {
926
927     }
928
929     static private void showLocksForAutoCommitSelect(Connection JavaDoc conn, Statement JavaDoc stmt, int action) throws Exception JavaDoc {
930
931         ResultSet JavaDoc rs = stmt.executeQuery("select i,b from bug4810");
932         rs.next();
933         System.out.println(" bug4810 " + rs.getInt(1) + ", " + rs.getInt(2));
934         rs.next();
935         System.out.println(" bug4810 " + rs.getInt(1) + ", " + rs.getInt(2));
936
937         if (action == 1) {
938             System.out.println("commit");
939             conn.commit();
940         } else if (action == 2) {
941             System.out.println("rollback");
942             conn.rollback();
943         }
944
945         showLocks();
946
947         try {
948
949             rs.next();
950             System.out.println(" bug4810 " + rs.getInt(1) + ", " + rs.getInt(2));
951         } catch (SQLException JavaDoc sqle) {
952             JDBCTestDisplayUtil.ShowCommonSQLException(System.out, sqle); }
953         showLocks();
954         rs.close();
955
956         showLocks();
957
958     }
959
960     private static void showLocks() throws Exception JavaDoc {
961         System.out.println(" LOCK TABLE");
962         Connection JavaDoc con2 = ij.startJBMS();
963         PreparedStatement JavaDoc ps2 = con2.prepareStatement("select XID, count(*) from SYSCS_DIAG.LOCK_TABLE as L group by XID");
964         ResultSet JavaDoc rs2 = ps2.executeQuery();
965
966         while (rs2.next()) {
967             if (rs2.getInt(2) > 0) {
968                 System.out.println("Locks are held");
969             } else if (rs2.getInt(2) == 0) {
970                 System.out.println("No locks to hold");
971             }
972         }
973         
974         rs2.close();
975         ps2.close();
976         con2.close();
977     }
978
979     static private void dumpSQLExceptions (SQLException JavaDoc se) {
980         while (se != null) {
981             JDBCTestDisplayUtil.ShowCommonSQLException(System.out, se);
982             se = se.getNextException();
983         }
984     }
985
986     static private String JavaDoc showBytes(byte[] bytes) {
987         if (bytes == null)
988             return "null";
989
990         StringBuffer JavaDoc s = new StringBuffer JavaDoc("0x");
991         s.ensureCapacity(2+2*bytes.length);
992         for (int i=0;i<bytes.length;i++) {
993             int hi = (bytes[i] & 0xf0) >>> 4;
994             int lo = (bytes[i] & 0x0f);
995             s.append(representation[hi]);
996             s.append(representation[lo]);
997         }
998         return s.toString();
999     }
1000
1001    static final char[] representation =
1002        { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
1003          'A', 'B', 'C', 'D', 'E', 'F' } ;
1004
1005
1006    /**
1007        Test that for mutable types returned from a ResultSet we do not
1008        re-use the type, thus conusing any application that holds onto
1009        the returned value. Possible mutable types are
1010
1011        byte[]
1012        java.sql.Date
1013        java.sql.Timestamp
1014        java.sql.Time
1015
1016        The stream types are mutable but they are closed once the appliction
1017        moves to the next column or row.
1018    */

1019    private static void testMutableValues(Connection JavaDoc conn) throws SQLException JavaDoc
1020    {
1021        System.out.println("START testMutableValues");
1022
1023        Statement JavaDoc s = conn.createStatement();
1024
1025        s.execute("CREATE TABLE MUTABLE.T1(C CHAR(10) FOR BIT DATA, V VARCHAR(10) FOR BIT DATA, L LONG VARCHAR FOR BIT DATA, D DATE, T TIME, TS TIMESTAMP)");
1026        s.execute("INSERT INTO MUTABLE.T1 VALUES (X'34', X'4de5', X'5e3a67', '1992-01-01', '17.05.00', '2003-3-1-17.05.43.123456')");
1027        s.execute("INSERT INTO MUTABLE.T1 VALUES (X'93', X'4825', X'6e3a64', '1992-01-03', '17.06.00', '2007-3-1-17.05.43.123456')");
1028        s.execute("INSERT INTO MUTABLE.T1 VALUES (X'34', X'4de5', X'5e3a67', '1992-01-01', '17.05.00', '2003-3-1-17.05.43.123456')");
1029
1030        {
1031        ResultSet JavaDoc rs = s.executeQuery("SELECT C,V,L,D,T,TS FROM MUTABLE.T1");
1032        java.util.ArrayList JavaDoc[] values = new java.util.ArrayList JavaDoc[6];
1033        for (int i = 0; i < values.length; i++) {
1034            values[i] = new java.util.ArrayList JavaDoc();
1035        }
1036        System.out.println("CHECKING on getXXX()");
1037        int rc = 0;
1038        while (rs.next()) {
1039            rc++;
1040            System.out.println("ROW " + rc);
1041
1042            checkMutableValue(values[0], 1, rs.getBytes(1));
1043            checkMutableValue(values[1], 2, rs.getBytes(2));
1044            checkMutableValue(values[2], 3, rs.getBytes(3));
1045
1046            checkMutableValue(values[3], 4, rs.getDate(4));
1047            checkMutableValue(values[4], 5, rs.getTime(5));
1048            checkMutableValue(values[5], 6, rs.getTimestamp(6));
1049
1050        }
1051        rs.close();
1052        }
1053        {
1054        ResultSet JavaDoc rs = s.executeQuery("SELECT C,V,L,D,T,TS FROM MUTABLE.T1");
1055        java.util.ArrayList JavaDoc[] values = new java.util.ArrayList JavaDoc[6];
1056        for (int i = 0; i < values.length; i++) {
1057            values[i] = new java.util.ArrayList JavaDoc();
1058        }
1059        System.out.println("CHECKING on getObject()");
1060        int rc = 0;
1061        while (rs.next()) {
1062            rc++;
1063            System.out.println("ROW " + rc);
1064
1065            for (int i = 0; i < 6; i++)
1066                checkMutableValue(values[i], i+1, rs.getObject(i+1));
1067        }
1068        rs.close();
1069        }
1070
1071        s.execute("DROP TABLE MUTABLE.T1");
1072
1073        System.out.println("COMPLETE testMutableValues");
1074    }
1075
1076    private static void checkMutableValue(java.util.ArrayList JavaDoc list, int col, Object JavaDoc value) {
1077
1078        int same = -1;
1079        int equals = -1;
1080        for (int i = 0; i < list.size(); i++) {
1081            Object JavaDoc previous = list.get(i);
1082            if (previous == value)
1083                same = i+1;
1084            if (previous.equals(value))
1085                equals = i+1;
1086        }
1087
1088        if (same != -1)
1089            System.out.println("FAIL SAME OBJECT RETURNED column " + col + " existing " + same);
1090        if (equals != -1)
1091            System.out.println("OK EQUALITY OBJECT RETURNED column " + col + " existing " + equals);
1092
1093        list.add(value);
1094    }
1095    
1096    // JIRA-1136: LossOfPrecisionConversionException fetching Float.MAX_VALUE.
1097
// This test proves that we can successfully fetch that value from the DB
1098
// Note that we still fail to fetch that value in the JCC driver, so we
1099
// don't try this test under that framework, since it would fail, and we
1100
// share masters for these tests.
1101
private static void testFloatMAX_VALUE(Connection JavaDoc conn)
1102        throws SQLException JavaDoc
1103    {
1104        Statement JavaDoc stmt = conn.createStatement();
1105        try { stmt.execute("drop table jira1136"); } catch (Throwable JavaDoc t) { }
1106        stmt.execute("create table jira1136 (f float)");
1107        stmt.execute("insert into jira1136 values (3.4028235E38)");
1108        PreparedStatement JavaDoc ps = conn.prepareStatement("select * from jira1136");
1109        ResultSet JavaDoc rs = ps.executeQuery();
1110        while (rs.next())
1111        {
1112            if (!TestUtil.isJCCFramework())
1113                System.out.println("DERBY-1136 fetched: " + rs.getFloat(1));
1114            else // lie about the result so we can share the master files.
1115
System.out.println("DERBY-1136 fetched: 3.4028235E38");
1116        }
1117    }
1118
1119    /**
1120     * Helper method to set up and run the auto-commit tests.
1121     *
1122     * @param conn The Connection
1123     * @throws SQLException
1124     */

1125    private static void runAutoCommitTests(Connection JavaDoc conn) throws SQLException JavaDoc {
1126        Statement JavaDoc s = conn.createStatement();
1127        ResultSet JavaDoc rs = s.executeQuery("select tablename from sys.systables " +
1128                "where tablename = 'AUTOCOMMITTABLE'");
1129        if (rs.next()) {
1130            rs.close();
1131            s.executeUpdate("delete from AutoCommitTable");
1132        } else {
1133            rs.close();
1134            s.executeUpdate("create table AutoCommitTable (num int)");
1135        }
1136        s.executeUpdate("insert into AutoCommitTable values (1)");
1137        s.executeUpdate("insert into AutoCommitTable values (2)");
1138        int isolation = conn.getTransactionIsolation();
1139        conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
1140        testSingleRSAutoCommit(conn);
1141        testSingleRSCloseCursorsAtCommit(conn);
1142        conn.setTransactionIsolation(isolation);
1143        s.executeUpdate("drop table AutoCommitTable");
1144        s.close();
1145    }
1146    
1147    /**
1148     * Tests for two things:
1149     *
1150     * 1) The ResultSet does not close implicitly when the ResultSet completes
1151     * and holdability == HOLD_CURSORS_OVER_COMMIT
1152     *
1153     * 2) The ResultSet auto-commits when it completes and auto-commit is on.
1154     *
1155     * @param conn The Connection
1156     * @param tableName
1157     * @throws SQLException
1158     */

1159    private static void testSingleRSAutoCommit(Connection JavaDoc conn) throws SQLException JavaDoc {
1160        setHoldability(conn, JDBC30Translation.HOLD_CURSORS_OVER_COMMIT);
1161        System.out.print("Single RS auto-commit test: ");
1162        Statement JavaDoc s = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
1163        ResultSet JavaDoc rs = s.executeQuery("select * from AutoCommitTable");
1164        while (rs.next());
1165        if (!checkLocks()) {
1166            System.out.println("FAIL. Auto-commit unsuccessful.");
1167            rs.close();
1168            return;
1169        }
1170        try {
1171            if (!rs.next()) {
1172                System.out.println("PASS.");
1173            } else {
1174                System.out.println("FAIL. Final call of the ResultSet should return false");
1175            }
1176            rs.close();
1177        } catch (SQLException JavaDoc e) {
1178            System.out.println("FAIL. Final call to ResultSet.next() threw an Exception: ");
1179            e.printStackTrace();
1180        }
1181    }
1182    
1183    /**
1184     * Check to see that ResultSet closes implicitly when holdability is set to
1185     * CLOSE_CURORS_AT_COMMIT.
1186     *
1187     * @param conn The Connection
1188     * @throws SQLException
1189     */

1190    private static void testSingleRSCloseCursorsAtCommit(Connection JavaDoc conn) throws SQLException JavaDoc {
1191        setHoldability(conn, JDBC30Translation.CLOSE_CURSORS_AT_COMMIT);
1192        conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
1193        System.out.print("SingleRSCloseCursorsAtCommit: ");
1194        Statement JavaDoc s = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
1195        ResultSet JavaDoc rs = s.executeQuery("select * from AutoCommitTable");
1196        while (rs.next());
1197        if (!checkLocks()) {
1198            System.out.println("FAIL. Auto-commit unsuccessful.");
1199            rs.close();
1200            return;
1201        }
1202        try {
1203            rs.next();
1204            System.out.println("FAIL. ResultSet not closed implicitly");
1205            rs.close();
1206        } catch (SQLException JavaDoc e) {
1207            System.out.println("PASS.");
1208        }
1209    }
1210    
1211    
1212    /**
1213     * Checks to see if there is a lock on a table by attempting to modify the
1214     * same table. If the first connection was serializable then it will
1215     * continue to hold a lock and the second Connection will time out.
1216     *
1217     * @return false if the a lock could not be established, true if a lock
1218     * can be established.
1219     * @throws SQLException
1220     */

1221    private static boolean checkLocks() throws SQLException JavaDoc {
1222        Connection JavaDoc conn = null;
1223        try {
1224            conn = ij.startJBMS();
1225        } catch (Exception JavaDoc e) {
1226            System.out.println("FAIL. Unable to establish connection in checkLocks");
1227            return false;
1228        }
1229        Statement JavaDoc stmt = conn.createStatement();
1230        try {
1231            stmt.executeUpdate("update AutoCommitTable "
1232                    + "set num = 3 where num = 2");
1233            stmt.executeUpdate("update AutoCommitTable "
1234                    + "set num = 2 where num = 3");
1235        } catch (SQLException JavaDoc e) {
1236            if (e.getSQLState().equals(SQLState.LOCK_TIMEOUT)) {
1237                return false;
1238            } else {
1239                throw e;
1240            }
1241        }
1242        stmt.close();
1243        conn.close();
1244        return true;
1245    }
1246    
1247    /**
1248     * Sets the holdability of a Connection using reflection so it is
1249     * JDBC2.0 compatible.
1250     *
1251     * @param conn The Connection
1252     * @param hold The new holdability.
1253     * @throws SQLException
1254     */

1255    public static void setHoldability(Connection JavaDoc conn, int hold) throws SQLException JavaDoc {
1256        try {
1257            Object JavaDoc[] holdArray = {new Integer JavaDoc(hold)};
1258            Method sh = conn.getClass().getMethod("setHoldability", CONN_PARAM);
1259            sh.invoke(conn, holdArray);
1260        } catch (Exception JavaDoc e) {System.out.println("shouldn't get that error " + e.getMessage());}//for jdks prior to jdk14
1261
}
1262
1263    private static void cleanUp(Connection JavaDoc conn) throws SQLException JavaDoc {
1264    Statement JavaDoc s = conn.createStatement();
1265    String JavaDoc[] testObjects = {"TABLE APP.T"};
1266        try {
1267        TestUtil.cleanUpTest(s, testObjects);
1268        } catch (SQLException JavaDoc se){
1269        }
1270    }
1271
1272}
1273
Popular Tags