001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.prepStmtNull
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.jdbcapi;
023:
024: import java.sql.*;
025:
026: import org.apache.derby.tools.ij;
027: import org.apache.derby.tools.JDBCDisplayUtil;
028:
029: import org.apache.derbyTesting.functionTests.util.TestUtil;
030:
031: public class prepStmtNull {
032:
033: public static void main(String[] args) {
034: test1(args);
035: test2(args);
036: test3(args);
037: }
038:
039: public static void test1(String[] args) {
040: Connection con;
041: ResultSet rs;
042: PreparedStatement stmt = null;
043: PreparedStatement pStmt = null;
044: Statement stmt1 = null;
045:
046: System.out.println("Test prepStmtNull starting");
047:
048: try {
049: // use the ij utility to read the property file and
050: // make the initial connection.
051: ij.getPropertyArg(args);
052: con = ij.startJBMS();
053:
054: con.setAutoCommit(false);
055:
056: stmt = con
057: .prepareStatement("create table nullTS(name varchar(10), ts timestamp)");
058: stmt.executeUpdate();
059: con.commit();
060:
061: pStmt = con
062: .prepareStatement("insert into nullTS values (?,?)");
063:
064: pStmt.setString(1, "work");
065: pStmt.setNull(2, java.sql.Types.TIMESTAMP);
066: pStmt.addBatch();
067: pStmt.setString(1, "work1");
068: pStmt.setNull(2, java.sql.Types.TIMESTAMP, "");
069: pStmt.addBatch();
070:
071: pStmt.executeBatch();
072: con.commit();
073:
074: stmt1 = con.createStatement();
075: rs = stmt1.executeQuery("select * from nullTS");
076: while (rs.next()) {
077: System.out.println("ResultSet is: " + rs.getObject(1));
078: System.out.println("ResultSet is: " + rs.getObject(2));
079: }
080: String[] testObjects = { "table nullTS" };
081: TestUtil.cleanUpTest(stmt1, testObjects);
082: con.commit();
083: } catch (SQLException sqle) {
084: dumpSQLExceptions(sqle);
085: sqle.printStackTrace();
086: } catch (Throwable e) {
087: System.out.println("FAIL -- unexpected exception: " + e);
088: e.printStackTrace();
089:
090: }
091: }
092:
093: public static void test2(String[] args) {
094: Connection con;
095: ResultSet rs;
096: PreparedStatement stmt = null;
097: PreparedStatement pStmt = null;
098: Statement stmt1 = null;
099:
100: System.out.println("Test prepStmtNull starting");
101:
102: try {
103: // use the ij utility to read the property file and
104: // make the initial connection.
105: ij.getPropertyArg(args);
106: con = ij.startJBMS();
107:
108: con.setAutoCommit(false);
109:
110: stmt = con
111: .prepareStatement("create table nullBlob(name varchar(10), bval blob(16K))");
112: stmt.executeUpdate();
113: con.commit();
114:
115: pStmt = con
116: .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: pStmt.executeBatch();
126: con.commit();
127:
128: stmt1 = con.createStatement();
129: rs = stmt1.executeQuery("select * from nullBlob");
130: while (rs.next()) {
131: System.out.println("ResultSet is: " + rs.getObject(1));
132: System.out.println("ResultSet is: " + rs.getObject(2));
133: }
134: String[] testObjects = { "table nullBlob" };
135: TestUtil.cleanUpTest(stmt1, testObjects);
136: con.commit();
137: } catch (SQLException sqle) {
138: dumpSQLExceptions(sqle);
139: sqle.printStackTrace();
140: } catch (Throwable e) {
141: System.out.println("FAIL -- unexpected exception: " + e);
142: e.printStackTrace();
143:
144: }
145: }
146:
147: /* Test setNull() on Clob/Blob using Varchar/binary types */
148: public static void test3(String[] args) {
149: Connection con;
150: ResultSet rs;
151: PreparedStatement stmt = null;
152: PreparedStatement pStmt = null;
153: Statement stmt1 = null;
154: byte[] b2 = new byte[1];
155: b2[0] = (byte) 64;
156:
157: System.out.println("Test3 prepStmtNull starting");
158:
159: try {
160: // use the ij utility to read the property file and
161: // make the initial connection.
162: ij.getPropertyArg(args);
163: con = ij.startJBMS();
164:
165: stmt = con
166: .prepareStatement("create table ClobBlob(cval clob, bval blob(16K))");
167: stmt.executeUpdate();
168:
169: pStmt = con
170: .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[] testObjects = { "table ClobBlob" };
185: TestUtil.cleanUpTest(stmt1, testObjects);
186: } catch (SQLException sqle) {
187: dumpSQLExceptions(sqle);
188: } catch (Throwable 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() + "): "
197: + se);
198: se = se.getNextException();
199: }
200: }
201: }
|