KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > outparams


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.lang.outparams
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.lang;
23 import java.sql.*;
24
25 import org.apache.derby.tools.ij;
26 import org.apache.derby.iapi.reference.JDBC30Translation;
27 import java.io.PrintStream JavaDoc;
28 import java.math.BigInteger JavaDoc;
29 import java.math.BigDecimal JavaDoc;
30 import org.apache.derbyTesting.functionTests.util.BigDecimalHandler;
31
32 public class outparams
33 {
34     
35     private static boolean HAVE_BIG_DECIMAL;
36     private static boolean HAVE_DRIVER_CLASS;
37     private static String JavaDoc CLASS_NAME;
38     
39     //Get the class name to be used for the procedures
40
//outparams - J2ME; outparams30 - non-J2ME
41
static{
42         if(BigDecimalHandler.representation != BigDecimalHandler.BIGDECIMAL_REPRESENTATION)
43             HAVE_BIG_DECIMAL = false;
44         else
45             HAVE_BIG_DECIMAL = true;
46         if(HAVE_BIG_DECIMAL)
47             CLASS_NAME = "org.apache.derbyTesting.functionTests.tests.lang.outparams30.";
48         else
49             CLASS_NAME = "org.apache.derbyTesting.functionTests.tests.lang.outparams.";
50     }
51     
52     static{
53         try{
54             Class.forName("java.sql.Driver");
55             HAVE_DRIVER_CLASS = true;
56         }
57         catch(ClassNotFoundException JavaDoc e){
58             //Used for JSR169
59
HAVE_DRIVER_CLASS = false;
60         }
61     }
62     
63     static String JavaDoc[] outputMethods;
64     //Get the array to be used based on HAVE_BIG_DECIMAL
65
static{
66         if(HAVE_BIG_DECIMAL){
67             outputMethods = new String JavaDoc[] {
68                                     "takesNothing",
69                             
70                                     null,
71                                 
72                                     null,
73                                                                     
74                                     "takesShortPrimitive",
75                                     null,
76                                 
77                                     "takesIntegerPrimitive",
78                                     null,
79                                 
80                                     "takesLongPrimitive",
81                                     null,
82                                 
83                                     "takesFloatPrimitive",
84                                     null,
85                                 
86                                     "takesDoublePrimitive",
87                                     null,
88                                 
89                                     "takesBigDecimal",
90                                 
91                                     "takesByteArray",
92                                     
93                                     "takesString",
94                                     
95                                     "takesDate",
96                                     
97                                     "takesTimestamp",
98                                 
99                                     "takesTime",
100                                 
101                                     null
102                                 };
103         }
104         else{
105             outputMethods = new String JavaDoc[] {
106                                     "takesNothing",
107                             
108                                     null,
109                                 
110                                     null,
111                                                                     
112                                     "takesShortPrimitive",
113                                     null,
114                                 
115                                     "takesIntegerPrimitive",
116                                     null,
117                                 
118                                     "takesLongPrimitive",
119                                     null,
120                                 
121                                     "takesFloatPrimitive",
122                                     null,
123                                 
124                                     "takesDoublePrimitive",
125                                     null,
126                                 
127                                     null,
128                                 
129                                     "takesByteArray",
130                                     
131                                     "takesString",
132                                     
133                                     "takesDate",
134                                     
135                                     "takesTimestamp",
136                                 
137                                     "takesTime",
138                                 
139                                     null
140                                 };
141         }
142     }
143
144     // parameter types for outputMethods.
145
private static final String JavaDoc[] outputProcParam =
146     {
147         null, // "takesNothing",
148

149         null,
150     
151         null,
152             
153         "SMALLINT", // "takesShortPrimitive",
154
null,
155     
156         "INT", // "takesIntegerPrimitive",
157
null,
158     
159         "BIGINT", // "takesLongPrimitive",
160
null,
161     
162         "REAL", // "takesFloatPrimitive",
163
null,
164     
165         "DOUBLE", // "takesDoublePrimitive",
166
null,
167     
168         "DECIMAL(10,4)", // "takesBigDecimal",
169

170         "VARCHAR(40) FOR BIT DATA", // "takesByteArray",
171

172         "VARCHAR(40)", // "takesString",
173

174         "DATE", // "takesDate",
175

176         "TIMESTAMP", // "takesTimestamp",
177

178         "TIME", // "takesTime",
179

180         null
181     };
182         
183     static String JavaDoc returnMethods[];
184     //Get the array to be used based on HAVE_BIG_DECIMAL
185
static{
186         if(HAVE_BIG_DECIMAL){
187             returnMethods = new String JavaDoc[] {
188                                     "returnsNothing",
189                 
190                                     null,
191                                     null,
192                                 
193                                     "returnsShortP",
194                                     null,
195                                 
196                                     "returnsIntegerP",
197                                     null,
198                                 
199                                     "returnsLongP",
200                                     null,
201                                 
202                                     "returnsFloatP",
203                                     null,
204                                 
205                                     "returnsDoubleP",
206                                     null,
207                                 
208                                     "returnsBigDecimal",
209                                 
210                                     "returnsByteArray",
211                                     
212                                     "returnsString",
213                                     
214                                     "returnsDate",
215                                     
216                                     "returnsTimestamp",
217                                 
218                                     "returnsTime",
219                                 
220                                     null
221                                 };
222         }
223         else{
224             returnMethods = new String JavaDoc[] {
225                                     "returnsNothing",
226                 
227                                     null,
228                                     null,
229                                 
230                                     "returnsShortP",
231                                     null,
232                                 
233                                     "returnsIntegerP",
234                                     null,
235                                 
236                                     "returnsLongP",
237                                     null,
238                                 
239                                     "returnsFloatP",
240                                     null,
241                                 
242                                     "returnsDoubleP",
243                                     null,
244                                 
245                                     null,
246                                 
247                                     "returnsByteArray",
248                                     
249                                     "returnsString",
250                                     
251                                     "returnsDate",
252                                     
253                                     "returnsTimestamp",
254                                 
255                                     "returnsTime",
256                                 
257                                     null
258                                 };
259         }
260     }
261     
262     static String JavaDoc[] returnMethodType =
263     {
264         null, // "returnsNothing",
265

266         null, // "returnsBytePrimitive",
267
null, // "returnsByte",
268

269         "SMALLINT", // "returnsShortPrimitive",
270
null, // "returnsShort",
271

272         "INT", // "returnsIntegerPrimitive",
273
null, // "returnsInteger",
274

275         "BIGINT", // "returnsLongPrimitive",
276
null, // "returnsLong",
277

278         "REAL", // "returnsFloatPrimitive",
279
null, // "returnsFloat",
280

281         "DOUBLE", // "returnsDoublePrimitive",
282
null, // "returnsDouble",
283

284         "DECIMAL(10,2)", // "returnsBigDecimal",
285

286         "VARCHAR(40) FOR BIT DATA", // "returnsByteArray",
287

288         "VARCHAR(40)", // "returnsString",
289

290         "DATE", // "returnsDate",
291

292         "TIMESTAMP", // "returnsTimestamp",
293

294         "TIME", // "returnsTime",
295

296         null, // "returnsBigInteger"
297
};
298     
299     //JDBC type (java.sql.Types) corresponding to the methods
300
static int[] paramJDBCType =
301     {
302         Types.NULL, // "returnsNothing",
303

304         Types.NULL, // "returnsBytePrimitive",
305
Types.NULL, // "returnsByte",
306

307         Types.SMALLINT, // "returnsShortPrimitive",
308
Types.NULL, // "returnsShort",
309

310         Types.INTEGER, // "returnsIntegerPrimitive",
311
Types.NULL, // "returnsInteger",
312

313         Types.BIGINT, // "returnsLongPrimitive",
314
Types.NULL, // "returnsLong",
315

316         Types.REAL, // "returnsFloatPrimitive",
317
Types.NULL, // "returnsFloat",
318

319         Types.DOUBLE, // "returnsDoublePrimitive",
320
Types.NULL, // "returnsDouble",
321

322         Types.NUMERIC, // "returnsBigDecimal",
323

324         Types.VARBINARY, // "returnsByteArray",
325

326         Types.VARCHAR, // "returnsString",
327

328         Types.DATE, // "returnsDate",
329

330         Types.TIMESTAMP, // "returnsTimestamp",
331

332         Types.TIME, // "returnsTime",
333

334         Types.NULL, // "returnsBigInteger"
335
};
336     
337     static final int types[] =
338     {
339         Types.BIT,
340         JDBC30Translation.SQL_TYPES_BOOLEAN,
341         Types.TINYINT,
342         Types.SMALLINT,
343         Types.INTEGER,
344         Types.BIGINT,
345         Types.FLOAT,
346         Types.REAL,
347         Types.DOUBLE,
348         Types.NUMERIC,
349         Types.DECIMAL,
350         Types.CHAR,
351         Types.VARCHAR,
352         Types.LONGVARCHAR,
353         Types.DATE,
354         Types.TIME,
355         Types.TIMESTAMP,
356         Types.BINARY,
357         Types.VARBINARY,
358         Types.LONGVARBINARY,
359         Types.OTHER
360     };
361     
362     static final String JavaDoc typeNames[] =
363     {
364         "BIT",
365         "BOOLEAN",
366         "TINYINT",
367         "SMALLINT",
368         "INTEGER",
369         "BIGINT",
370         "FLOAT",
371         "REAL",
372         "DOUBLE",
373         "NUMERIC",
374         "DECIMAL",
375         "CHAR",
376         "VARCHAR",
377         "LONGVARCHAR",
378         "DATE",
379         "TIME",
380         "TIMESTAMP",
381         "BINARY",
382         "VARBINARY",
383         "LONGVARBINARY",
384         "OTHER"
385     };
386
387     //public static Connection conn;
388

389     public static void main (String JavaDoc[] argv) throws Throwable JavaDoc
390     {
391         ij.getPropertyArg(argv);
392         Connection conn = ij.startJBMS();
393
394         runTests( conn);
395     }
396
397     public static void runTests( Connection conn) throws Throwable JavaDoc
398     {
399         conn.setAutoCommit(false);
400
401         testMisc(conn);
402         testNull(conn);
403         testUpdate(conn);
404         testEachOutputType(conn);
405         testReturnTypes(conn);
406         testOtherOutputType(conn);
407         testManyOut(conn);
408         //Uses a procedure with nested connection - Cannot be tested with JSR169
409
if(HAVE_DRIVER_CLASS)
410             test5116(conn);
411     }
412
413     private static void testMisc(Connection conn) throws Throwable JavaDoc
414     {
415         System.out.println("==============================================");
416         System.out.println("TESTING BOUNDARY CONDITIONS");
417         System.out.println("==============================================\n");
418
419         Statement scp = conn.createStatement();
420
421         scp.execute("CREATE PROCEDURE takesString(OUT P1 VARCHAR(40), IN P2 INT) " +
422                         "EXTERNAL NAME '" + CLASS_NAME + "takesString'" +
423                         " NO SQL LANGUAGE JAVA PARAMETER STYLE JAVA");
424
425         CallableStatement cs = conn.prepareCall("call takesString(?,?)");
426
427         // register a normal int as an output param, should fail
428
boolean failed = false;
429         try
430         {
431             cs.registerOutParameter(2, Types.INTEGER);
432         }
433         catch (SQLException se)
434         {
435             failed = true;
436             System.out.println("Expected exception "+se);
437         }
438         if (!failed)
439         {
440             System.out.println("registerOutParameter on non-output didn't fail");
441         }
442
443         // invalid param number
444
failed = false;
445         try
446         {
447             cs.registerOutParameter(9, Types.INTEGER);
448         }
449         catch (SQLException se)
450         {
451             failed = true;
452             System.out.println("Expected exception "+se);
453         }
454         if (!failed)
455         {
456             System.out.println("registerOutParameter on bad value didn't fail");
457         }
458
459         // invalid param number
460
failed = false;
461         try
462         {
463             cs.registerOutParameter(0, Types.INTEGER);
464         }
465         catch (SQLException se)
466         {
467             failed = true;
468             System.out.println("Expected exception "+se);
469         }
470         if (!failed)
471         {
472             System.out.println("registerOutParameter on bad value didn't fail");
473         }
474
475         // set before register, bad type, should fail as is output parameter.
476
try
477         {
478             cs.setDouble(1, 1);
479             System.out.println("FAIL setDouble() on takesString() accepted");
480         }
481         catch (SQLException se)
482         {
483             System.out.println("Expected exception "+se);
484         }
485
486         // set before register, should fail as is output parameter.
487
try
488         {
489             cs.setString(1, "hello");
490             System.out.println("FAIL setString() on takesString() accepted");
491         }
492         catch (SQLException se)
493         {
494             System.out.println("Expected exception "+se);
495         }
496
497         cs.registerOutParameter(1, Types.CHAR);
498         cs.setInt(2, Types.INTEGER);
499         try
500         {
501             cs.execute();
502         }
503         catch (SQLException se)
504         {
505             System.out.println("cs.execute() got unexpected exception: "+se);
506         }
507
508         // shouldn't have to reregister the type, and shouldn't
509
// need to set the output parameters
510
cs.clearParameters();
511         cs.setInt(2, Types.INTEGER);
512         try
513         {
514             cs.execute();
515         }
516         catch (SQLException se)
517         {
518             System.out.println("cs.execute() got unexpected exception: "+se);
519         }
520         cs.close();
521         scp.execute("DROP PROCEDURE takesString");
522
523         scp.execute("CREATE FUNCTION returnsString(P2 INT) RETURNS VARCHAR(40) " +
524                         "EXTERNAL NAME '" + CLASS_NAME + "returnsString'" +
525                         " NO SQL LANGUAGE JAVA PARAMETER STYLE JAVA");
526         // return output params -- cannot do set on return output param
527
cs = conn.prepareCall("? = call returnsString(?)");
528         try
529         {
530             cs.setString(1, new String JavaDoc("test"));
531             System.out.println("ERROR: setString() on return output parameter succeeded");
532         }
533         catch (SQLException se)
534         {
535             System.out.println("Expected exception on setString() on a return output param: "+se);
536         }
537         cs.close();
538         scp.execute("DROP FUNCTION returnsString");
539
540         // lets try ? = call syntax on a call that doesn't return anything
541

542         scp.execute("CREATE PROCEDURE returnsNothing() " +
543                         "EXTERNAL NAME '" + CLASS_NAME + "returnsNothing'" +
544                         " NO SQL LANGUAGE JAVA PARAMETER STYLE JAVA");
545         try
546         {
547             cs = conn.prepareCall("? = call returnsNothing()");
548             System.out.println("ERROR: no exception on prepare of '? = call returnsNothing()");
549         }
550         catch (SQLException se)
551         {
552             System.out.println("Expected exception on prepare of '? = call returnsNothing()': "+se);
553         }
554         scp.execute("DROP PROCEDURE returnsNothing");
555     }
556
557     
558     private static void testNull(Connection conn) throws Throwable JavaDoc
559     {
560         System.out.println("==============================================");
561         System.out.println("TESTING NULLS");
562         System.out.println("==============================================\n");
563         System.out.println("Test for bug 4317, passing null value for a parameter");
564
565         Statement scp = conn.createStatement();
566
567         scp.execute("CREATE PROCEDURE testNullBug4317(IN P1 VARCHAR(10)) " +
568                         "EXTERNAL NAME '" + CLASS_NAME + "testNullBug4317'" +
569                         " NO SQL LANGUAGE JAVA PARAMETER STYLE JAVA");
570
571
572         CallableStatement cs0 = conn.prepareCall("call testNullBug4317(?)");
573         try
574         {
575             cs0.setString(1, null); // passing in null
576
cs0.execute();
577         }
578         catch (SQLException se)
579         {
580             System.out.println("cs0.execute() got unexpected exception: "+se);
581         }
582
583         try
584         {
585             // BUG 5928 - setNull throws an exception - fixed.
586
cs0.setNull(1, java.sql.Types.VARCHAR); // passing in null
587
cs0.execute();
588         }
589         catch (SQLException se)
590         {
591             System.out.println("cs0.execute() got unexpected exception: "+se);
592         }
593         cs0.close();
594         scp.execute("DROP PROCEDURE testNullBug4317");
595
596
597     }
598
599     // test: do we get an appropriate update count when using ?=call?
600
private static void testUpdate(Connection conn) throws Throwable JavaDoc
601     {
602         System.out.println("==============================================");
603         System.out.println("TESTING UPDATE COUNT");
604         System.out.println("==============================================\n");
605
606         Statement scp = conn.createStatement();
607
608         scp.execute("CREATE FUNCTION returnsIntegerP(P1 INT) RETURNS INTEGER " +
609                         "EXTERNAL NAME '" + CLASS_NAME + "returnsIntegerP'" +
610                         " NO SQL LANGUAGE JAVA PARAMETER STYLE JAVA");
611
612
613
614         CallableStatement cs = conn.prepareCall("? = call returnsIntegerP(0)");
615         cs.registerOutParameter(1, Types.INTEGER);
616         try
617         {
618             int updCount = cs.executeUpdate();
619             System.out.println("executeUpdate on ? = call returnsIntegerP returned "+updCount);
620             System.out.println("getString(1) returned "+cs.getString(1));
621         }
622         catch (SQLException se)
623         {
624             System.out.println("cs.execute() got unexpected exception: "+se);
625         }
626
627         cs.close();
628         scp.execute("DROP FUNCTION returnsIntegerP");
629         scp.close();
630     }
631
632     // should do get setString() to use a string that is appropriate for
633
// the target type
634
private static void testEachOutputType(Connection conn) throws Throwable JavaDoc
635     {
636         System.out.println("==============================================");
637         System.out.println("TESTING NORMAL OUTPUT PARAMETERS");
638         System.out.println("==============================================\n");
639         CallableStatement cs = null;
640
641         for (int doSetObject = 0; doSetObject < 3; doSetObject++)
642         {
643             switch (doSetObject)
644             {
645                 case 0:
646                     System.out.println("...starting doing setXXX for each type xxx");
647                     break;
648                 case 1:
649                     System.out.println("...now doing setObject on each type xxx");
650                     break;
651                 case 2:
652                     System.out.println("...not doing any setXXX, just OUT parameters, not IN/OUT");
653                     break;
654             }
655
656             for (int method = 0; method < outputMethods.length; method++)
657             {
658                 String JavaDoc methodName = outputMethods[method];
659                 if (methodName == null)
660                     continue;
661
662                 System.out.println("\n------------------------------------");
663
664                 Statement scp = conn.createStatement();
665                 String JavaDoc str;
666                 if (methodName.indexOf("Nothing") == -1)
667                 {
668
669                     scp.execute("CREATE PROCEDURE " + methodName + "(INOUT P1 " + outputProcParam[method] + ", IN P2 INT) " +
670                         "EXTERNAL NAME '" + CLASS_NAME + "" + methodName +
671                         "' NO SQL LANGUAGE JAVA PARAMETER STYLE JAVA");
672
673
674                     if (method%2 == 0)
675                         str = "{call "+methodName+"(?,?)}";
676                     else
677                         str = "call "+methodName+"(?,?)";
678                 }
679                 else
680                 {
681                     scp.execute("CREATE PROCEDURE " + methodName + "() " +
682                         "EXTERNAL NAME '" + CLASS_NAME + "" + methodName +
683                         "' NO SQL LANGUAGE JAVA PARAMETER STYLE JAVA");
684                     str = "{call "+methodName+"()}";
685                 }
686
687
688     
689                 System.out.println(str);
690                 try
691                 {
692                     cs = conn.prepareCall(str);
693                 }
694                 catch (SQLException se)
695                 {
696                     System.out.println("ERROR: unexpected exception "+se);
697                     throw se;
698                 }
699     
700                 for (int type = 0; type < types.length; type++)
701                 {
702                     cs.clearParameters();
703                     System.out.println();
704                     try
705                     {
706                         System.out.println("\n\tcs.registerOutParameter(1, "+typeNames[type]+")");
707                         cs.registerOutParameter(1, types[type]);
708                     }
709                     catch (SQLException se)
710                     {
711                         System.out.println("\tException "+se);
712                         continue;
713                     }
714     
715                     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
716                     try
717                     {
718                         if (doSetObject == 0)
719                         {
720                             callSetMethod(cs, 1, types[type], buf);
721                         }
722                         else if (doSetObject == 1)
723                         {
724                             callSetObject(cs, 1, types[type], buf);
725                         }
726                         else
727                         {
728                             // only try this once
729
type = types.length-1;
730                             buf.append("...no setXXX(1) at all");
731                         }
732                     }
733                     catch (SQLException se)
734                     {
735                         System.out.println("\t"+buf.toString());
736                         System.out.println("\tException "+se);
737                         continue;
738                     }
739                     System.out.println("\t"+buf.toString());
740                     cs.setInt(2, types[type]);
741     
742                     try
743                     {
744                         System.out.println("\tcs.execute()");
745                         boolean hasResultSet = cs.execute();
746                         if (hasResultSet)
747                             System.out.println("testEachOutputType HAS RESULT SET cs.execute() returned true");
748                         }
749                     catch (SQLException se)
750                     {
751                         System.out.println("\tException "+se);
752                         continue;
753                     }
754                     for (int getType = 0; getType < types.length; getType++)
755                     {
756                         StringBuffer JavaDoc getbuf = new StringBuffer JavaDoc();
757                         try
758                         {
759                             callGetMethod(cs, 1, types[getType], paramJDBCType[method], getbuf);
760                         }
761                         catch (SQLException se)
762                         {
763                             getbuf.append(se);
764                         }
765                         System.out.println("\t\t\t"+getbuf.toString());
766                     }
767     
768                 }
769
770                 cs.close();
771
772                 scp.execute("DROP PROCEDURE " + methodName);
773                 scp.close();
774             }
775         }
776
777         System.out.println("------------------------------------\n");
778
779     }
780
781     // test that everything works ok when we regsiter the param as type OTHER.
782
// should be able to get/setXXX of the appropriate type
783
private static void testOtherOutputType(Connection conn) throws Throwable JavaDoc
784     {
785         System.out.println("==============================================");
786         System.out.println("TESTING OUTPUT PARAMETERS WITH register(OTHER)");
787         System.out.println("==============================================\n");
788         CallableStatement cs = null;
789
790         for (int method = 0; method < outputMethods.length; method++)
791         {
792             String JavaDoc methodName = outputMethods[method];
793             if (methodName == null)
794                 continue;
795             System.out.println("\n------------------------------------");
796
797
798             Statement scp = conn.createStatement();
799             String JavaDoc str;
800             if (methodName.indexOf("Nothing") == -1)
801             {
802
803                 scp.execute("CREATE PROCEDURE " + methodName + "(INOUT P1 " + outputProcParam[method] + ", IN P2 INT) " +
804                     "EXTERNAL NAME '" + CLASS_NAME + "" + methodName +
805                     "' NO SQL LANGUAGE JAVA PARAMETER STYLE JAVA");
806
807
808                 if (method%2 == 0)
809                     str = "{call "+methodName+"(?,?)}";
810                 else
811                     str = "call "+methodName+"(?,?)";
812             }
813             else
814             {
815                 scp.execute("CREATE PROCEDURE " + methodName + "() " +
816                     "EXTERNAL NAME '" + CLASS_NAME + "" + methodName +
817                     "' NO SQL LANGUAGE JAVA PARAMETER STYLE JAVA");
818                 str = "{call "+methodName+"()}";
819             }
820
821             System.out.println(str);
822             try
823             {
824                 cs = conn.prepareCall(str);
825             }
826             catch (SQLException se)
827             {
828                 System.out.println("ERROR: unexpected exception "+se);
829                 throw se;
830             }
831     
832             for (int type = 0; type < types.length; type++)
833             {
834                 cs.clearParameters();
835                 System.out.println();
836                 try
837                 {
838                     System.out.println("\n\tcs.registerOutParameter(1, Types.OTHER)");
839                     cs.registerOutParameter(1, Types.OTHER);
840                 }
841                 catch (SQLException se)
842                 {
843                     System.out.println("\tException "+se);
844                     continue;
845                 }
846
847                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
848                 try
849                 {
850                     callSetMethod(cs, 1, types[type], buf);
851                 }
852                 catch (SQLException se)
853                 {
854                     System.out.println("\t"+buf.toString());
855                     System.out.println("\tException "+se);
856                     continue;
857                 }
858                 System.out.println("\t"+buf.toString());
859                 cs.setInt(2, types[type]);
860
861                 try
862                 {
863                     System.out.println("\tcs.execute()");
864                     cs.execute();
865                 }
866                 catch (SQLException se)
867                 {
868                     System.out.println("\tException "+se);
869                     continue;
870                 }
871                 for (int getType = 0; getType < types.length; getType++)
872                 {
873                     StringBuffer JavaDoc getbuf = new StringBuffer JavaDoc();
874                     try
875                     {
876                         callGetMethod(cs, 1, types[getType], Types.OTHER , getbuf);
877                     }
878                     catch (SQLException se)
879                     {
880                         getbuf.append(se);
881                     }
882                     System.out.println("\t\t\t"+getbuf.toString());
883                 }
884
885             }
886
887             cs.close();
888
889             scp.execute("DROP PROCEDURE " + methodName);
890             scp.close();
891         }
892
893         System.out.println("------------------------------------\n");
894     }
895
896     private static void testReturnTypes(Connection conn) throws Throwable JavaDoc
897     {
898         System.out.println("==============================================\n");
899         System.out.println("TESTING RETURN OUTPUT PARAMETERS");
900         System.out.println("==============================================\n");
901         CallableStatement cs = null;
902         for (int method = 0; method < returnMethods.length; method++)
903         {
904             String JavaDoc methodName = returnMethods[method];
905             if (methodName == null)
906                 continue;
907
908             Statement scf = conn.createStatement();
909             String JavaDoc str;
910             String JavaDoc dropRoutine;
911             if (methodName.indexOf("Nothing") != -1)
912             {
913
914                 scf.execute("CREATE PROCEDURE " + methodName + "()" +
915                     " EXTERNAL NAME '" + CLASS_NAME + "" + methodName +
916                     "' NO SQL LANGUAGE JAVA PARAMETER STYLE JAVA");
917
918
919                 dropRoutine = "DROP PROCEDURE " + methodName;
920
921                 str = "{call "+returnMethods[method]+"()}";
922             }
923             else
924             {
925
926                 scf.execute("CREATE FUNCTION " + methodName + "(P1 INT) RETURNS " + returnMethodType[method] +
927                     " EXTERNAL NAME '" + CLASS_NAME + "" + methodName +
928                     "' NO SQL LANGUAGE JAVA PARAMETER STYLE JAVA");
929                 dropRoutine = "DROP FUNCTION " + methodName;
930
931                 str = "{? = call "+returnMethods[method]+"(?)}";
932             }
933
934
935                 
936             System.out.println("\n------------------------------------");
937                 
938
939             System.out.println(str);
940             try
941             {
942                 cs = conn.prepareCall(str);
943             }
944             catch (SQLException se)
945             {
946                 System.out.println("ERROR: unexpected exception "+se);
947                 throw se;
948             }
949
950             for (int type = 0; type < types.length; type++)
951             {
952                 cs.clearParameters();
953                 System.out.println();
954                 try
955                 {
956                     System.out.println("\n\tcs.registerOutParameter(1, "+typeNames[type]+")");
957                     cs.registerOutParameter(1, types[type]);
958                 }
959                 catch (SQLException se)
960                 {
961                     System.out.println("\tException "+se);
962                     continue;
963                 }
964                 try
965                 {
966                     cs.setInt(2, types[type]);
967                 }
968                 catch (SQLException se)
969                 {
970                     System.out.println("\tUnexpected exception on cs.setInt(2, "+types[type]+"): "+se);
971                     continue;
972                 }
973
974                 try
975                 {
976                     System.out.println("\tcs.execute()");
977                     boolean hasResultSet = cs.execute();
978                     if (hasResultSet)
979                         System.out.println("testReturnTypes HAS RESULT SET cs.execute() returned true");
980
981                 }
982                 catch (SQLException se)
983                 {
984                     System.out.println("\tException "+se);
985                     continue;
986                 }
987                 for (int getType = 0; getType < types.length; getType++)
988                 {
989                     StringBuffer JavaDoc getbuf = new StringBuffer JavaDoc();
990                     try
991                     {
992                         callGetMethod(cs, 1, types[getType], paramJDBCType[method], getbuf);
993                     }
994                     catch (SQLException se)
995                     {
996                         getbuf.append(se);
997                     }
998                     System.out.println("\t\t\t"+getbuf.toString());
999                 }
1000
1001            }
1002
1003            cs.close();
1004            scf.execute(dropRoutine);
1005            scf.close();
1006        }
1007
1008        System.out.println("------------------------------------\n");
1009
1010    }
1011
1012    private static void callSetObject(CallableStatement cs, int arg, int type, StringBuffer JavaDoc strbuf) throws Throwable JavaDoc
1013    {
1014
1015        switch (type)
1016        {
1017            case Types.BIT:
1018            case JDBC30Translation.SQL_TYPES_BOOLEAN:
1019                strbuf.append("setObject("+arg+", true)");
1020                cs.setObject(arg, new Boolean JavaDoc(true));
1021                break;
1022
1023            case Types.TINYINT:
1024                strbuf.append("setObject("+arg+", 6)");
1025                cs.setObject(arg, new Integer JavaDoc((byte)6));
1026                break;
1027
1028            case Types.SMALLINT:
1029                strbuf.append("setObject("+arg+", 66)");
1030                cs.setObject(arg, new Integer JavaDoc((short)66));
1031                break;
1032
1033            case Types.INTEGER:
1034                strbuf.append("setObject("+arg+", 666)");
1035                cs.setObject(arg, new Integer JavaDoc(666));
1036                break;
1037
1038            case Types.BIGINT:
1039                strbuf.append("setObject("+arg+", 666)");
1040                cs.setObject(arg, new Long JavaDoc(666));
1041                break;
1042
1043            case Types.FLOAT:
1044            case Types.REAL:
1045                strbuf.append("setObject("+arg+", 666)");
1046                cs.setObject(arg, new Float JavaDoc(666));
1047                break;
1048
1049            case Types.DOUBLE:
1050                strbuf.append("setObject("+arg+", 666)");
1051                cs.setObject(arg, new Double JavaDoc(666));
1052                break;
1053
1054            case Types.DECIMAL:
1055            case Types.NUMERIC:
1056                strbuf.append("setObject("+arg+", 666.666)");
1057                BigDecimalHandler.setObjectString(cs,arg,"666.666");
1058                break;
1059
1060            case Types.CHAR:
1061            case Types.VARCHAR:
1062            case Types.LONGVARCHAR:
1063                strbuf.append("setObject("+arg+", \"Set via setString()\")");
1064                cs.setObject(arg, "Set via setString()");
1065                break;
1066
1067            case Types.BINARY:
1068            case Types.VARBINARY:
1069            case Types.LONGVARBINARY:
1070                strbuf.append("setObject("+arg+", byte[])");
1071                byte[] myarray = new byte[16];
1072                myarray[0] = (byte)255;
1073                cs.setObject(arg, myarray);
1074                break;
1075
1076            case Types.DATE:
1077                strbuf.append("setObject("+arg+", Date.valueOf(1999-09-09))");
1078                cs.setObject(arg, Date.valueOf("1999-09-09"));
1079                break;
1080
1081            case Types.TIME:
1082                strbuf.append("setObject("+arg+", Time.valueOf(09:09:09))");
1083                cs.setObject(arg, Time.valueOf("09:09:09"));
1084                break;
1085
1086            case Types.TIMESTAMP:
1087                strbuf.append("setObject("+arg+", Timestamp.valueOf(1999-09-09 09:09:09.999))");
1088                cs.setObject(arg, Timestamp.valueOf("1999-09-09 09:09:09.999"));
1089                break;
1090
1091            case Types.OTHER:
1092                strbuf.append("setObject("+arg+", new BigInteger(666))");
1093                cs.setObject(arg, new BigInteger JavaDoc("666"));
1094                break;
1095
1096            default:
1097                throw new Throwable JavaDoc("TEST ERROR: unexpected type "+type);
1098        }
1099    }
1100    private static void callSetMethod(CallableStatement cs, int arg, int type, StringBuffer JavaDoc strbuf) throws Throwable JavaDoc
1101    {
1102        switch (type)
1103        {
1104            case Types.BIT:
1105            case JDBC30Translation.SQL_TYPES_BOOLEAN:
1106                strbuf.append("setBoolean("+arg+", true)");
1107                cs.setBoolean(arg, true);
1108                break;
1109
1110            case Types.TINYINT:
1111                strbuf.append("setByte("+arg+", 6)");
1112                cs.setByte(arg, (byte)6);
1113                break;
1114
1115            case Types.SMALLINT:
1116                strbuf.append("setShort("+arg+", 66)");
1117                cs.setShort(arg, (short)66);
1118                break;
1119
1120            case Types.INTEGER:
1121                strbuf.append("setInt("+arg+", 666)");
1122                cs.setInt(arg, 666);
1123                break;
1124
1125            case Types.BIGINT:
1126                strbuf.append("setLong("+arg+", 666)");
1127                cs.setLong(arg, 666);
1128                break;
1129
1130            case Types.FLOAT:
1131            case Types.REAL:
1132                strbuf.append("setFLoat("+arg+", 666)");
1133                cs.setFloat(arg, 666);
1134                break;
1135
1136            case Types.DOUBLE:
1137                strbuf.append("setDouble("+arg+", 666)");
1138                cs.setDouble(arg, 666);
1139                break;
1140
1141            case Types.DECIMAL:
1142            case Types.NUMERIC:
1143                strbuf.append("setBigDecimal("+arg+", 666.666)");
1144                BigDecimalHandler.setBigDecimalString(cs,arg,"666.666");
1145                break;
1146
1147            case Types.CHAR:
1148            case Types.VARCHAR:
1149            case Types.LONGVARCHAR:
1150                strbuf.append("setString("+arg+", \"Set via setString()\")");
1151                cs.setString(arg, "Set via setString()");
1152                break;
1153
1154            case Types.BINARY:
1155            case Types.VARBINARY:
1156            case Types.LONGVARBINARY:
1157                strbuf.append("setBytes("+arg+", byte[])");
1158                byte[] myarray = new byte[16];
1159                myarray[0] = (byte)255;
1160                cs.setBytes(arg, myarray);
1161                break;
1162
1163            case Types.DATE:
1164                strbuf.append("setDate("+arg+", Date.valueOf(1999-09-09))");
1165                cs.setDate(arg, Date.valueOf("1999-09-09"));
1166                break;
1167
1168            case Types.TIME:
1169                strbuf.append("setTime("+arg+", Time.valueOf(09:09:09))");
1170                cs.setTime(arg, Time.valueOf("09:09:09"));
1171                break;
1172
1173            case Types.TIMESTAMP:
1174                strbuf.append("setTimestamp("+arg+", Timestamp.valueOf(1999-09-09 09:09:09.999))");
1175                cs.setTimestamp(arg, Timestamp.valueOf("1999-09-09 09:09:09.999"));
1176                break;
1177
1178            case Types.OTHER:
1179                strbuf.append("setObject("+arg+", new BigInteger(666))");
1180                cs.setObject(arg, new BigInteger JavaDoc("666"));
1181                break;
1182
1183            default:
1184                throw new Throwable JavaDoc("TEST ERROR: unexpected type "+type);
1185        }
1186    }
1187
1188    private static void callGetMethod(CallableStatement cs, int arg, int type, int paramType ,StringBuffer JavaDoc strbuf) throws Throwable JavaDoc
1189    {
1190        switch (type)
1191        {
1192            case Types.BIT:
1193            case JDBC30Translation.SQL_TYPES_BOOLEAN:
1194                strbuf.append("getBoolean("+arg+") = ");
1195                strbuf.append(cs.getBoolean(arg));
1196                break;
1197
1198            case Types.TINYINT:
1199                strbuf.append("getByte("+arg+") = ");
1200                strbuf.append(Byte.toString(cs.getByte(arg)));
1201                break;
1202
1203            case Types.SMALLINT:
1204                strbuf.append("getShort("+arg+") = ");
1205                strbuf.append(Short.toString(cs.getShort(arg)));
1206                break;
1207
1208            case Types.INTEGER:
1209                strbuf.append("getInt("+arg+") = ");
1210                strbuf.append(Integer.toString(cs.getInt(arg)));
1211                break;
1212
1213            case Types.BIGINT:
1214                strbuf.append("getLong("+arg+") = ");
1215                strbuf.append(Long.toString(cs.getLong(arg)));
1216                break;
1217
1218            case Types.FLOAT:
1219            case Types.REAL:
1220                strbuf.append("getFloat("+arg+") = ");
1221                strbuf.append(Float.toString(cs.getFloat(arg)));
1222                break;
1223
1224            case Types.DOUBLE:
1225                strbuf.append("getDouble("+arg+") = ");
1226                strbuf.append(Double.toString(cs.getDouble(arg)));
1227                break;
1228
1229            case Types.DECIMAL:
1230            case Types.NUMERIC:
1231                strbuf.append("getBigDecimal("+arg+") = ");
1232                strbuf.append(BigDecimalHandler.getBigDecimalString(cs,arg,paramType));
1233                break;
1234
1235            case Types.CHAR:
1236            case Types.VARCHAR:
1237            case Types.LONGVARCHAR:
1238                strbuf.append("getString("+arg+") = ");
1239                String JavaDoc s = cs.getString(arg);
1240                if (s.startsWith("[B@"))
1241                    s = "byte[] reference";
1242                strbuf.append(s);
1243                break;
1244
1245            case Types.BINARY:
1246            case Types.VARBINARY:
1247            case Types.LONGVARBINARY:
1248                strbuf.append("getBytes("+arg+") = ");
1249                byteArrayToString(cs.getBytes(arg), strbuf);
1250                break;
1251
1252            case Types.DATE:
1253                strbuf.append("getDate("+arg+") = ");
1254                Date date = cs.getDate(arg);
1255                strbuf.append(date == null ? "null" : date.toString());
1256                break;
1257
1258            case Types.TIME:
1259                strbuf.append("getTime("+arg+") = ");
1260                Time time = cs.getTime(arg);
1261                strbuf.append(time == null ? "null" : time.toString());
1262                break;
1263
1264            case Types.TIMESTAMP:
1265                strbuf.append("getTimestamp("+arg+") = ");
1266                Timestamp timestamp = cs.getTimestamp(arg);
1267                strbuf.append(timestamp == null ? "null" : timestamp.toString());
1268                break;
1269
1270            case Types.OTHER:
1271                strbuf.append("getObject("+arg+") = ");
1272                Object JavaDoc o = cs.getObject(arg);
1273                if (o == null)
1274                {
1275                    strbuf.append("null");
1276                }
1277                else if (o instanceof byte[])
1278                {
1279                    byteArrayToString((byte[])o, strbuf);
1280                }
1281                else
1282                {
1283                    strbuf.append(o.toString());
1284                }
1285
1286                break;
1287
1288            default:
1289                throw new Throwable JavaDoc("TEST ERROR: unexpected type "+type);
1290        }
1291    }
1292
1293    static private void byteArrayToString(byte[] barray, StringBuffer JavaDoc strbuf)
1294    {
1295        if (barray == null)
1296        {
1297            strbuf.append("null");
1298        }
1299        else
1300        {
1301            for (int i = 0; i<barray.length; i++)
1302            {
1303                strbuf.append(barray[i]);
1304            }
1305        }
1306    }
1307
1308    private static String JavaDoc getStringOfType(int type) throws Throwable JavaDoc
1309    {
1310        switch (type)
1311        {
1312            case Types.CHAR:
1313            case Types.VARCHAR:
1314            case Types.LONGVARCHAR:
1315                return "I am a string";
1316
1317            case Types.TINYINT:
1318            case Types.SMALLINT:
1319            case Types.INTEGER:
1320            case Types.BIGINT:
1321            case Types.OTHER: // other is bigInt
1322
return "3";
1323
1324            case Types.FLOAT:
1325            case Types.REAL:
1326            case Types.DECIMAL:
1327            case Types.NUMERIC:
1328                return "3.33";
1329
1330            case Types.DATE:
1331                return "1933-03-03";
1332
1333            case Types.TIME:
1334                return "03:03:03";
1335
1336            case Types.TIMESTAMP:
1337                return "1933-03-03 03:03:03.333";
1338
1339            case Types.BINARY:
1340            case Types.VARBINARY:
1341            case Types.LONGVARBINARY:
1342                return "00680065006c006c006f";
1343
1344            case Types.BIT:
1345            case JDBC30Translation.SQL_TYPES_BOOLEAN:
1346                return "true";
1347
1348            default:
1349                throw new Throwable JavaDoc("bad type "+type);
1350        }
1351    }
1352
1353    /////////////////////////////////////////////////////////////
1354
//
1355
// OUTPUT PARAMETER METHODS
1356
//
1357
/////////////////////////////////////////////////////////////
1358
public static void testNull(Boolean JavaDoc passedInNull, Boolean JavaDoc setToNull, Integer JavaDoc[] retval) throws Throwable JavaDoc
1359    {
1360        if (passedInNull.booleanValue())
1361        {
1362            if (retval[0] != null)
1363            {
1364                throw new Throwable JavaDoc("testNull() got a non-null param when it should have been null");
1365            }
1366        }
1367
1368        retval[0] = (setToNull.booleanValue()) ? null : new Integer JavaDoc((short)66);
1369    }
1370
1371    public static void testNullBug4317(String JavaDoc passedInNull) throws Throwable JavaDoc
1372    {
1373    }
1374
1375    public static void takesNothing()
1376    {
1377    }
1378    public static void takesBytePrimitive(byte[] outparam, int type)
1379    {
1380        outparam[0]+=outparam[0];
1381    }
1382    public static void takesByte(Byte JavaDoc[] outparam, int type)
1383    {
1384        outparam[0] = new Byte JavaDoc((byte)(outparam[0] == null ? 33 : outparam[0].byteValue()*2));
1385    }
1386
1387    public static void takesShortPrimitive(short[] outparam, int type)
1388    {
1389        outparam[0]+=outparam[0];
1390    }
1391    public static void takesShort(Short JavaDoc[] outparam, int type)
1392    {
1393        outparam[0] = new Short JavaDoc((byte)(outparam[0] == null ? 33 : outparam[0].shortValue()*2));
1394    }
1395
1396    public static void takesIntegerPrimitive(int[] outparam, int type)
1397    {
1398        outparam[0]+=outparam[0];
1399    }
1400    public static void takesInteger(Integer JavaDoc[] outparam, int type)
1401    {
1402        outparam[0] = new Integer JavaDoc(outparam[0] == null ? 33 : outparam[0].intValue()*2);
1403    }
1404
1405    public static void takesLongPrimitive(long[] outparam, int type)
1406    {
1407        outparam[0]+=outparam[0];
1408    }
1409    public static void takesLong(Long JavaDoc[] outparam, int type)
1410    {
1411        outparam[0] = new Long JavaDoc(outparam[0] == null ? 33 : outparam[0].longValue()*2);
1412    }
1413
1414    public static void takesDoublePrimitive(double[] outparam, int type)
1415    {
1416        outparam[0]+=outparam[0];
1417    }
1418    public static void takesDouble(Double JavaDoc[] outparam, int type)
1419    {
1420        outparam[0] = new Double JavaDoc(outparam[0] == null ? 33 : outparam[0].doubleValue()*2);
1421    }
1422
1423    public static void takesFloatPrimitive(float[] outparam, int type)
1424    {
1425        outparam[0]+=outparam[0];
1426    }
1427    public static void takesFloat(Float JavaDoc[] outparam, int type)
1428    {
1429        outparam[0] = new Float JavaDoc(outparam[0] == null ? 33 : outparam[0].floatValue()*2);
1430    }
1431
1432    public static void takesBooleanPrimitive(boolean[] outparam, int type)
1433    {
1434        outparam[0] = true;
1435    }
1436    public static void takesBoolean(Boolean JavaDoc[] outparam, int type)
1437    {
1438        outparam[0] = new Boolean JavaDoc(true);
1439    }
1440
1441    public static void takesByteArray(byte[][] outparam, int type)
1442    {
1443        byte[] myarray = new byte[16];
1444        myarray[0] = (byte)255;
1445        outparam[0] = myarray;
1446    }
1447
1448    public static void takesDate(Date[] outparam, int type)
1449    {
1450        outparam[0] = Date.valueOf("1966-06-06");
1451    }
1452
1453    public static void takesTime(Time[] outparam, int type)
1454    {
1455        outparam[0] = Time.valueOf("06:06:06");
1456    }
1457
1458    public static void takesTimestamp(Timestamp[] outparam, int type)
1459    {
1460        outparam[0] = Timestamp.valueOf("1966-06-06 06:06:06.666");
1461    }
1462
1463    public static void takesString(String JavaDoc[] outparam, int type) throws Throwable JavaDoc
1464    {
1465        outparam[0] = getStringOfType(type);
1466    }
1467
1468    public static void takesBigInteger(BigInteger JavaDoc[] outparam, int type)
1469    {
1470        outparam[0] = (outparam[0] == null ? new BigInteger JavaDoc("33") : outparam[0].add(outparam[0]));
1471    }
1472    
1473    
1474    /////////////////////////////////////////////////////////////
1475
//
1476
// RETURN PARAMETER METHODS
1477
//
1478
/////////////////////////////////////////////////////////////
1479
public static void returnsNothing()
1480    {
1481    }
1482
1483    public static byte returnsByteP(int type)
1484    {
1485        return 66;
1486    }
1487    public static Byte JavaDoc returnsByte(int type)
1488    {
1489        return new Byte JavaDoc((byte)66);
1490    }
1491
1492    public static short returnsShortP(int type)
1493    {
1494        return 666;
1495    }
1496    public static Short JavaDoc returnsShort(int type)
1497    {
1498        return new Short JavaDoc((short)666);
1499    }
1500
1501    public static int returnsIntegerP(int type)
1502    {
1503        return 666;
1504    }
1505    public static Integer JavaDoc returnsInteger(int type)
1506    {
1507        return new Integer JavaDoc(666);
1508    }
1509
1510    public static long returnsLongP(int type)
1511    {
1512        return 666;
1513    }
1514    public static Long JavaDoc returnsLong(int type)
1515    {
1516        return new Long JavaDoc(666);
1517    }
1518
1519    public static float returnsFloatP(int type)
1520    {
1521        return 666;
1522    }
1523    public static Float JavaDoc returnsFloat(int type)
1524    {
1525        return new Float JavaDoc(666);
1526    }
1527
1528    public static double returnsDoubleP(int type)
1529    {
1530        return 666;
1531    }
1532    public static Double JavaDoc returnsDouble(int type)
1533    {
1534        return new Double JavaDoc(666);
1535    }
1536
1537    public static byte[] returnsByteArray(int type)
1538    {
1539        byte[] myarray = new byte[16];
1540        myarray[0] = (byte)255;
1541        return myarray;
1542    }
1543
1544    public static String JavaDoc returnsString(int type) throws Throwable JavaDoc
1545    {
1546        return getStringOfType(type);
1547    }
1548
1549    public static Date returnsDate(int type)
1550    {
1551        return Date.valueOf("1966-06-06");
1552    }
1553
1554    public static Time returnsTime(int type)
1555    {
1556        return Time.valueOf("06:06:06");
1557    }
1558
1559    public static Timestamp returnsTimestamp(int type)
1560    {
1561        return Timestamp.valueOf("1966-06-06 06:06:06.666");
1562    }
1563
1564    public static BigInteger JavaDoc returnsBigInteger(int type)
1565    {
1566        return new BigInteger JavaDoc("666");
1567    }
1568
1569
1570    // these come from the performance test JDBC.Parameters that was failing
1571
private static void testManyOut(Connection conn) throws SQLException {
1572
1573        System.out.println("start testManyOut");
1574
1575        Statement scp = conn.createStatement();
1576
1577        scp.execute("CREATE PROCEDURE OP_OUT " +
1578            "(OUT I1 INT, OUT I2 INT, OUT I3 INT, OUT I4 INT, OUT I5 INT, "+
1579            "OUT V1 VARCHAR(40), OUT V2 VARCHAR(40), OUT V3 VARCHAR(40), OUT V4 VARCHAR(40), OUT V5 VARCHAR(40)) "+
1580
1581            "EXTERNAL NAME '" + CLASS_NAME + "output' NO SQL LANGUAGE JAVA PARAMETER STYLE JAVA");
1582
1583
1584        scp.execute("CREATE PROCEDURE OP_INOUT " +
1585            "(INOUT I1 INT, INOUT I2 INT, INOUT I3 INT, INOUT I4 INT, INOUT I5 INT, " +
1586            "INOUT V1 VARCHAR(40), INOUT V2 VARCHAR(40), INOUT V3 VARCHAR(40), INOUT V4 VARCHAR(40), INOUT V5 VARCHAR(40)) " +
1587
1588            "EXTERNAL NAME '" + CLASS_NAME + "output' NO SQL LANGUAGE JAVA PARAMETER STYLE JAVA");
1589
1590
1591        CallableStatement csOut_cs = conn.prepareCall("CALL OP_OUT(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
1592        CallableStatement csInOut_cs = conn.prepareCall("CALL OP_INOUT(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
1593
1594        System.out.println("Ten OUT parameters");
1595
1596        executeOutput(csOut_cs);
1597        executeOutput(csOut_cs);
1598
1599        csOut_cs.close();
1600
1601        System.out.println("Ten INOUT parameters");
1602
1603
1604        setupInput(csInOut_cs);
1605        executeOutput(csInOut_cs);
1606
1607        setupInput(csInOut_cs);
1608        executeOutput(csInOut_cs);
1609
1610        csInOut_cs.close();
1611
1612        scp.execute("DROP PROCEDURE OP_OUT");
1613        scp.execute("DROP PROCEDURE OP_INOUT");
1614        scp.close();
1615
1616
1617        System.out.println("end testManyOut");
1618
1619
1620    }
1621
1622
1623    private static void setupInput(PreparedStatement ps) throws SQLException {
1624
1625        ps.setInt(1, 0);
1626        ps.setInt(2, 0);
1627        ps.setInt(3, 99);
1628        ps.setInt(4, 103);
1629        ps.setInt(5, 1456);
1630
1631        ps.setNull(6, Types.CHAR);
1632        ps.setString(7, null);
1633        ps.setString(8, "hello");
1634        ps.setString(9, "goodbye");
1635        ps.setString(10, "welcome");
1636    }
1637    private static void executeOutput(CallableStatement cs) throws SQLException {
1638
1639        for (int p = 1; p <= 5; p++)
1640            cs.registerOutParameter(p, Types.INTEGER);
1641
1642        for (int p = 6; p <= 10; p++)
1643            cs.registerOutParameter(p, Types.VARCHAR);
1644
1645        cs.execute();
1646
1647        for (int p = 1; p <= 5; p++) {
1648            System.out.println(" " + p + " = " + cs.getInt(p) + " was null " + cs.wasNull());
1649    
1650        }
1651        for (int p = 6; p <= 10; p++) {
1652            System.out.println(" " + p + " = " + cs.getString(p) + " was null " + cs.wasNull());
1653        }
1654    }
1655
1656
1657    public static void output(int[] a1, int[] a2, int[] a3, int[] a4, int[] a5,
1658        String JavaDoc[] s1, String JavaDoc[] s2, String JavaDoc[] s3, String JavaDoc[] s4, String JavaDoc[] s5) {
1659
1660        System.out.println(" a1 = " + a1[0]);
1661        System.out.println(" a2 = " + a2[0]);
1662        System.out.println(" a3 = " + a3[0]);
1663        System.out.println(" a4 = " + a4[0]);
1664        System.out.println(" a5 = " + a5[0]);
1665
1666        System.out.println(" s1 = " + s1[0]);
1667        System.out.println(" s2 = " + s2[0]);
1668        System.out.println(" s3 = " + s3[0]);
1669        System.out.println(" s4 = " + s4[0]);
1670        System.out.println(" s5 = " + s5[0]);
1671
1672        a1[0] = 0;
1673        a2[0] = 0;
1674        a3[0] = 77;
1675        a4[0] = 4;
1676        a5[0] = 2003;
1677
1678        s1[0] = null;
1679        s2[0] = null;
1680        s3[0] = "cloudscape";
1681        s4[0] = "jbms";
1682        s5[0] = "IBM CS";
1683    }
1684
1685    private static void test5116(Connection conn) throws Throwable JavaDoc
1686    {
1687        System.out.println("==============================================");
1688        System.out.println("TESTING FIX OF 5116 -- VAR BIT VARYING INPUT");
1689        System.out.println("==============================================\n");
1690
1691        Statement stmt = conn.createStatement();
1692        stmt.executeUpdate("CREATE TABLE ACTIVITY_INSTANCE_T (" +
1693    "AIID char(16) for bit data NOT NULL ," +
1694    "KIND INTEGER NOT NULL ," +
1695    "PIID char(16) for bit data NOT NULL ," +
1696    "PTID char(16) for bit data NOT NULL ," +
1697    "ATID char(16) for bit data NOT NULL ," +
1698    "RUN_MODE INTEGER NOT NULL ," +
1699    "FINISHED TIMESTAMP ," +
1700    "ACTIVATED TIMESTAMP ," +
1701    "STARTED TIMESTAMP ," +
1702    "LAST_MODIFIED TIMESTAMP ," +
1703    "LAST_STATE_CHANGE TIMESTAMP ," +
1704    "STATE INTEGER NOT NULL ," +
1705    "TRANS_COND_VALUES VARCHAR(66) FOR BIT DATA NOT NULL ," +
1706    "NUM_CONN_ACT_EVA INTEGER NOT NULL ," +
1707    "NUMBER_OF_ITERATIONS INTEGER NOT NULL ," +
1708    "NUMBER_OF_RETRIES INTEGER NOT NULL ," +
1709    "HAS_CUSTOM_ATTRIBUTES SMALLINT NOT NULL ," +
1710    "NON_BLOCK_PTID char(16) for bit data NOT NULL ," +
1711    "NON_BLOCK_PIID char(16) for bit data NOT NULL ," +
1712    "EXPIRES TIMESTAMP ," +
1713    "TASK_ID VARCHAR(254) ," +
1714    "UNHANDLED_EXCEPTION BLOB(3993600) ," +
1715    "SUB_PROCESS_PIID char(16) for bit data ," +
1716    "OWNER VARCHAR(32) ," +
1717    "USER_INPUT VARCHAR(130) FOR BIT DATA ," +
1718    "DESCRIPTION VARCHAR(254) ," +
1719    "VERSION_ID SMALLINT NOT NULL ," +
1720    "PRIMARY KEY ( AIID ) )");
1721
1722        stmt.execute("CREATE PROCEDURE doInsertion(IN P1 VARCHAR(2) FOR BIT DATA) " +
1723                        "EXTERNAL NAME '" + CLASS_NAME + "doInsertion'" +
1724                        " MODIFIES SQL DATA LANGUAGE JAVA PARAMETER STYLE JAVA");
1725
1726        CallableStatement cs = conn.prepareCall("call doInsertion (?)");
1727        cs.setNull(1, java.sql.Types.VARBINARY);
1728        cs.execute();
1729        byte [] b = new byte[2];
1730        b[0]=1; b[1] = 2;
1731        cs.setBytes( 1, b );
1732        cs.execute();
1733        cs.close();
1734        stmt.executeUpdate("DROP PROCEDURE doInsertion");
1735        stmt.close();
1736    }
1737
1738    public static void doInsertion (byte[] p25) throws Throwable JavaDoc
1739    {
1740        Connection connNested = DriverManager.getConnection("jdbc:default:connection");
1741        Statement stmt = connNested.createStatement();
1742        stmt.executeUpdate("delete from ACTIVITY_INSTANCE_T");
1743
1744        String JavaDoc strStmt = "INSERT INTO ACTIVITY_INSTANCE_T VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? )";
1745        PreparedStatement pstmt = connNested.prepareStatement( strStmt );
1746
1747        byte [] b = new byte[2];
1748        b[0]=1;
1749        byte[] b2 = new byte[1];
1750        b2[0] = 0;
1751
1752         pstmt.setBytes( 1, b ); //ids
1753
pstmt.setInt( 2, 0);
1754         pstmt.setBytes( 3, b );
1755         pstmt.setBytes( 4, b );
1756         pstmt.setBytes( 5, b );
1757         pstmt.setInt( 6, 0);
1758         pstmt.setNull( 7, java.sql.Types.TIMESTAMP);
1759         pstmt.setNull( 8, java.sql.Types.TIMESTAMP);
1760         pstmt.setNull( 9, java.sql.Types.TIMESTAMP);
1761         pstmt.setNull( 10, java.sql.Types.TIMESTAMP);
1762         pstmt.setNull( 11, java.sql.Types.TIMESTAMP);
1763         pstmt.setInt( 12, 0);
1764         pstmt.setBytes( 13, b );
1765
1766         pstmt.setInt( 14, 0);
1767         pstmt.setInt( 15, 0);
1768         pstmt.setInt( 16, 0);
1769         pstmt.setBoolean( 17, false);
1770         pstmt.setBytes( 18, b );
1771         pstmt.setBytes( 19, b );
1772         pstmt.setNull( 20, java.sql.Types.TIMESTAMP);
1773         pstmt.setNull( 21, java.sql.Types.VARCHAR);
1774         pstmt.setNull( 22, java.sql.Types.BLOB );
1775         pstmt.setNull( 23, java.sql.Types.VARBINARY );
1776         pstmt.setNull( 24, java.sql.Types.VARCHAR);
1777        if (p25 == null)
1778             pstmt.setNull( 25, java.sql.Types.VARBINARY);
1779        else
1780             pstmt.setBytes(25, p25);
1781         pstmt.setNull( 26, java.sql.Types.VARCHAR);
1782         pstmt.setShort( 27, (short) 0);
1783         pstmt.executeUpdate();
1784         pstmt.close();
1785
1786         pstmt = connNested.prepareStatement( "SELECT version_id, user_input FROM activity_instance_t");
1787         ResultSet resultSet = pstmt.executeQuery();
1788         System.out.println("Executed query");
1789         while( resultSet.next() )
1790         {
1791            System.out.println("i= " + resultSet.getInt(1) );
1792            byte [] userInput = resultSet.getBytes(2);
1793            if( userInput == null || resultSet.wasNull() )
1794            {
1795                if( userInput == null)
1796                    System.out.println("UserInput = null");
1797                if (resultSet.wasNull())
1798                    System.out.println("resultSet wasNull");
1799            }
1800            else
1801            {
1802               System.out.println("UserInput length = " + userInput.length + " bytes");
1803               for( int i=0; i<userInput.length; i++ )
1804               {
1805                  System.out.println( i + ") = " + userInput[i] );
1806               }
1807            }
1808         }
1809         System.out.println("Close result set.");
1810         resultSet.close();
1811         pstmt.close();
1812         stmt.close();
1813         connNested.close();
1814   }
1815}
1816
1817
Popular Tags