1 30 31 32 37 package org.hsqldb.test; 38 39 import java.sql.Connection ; 40 import java.sql.Driver ; 41 import java.sql.DriverManager ; 42 import java.sql.PreparedStatement ; 43 import java.sql.ResultSet ; 44 import java.sql.Statement ; 45 46 50 public class TestSubQueriesInPreparedStatements { 51 52 public static void main(String [] args) throws Exception { 53 test(); 54 } 55 56 public static void test() throws Exception { 57 58 Connection conn; 59 Statement stmnt; 60 PreparedStatement pstmnt; 61 Driver driver; 62 63 driver = 64 (Driver ) Class.forName("org.hsqldb.jdbcDriver").newInstance(); 65 66 DriverManager.registerDriver(driver); 67 68 conn = DriverManager.getConnection("jdbc:hsqldb:mem:test", "sa", ""); 69 stmnt = conn.createStatement(); 70 pstmnt = conn.prepareStatement("drop table t if exists"); 71 72 boolean result = pstmnt.execute(); 73 74 pstmnt = conn.prepareStatement("create table t(i decimal)"); 75 76 int updatecount = pstmnt.executeUpdate(); 77 78 pstmnt = conn.prepareStatement("insert into t values(?)"); 79 80 for (int i = 0; i < 100; i++) { 81 pstmnt.setInt(1, i); 82 pstmnt.executeUpdate(); 83 } 84 85 pstmnt = conn.prepareStatement( 86 "select * from (select * from t where i < ?)"); 87 88 System.out.println("Expecting: 0..3"); 89 pstmnt.setInt(1, 4); 90 91 ResultSet rs = pstmnt.executeQuery(); 92 93 while (rs.next()) { 94 System.out.println(rs.getInt(1)); 95 } 96 97 System.out.println("Expecting: 0..4"); 98 pstmnt.setInt(1, 5); 99 100 rs = pstmnt.executeQuery(); 101 102 while (rs.next()) { 103 System.out.println(rs.getInt(1)); 104 } 105 106 pstmnt = conn.prepareStatement( 107 "select sum(i) from (select i from t where i between ? and ?)"); 108 109 System.out.println("Expecting: 9"); 110 pstmnt.setInt(1, 4); 111 pstmnt.setInt(2, 5); 112 113 rs = pstmnt.executeQuery(); 114 115 while (rs.next()) { 116 System.out.println(rs.getInt(1)); 117 } 118 119 System.out.println("Expecting: 15"); 120 pstmnt.setInt(2, 6); 121 122 rs = pstmnt.executeQuery(); 123 124 while (rs.next()) { 125 System.out.println(rs.getInt(1)); 126 } 127 128 pstmnt = conn.prepareStatement( 129 "select * from (select i as c1 from t where i < ?) a, (select i as c2 from t where i < ?) b"); 130 131 System.out.println("Expecting: (0,0)"); 132 pstmnt.setInt(1, 1); 133 pstmnt.setInt(2, 1); 134 135 rs = pstmnt.executeQuery(); 136 137 while (rs.next()) { 138 System.out.println("(" + rs.getInt(1) + "," + rs.getInt(2) + ")"); 139 } 140 141 System.out.println("Expecting: ((0,0), (0,1), (1,0), (1,1)"); 142 pstmnt.setInt(1, 2); 143 pstmnt.setInt(2, 2); 144 145 rs = pstmnt.executeQuery(); 146 147 while (rs.next()) { 148 System.out.println("(" + rs.getInt(1) + "," + rs.getInt(2) + ")"); 149 } 150 151 System.out.println("Expecting: ((0,0) .. (3,3)"); 152 pstmnt.setInt(1, 4); 153 pstmnt.setInt(2, 4); 154 155 rs = pstmnt.executeQuery(); 156 157 while (rs.next()) { 158 System.out.println("(" + rs.getInt(1) + "," + rs.getInt(2) + ")"); 159 } 160 } 161 } 162 | Popular Tags |