KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.lang.bug4356
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
24 import java.sql.Connection JavaDoc;
25 import java.sql.DriverManager JavaDoc;
26 import java.sql.DatabaseMetaData JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.Statement JavaDoc;
30 import java.sql.SQLException JavaDoc;
31
32 import org.apache.derby.tools.ij;
33 import org.apache.derby.tools.JDBCDisplayUtil;
34
35 /**
36  * Demonstrate subselect behavior with prepared statement.
37  */

38 public class bug4356 {
39  
40   public static void main (String JavaDoc args[])
41   {
42     try {
43         /* Load the JDBC Driver class */
44         // use the ij utility to read the property file and
45
// make the initial connection.
46
ij.getPropertyArg(args);
47         Connection JavaDoc conn = ij.startJBMS();
48
49         createTables(conn);
50         doUpdates(conn);
51         dumpResult(conn);
52         conn.close();
53     } catch (Exception JavaDoc e) {
54         System.out.println("FAIL -- unexpected exception "+e);
55         JDBCDisplayUtil.ShowException(System.out, e);
56         e.printStackTrace();
57     }
58   }
59
60   private static void createTables(Connection JavaDoc conn) throws SQLException JavaDoc {
61     Statement JavaDoc stmt = conn.createStatement();
62     try {
63         stmt.executeUpdate( "DROP TABLE T1" );
64         stmt.executeUpdate( "DROP TABLE T2" );
65     }catch (Exception JavaDoc e) {}
66
67     System.out.print("Creating tables...");
68     stmt.executeUpdate( "CREATE TABLE T1 (a integer, b integer)" );
69     stmt.executeUpdate( "CREATE TABLE T2 (a integer)" );
70     stmt.executeUpdate("INSERT INTO T2 VALUES(1)");
71     System.out.println("done.");
72
73     stmt.close();
74   }
75
76   private static void doUpdates(Connection JavaDoc conn) throws SQLException JavaDoc
77   {
78     int rc;
79     // bug only happens when autocommit is off
80
conn.setAutoCommit(false);
81     PreparedStatement JavaDoc stmt = conn.prepareStatement(
82       "INSERT INTO T1 VALUES (?,(select count(*) from t2 where a = ?)) "
83     );
84     stmt.setInt(1, 1);
85     stmt.setInt(2, 1);
86
87     rc = stmt.executeUpdate();
88
89     stmt.setInt(1, 2);
90     stmt.setInt(2, 2);
91     rc = stmt.executeUpdate();
92
93     conn.commit();
94     stmt.close();
95   }
96
97   private static void dumpResult(Connection JavaDoc conn) throws SQLException JavaDoc
98   {
99     Statement JavaDoc stmt = conn.createStatement();
100     ResultSet JavaDoc rs = stmt.executeQuery(
101       "SELECT * FROM T1"
102     );
103     System.out.println("T1 contents:");
104     System.out.println("Second row should have a b value of 0");
105     while (rs.next()) {
106       System.out.println(rs.getInt(1)+ " " + rs.getInt(2));
107     }
108     rs.close();
109     conn.commit();
110     stmt.close();
111   }
112
113 }
114
Popular Tags