KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > test > TestPreparedSubQueries


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.test;
33
34 import java.sql.Connection JavaDoc;
35 import java.sql.PreparedStatement JavaDoc;
36 import java.sql.Statement JavaDoc;
37
38 /**
39  * @author kloska@users
40  */

41 class TestPreparedSubQueries {
42
43     private Connection JavaDoc con = null;
44
45     private class sqlStmt {
46
47         boolean prepare;
48         boolean update;
49         String JavaDoc command;
50
51         sqlStmt(String JavaDoc 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 JavaDoc[][] stmtArgs = {
81         {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, { new Integer JavaDoc(2) },
82         { new Integer JavaDoc(2) }
83     };
84
85     public static void main(String JavaDoc[] argv) {
86
87         Connection JavaDoc con = null;
88
89         try {
90             String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc e) {
168             System.out.println(" ?? Caught Exception " + e);
169
170             return false;
171         }
172
173         return true;
174     }
175 }
176
Popular Tags