1 30 31 32 package org.hsqldb.test; 33 34 import java.sql.Connection ; 35 import java.sql.PreparedStatement ; 36 import java.sql.Statement ; 37 38 41 class TestPreparedSubQueries { 42 43 private Connection con = null; 44 45 private class sqlStmt { 46 47 boolean prepare; 48 boolean update; 49 String command; 50 51 sqlStmt(String c, boolean p, boolean u) { 52 53 prepare = p; 54 command = c; 55 update = u; 56 } 57 } 58 ; 59 60 private sqlStmt[] stmtArray = { 61 new sqlStmt("drop table a if exists", false, false), 62 new sqlStmt("create cached table a (a int identity,b int)", false, 63 false), 64 new sqlStmt("create index bIdx on a(b)", false, false), 65 new sqlStmt("insert into a(b) values(1)", true, true), 66 new sqlStmt("insert into a(b) values(2)", true, true), 67 new sqlStmt("insert into a(b) values(3)", true, true), 68 new sqlStmt("insert into a(b) values(4)", true, true), 69 new sqlStmt("insert into a(b) values(5)", true, true), 70 new sqlStmt("insert into a(b) values(6)", true, true), 71 new sqlStmt( 72 "update a set b=100 where b>(select b from a X where X.a=2)", 73 true, true), 74 new sqlStmt("update a set b=200 where b>(select b from a where a=?)", 75 true, true), 76 new sqlStmt( 77 "update a set b=300 where b>(select b from a X where X.a=?)", 78 true, true) 79 }; 80 private Object [][] stmtArgs = { 81 {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, { new Integer (2) }, 82 { new Integer (2) } 83 }; 84 85 public static void main(String [] argv) { 86 87 Connection con = null; 88 89 try { 90 String url = "jdbc:hsqldb:test"; 91 92 Class.forName("org.hsqldb.jdbcDriver"); 93 94 con = java.sql.DriverManager.getConnection(url, "sa", ""); 95 96 System.out.println("SciSelect::connect -- connected to '" + url 97 + "'"); 98 } catch (Exception e) { 99 System.out.println(" ?? main: Caught Exception " + e); 100 System.out.println(" - FAILED - "); 101 102 return; 103 } 104 105 TestPreparedSubQueries t = new TestPreparedSubQueries(con); 106 boolean b = t.test(); 107 108 System.out.println(b ? " -- OK -- " 109 : " ?? FAILED ?? "); 110 System.exit(0); 111 } 112 113 public TestPreparedSubQueries(Connection c) { 114 con = c; 115 } 116 117 public boolean test() { 118 119 try { 120 int i = 0; 121 122 for (i = 0; i < stmtArray.length; i++) { 123 int j; 124 125 System.out.println(" -- #" + i + " ----------------------- "); 126 127 if (stmtArray[i].prepare) { 128 PreparedStatement ps = null; 129 130 System.out.println(" -- preparing\n<<<\n" 131 + stmtArray[i].command + "\n>>>\n"); 132 133 ps = con.prepareStatement(stmtArray[i].command); 134 135 System.out.print(" -- setting " + stmtArgs[i].length 136 + " Args ["); 137 138 for (j = 0; j < stmtArgs[i].length; j++) { 139 System.out.print((j > 0 ? "; " 140 : "") + stmtArgs[i][j]); 141 ps.setObject(j + 1, stmtArgs[i][j]); 142 } 143 144 System.out.println("]"); 145 System.out.println(" -- executing "); 146 147 if (stmtArray[i].update) { 148 int r = ps.executeUpdate(); 149 150 System.out.println(" ***** ps.executeUpdate gave me " 151 + r); 152 } else { 153 boolean b = ps.execute(); 154 155 System.out.print(" ***** ps.execute gave me " + b); 156 } 157 } else { 158 System.out.println(" -- executing directly\n<<<\n" 159 + stmtArray[i].command + "\n>>>\n"); 160 161 Statement s = con.createStatement(); 162 boolean b = s.execute(stmtArray[i].command); 163 164 System.out.println(" ***** st.execute gave me " + b); 165 } 166 } 167 } catch (Exception e) { 168 System.out.println(" ?? Caught Exception " + e); 169 170 return false; 171 } 172 173 return true; 174 } 175 } 176 | Popular Tags |