KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.prepStmtNull
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
25 import java.sql.*;
26
27 import org.apache.derby.tools.ij;
28 import org.apache.derby.tools.JDBCDisplayUtil;
29
30 import org.apache.derbyTesting.functionTests.util.TestUtil;
31
32 public class prepStmtNull {
33
34     public static void main(String JavaDoc[] args) {
35         test1(args);
36         test2(args);
37         test3(args);
38     }
39     
40         public static void test1(String JavaDoc []args) {
41                 Connection con;
42                 ResultSet rs;
43                 PreparedStatement stmt = null;
44                 PreparedStatement pStmt = null;
45                 Statement stmt1 = null;
46
47                 System.out.println("Test prepStmtNull starting");
48
49                 try
50                 {
51                         // use the ij utility to read the property file and
52
// make the initial connection.
53
ij.getPropertyArg(args);
54                         con = ij.startJBMS();
55                     
56             con.setAutoCommit(false);
57                         
58                         stmt = con.prepareStatement("create table nullTS(name varchar(10), ts timestamp)");
59             stmt.executeUpdate();
60             con.commit();
61             
62             pStmt = con.prepareStatement("insert into nullTS values (?,?)");
63                         
64             pStmt.setString(1,"work");
65             pStmt.setNull(2,java.sql.Types.TIMESTAMP);
66             pStmt.addBatch();
67             pStmt.setString(1,"work1");
68             pStmt.setNull(2,java.sql.Types.TIMESTAMP,"");
69             pStmt.addBatch();
70
71         
72             pStmt.executeBatch();
73             con.commit();
74
75             stmt1 = con.createStatement();
76                 rs = stmt1.executeQuery("select * from nullTS");
77             while(rs.next()) {
78                System.out.println("ResultSet is: "+rs.getObject(1));
79                System.out.println("ResultSet is: "+rs.getObject(2));
80             }
81             String JavaDoc[] testObjects = {"table nullTS"};
82                         TestUtil.cleanUpTest(stmt1, testObjects);
83             con.commit();
84         } catch(SQLException sqle) {
85            dumpSQLExceptions(sqle);
86            sqle.printStackTrace();
87         } catch(Throwable JavaDoc e) {
88            System.out.println("FAIL -- unexpected exception: "+e);
89                    e.printStackTrace();
90
91         }
92      }
93      
94      public static void test2(String JavaDoc []args) {
95                 Connection con;
96                 ResultSet rs;
97                 PreparedStatement stmt = null;
98                 PreparedStatement pStmt = null;
99                 Statement stmt1 = null;
100
101                 System.out.println("Test prepStmtNull starting");
102
103                 try
104                 {
105                         // use the ij utility to read the property file and
106
// make the initial connection.
107
ij.getPropertyArg(args);
108                         con = ij.startJBMS();
109                     
110             con.setAutoCommit(false);
111                         
112                         stmt = con.prepareStatement("create table nullBlob(name varchar(10), bval blob(16K))");
113             stmt.executeUpdate();
114             con.commit();
115             
116             pStmt = con.prepareStatement("insert into nullBlob values (?,?)");
117                         
118             pStmt.setString(1,"blob");
119             pStmt.setNull(2,java.sql.Types.BLOB);
120             pStmt.addBatch();
121             pStmt.setString(1,"blob1");
122             pStmt.setNull(2,java.sql.Types.BLOB,"");
123             pStmt.addBatch();
124
125         
126             pStmt.executeBatch();
127             con.commit();
128
129             stmt1 = con.createStatement();
130                 rs = stmt1.executeQuery("select * from nullBlob");
131             while(rs.next()) {
132                System.out.println("ResultSet is: "+rs.getObject(1));
133                System.out.println("ResultSet is: "+rs.getObject(2));
134             }
135             String JavaDoc[] testObjects = {"table nullBlob"};
136                         TestUtil.cleanUpTest(stmt1, testObjects);
137             con.commit();
138         } catch(SQLException sqle) {
139            dumpSQLExceptions(sqle);
140            sqle.printStackTrace();
141         } catch(Throwable JavaDoc e) {
142            System.out.println("FAIL -- unexpected exception: "+e);
143                    e.printStackTrace();
144
145         }
146      }
147      
148      /* Test setNull() on Clob/Blob using Varchar/binary types */
149      public static void test3(String JavaDoc []args) {
150           Connection con;
151           ResultSet rs;
152           PreparedStatement stmt = null;
153           PreparedStatement pStmt = null;
154           Statement stmt1 = null;
155           byte[] b2 = new byte[1];
156           b2[0] = (byte)64;
157
158           System.out.println("Test3 prepStmtNull starting");
159
160           try
161           {
162                // use the ij utility to read the property file and
163
// make the initial connection.
164
ij.getPropertyArg(args);
165                con = ij.startJBMS();
166                     
167                stmt = con.prepareStatement("create table ClobBlob(cval clob, bval blob(16K))");
168                stmt.executeUpdate();
169             
170                pStmt = con.prepareStatement("insert into ClobBlob values (?,?)");
171                         
172                pStmt.setNull(1, Types.VARCHAR);
173                pStmt.setBytes(2, b2);
174                pStmt.execute();
175                pStmt.setNull(1, Types.VARCHAR,"");
176                pStmt.setBytes(2, b2);
177                pStmt.execute();
178
179                stmt1 = con.createStatement();
180                rs = stmt1.executeQuery("select * from ClobBlob");
181                while(rs.next()) {
182                     System.out.println("ResultSet is: "+rs.getObject(1));
183                }
184                String JavaDoc[] testObjects = {"table ClobBlob"};
185                TestUtil.cleanUpTest(stmt1, testObjects);
186           } catch(SQLException sqle) {
187                dumpSQLExceptions(sqle);
188           } catch(Throwable JavaDoc e) {
189                System.out.println("FAIL -- unexpected exception: ");
190           }
191      }
192      
193      static private void dumpSQLExceptions (SQLException se) {
194                 System.out.println("FAIL -- unexpected exception");
195                 while (se != null) {
196                         System.out.println("SQLSTATE("+se.getSQLState()+"): "+se);
197                         se = se.getNextException();
198                 }
199         }
200 }
201
Popular Tags