1 21 22 package org.apache.derbyTesting.functionTests.tests.lang; 23 24 import java.sql.Connection ; 25 import java.sql.DriverManager ; 26 import java.sql.DatabaseMetaData ; 27 import java.sql.ResultSet ; 28 import java.sql.PreparedStatement ; 29 import java.sql.Statement ; 30 import java.sql.SQLException ; 31 32 import org.apache.derby.tools.ij; 33 import org.apache.derby.tools.JDBCDisplayUtil; 34 35 38 public class bug4356 { 39 40 public static void main (String args[]) 41 { 42 try { 43 44 ij.getPropertyArg(args); 47 Connection conn = ij.startJBMS(); 48 49 createTables(conn); 50 doUpdates(conn); 51 dumpResult(conn); 52 conn.close(); 53 } catch (Exception 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 conn) throws SQLException { 61 Statement stmt = conn.createStatement(); 62 try { 63 stmt.executeUpdate( "DROP TABLE T1" ); 64 stmt.executeUpdate( "DROP TABLE T2" ); 65 }catch (Exception 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 conn) throws SQLException 77 { 78 int rc; 79 conn.setAutoCommit(false); 81 PreparedStatement 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 conn) throws SQLException 98 { 99 Statement stmt = conn.createStatement(); 100 ResultSet 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 |