KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbcapi > prepStmtMetaData


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.prepStmtMetaData
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.jdbcapi;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.DriverManager JavaDoc;
26 import java.sql.DatabaseMetaData JavaDoc;
27 import java.sql.ResultSetMetaData JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.sql.PreparedStatement JavaDoc;
30 import java.sql.ResultSet JavaDoc;
31 import java.sql.SQLException JavaDoc;
32 import java.sql.Types JavaDoc;
33
34 import org.apache.derby.tools.ij;
35
36 /**
37  * Test of prepared statement getMetaData for statements that don't produce a
38  * result set
39  *
40  * @author peachey
41  */

42
43 public class prepStmtMetaData {
44     
45     public static void main(String JavaDoc[] args) {
46         ResultSetMetaData JavaDoc met;
47         Connection JavaDoc con;
48         PreparedStatement JavaDoc ps;
49         System.out.println("Test prepStmtMetaData starting");
50     
51         try
52         {
53             // use the ij utility to read the property file and
54
// make the initial connection.
55
ij.getPropertyArg(args);
56             con = ij.startJBMS();
57             con.setAutoCommit(true); // make sure it is true
58

59             // test create table - meta data should be null
60
testMetaData(con, "create table ab(a int)", true);
61
62             // test alter table - meta data should be null
63
testMetaData(con, "alter table ab add column b int", true);
64
65             // for test call statement
66
Statement JavaDoc s = con.createStatement();
67             s.execute("create procedure testproc() language java external name " +
68                 "'org.apache.derbyTesting.functionTests.tests.jdbcapi.prepStmtMetaData.tstmeth'"+
69                 " parameter style java");
70
71             // test call statement - shouldn't have meta data
72
testMetaData(con, "call testproc()", false);
73             
74             // test drop procedure - meta data should be null
75
testMetaData(con, "drop procedure testproc", true);
76
77             // test create schema - meta data should be null
78
testMetaData(con, "create schema myschema", true);
79
80             // test drop schema - meta data should be null
81
testMetaData(con, "drop schema myschema restrict", true);
82
83             // test create trigger - meta data should be null
84
//testMetaData(con, "create trigger mytrig after insert on ab for each row mode db2sql create table yy(a int)", true);
85

86             // test drop trigger - meta data should be null
87
//testMetaData(con, "drop trigger mytrig", true);
88

89             // test create view - meta data should be null
90
testMetaData(con, "create view myview as select * from ab", true);
91
92             // test drop view - meta data should be null
93
testMetaData(con, "drop view myview", true);
94
95             // test drop table - meta data should be null
96
testMetaData(con, "drop table ab", false);
97
98             // test create index - meta data should be null
99
testMetaData(con, "create index aindex on ab(a)", true);
100
101             // test drop index - meta data should be null
102
testMetaData(con, "drop index aindex", false);
103
104             // test insert - meta data should be null
105
testMetaData(con, "insert into ab values(1,1)", true);
106
107             // test update - meta data should be null
108
testMetaData(con, "update ab set a = 2", false);
109
110             // test delete - meta data should be null
111
testMetaData(con, "delete from ab", false);
112
113             // test select - meta data should be provided
114
ps = con.prepareStatement("select * from ab");
115             met = ps.getMetaData();
116             System.out.println("Result meta data for select");
117             dumpRSMetaData(met);
118             ps.close();
119
120             // test set
121
testMetaData(con, "set schema rock", false);
122
123             // bug 4579 : PreparedStatement.getMetaData should not return stale data
124
executeStmt(con,"create table bug4579 (c11 int)");
125             executeStmt(con,"insert into bug4579 values 1");
126             testMetaData(con, "set schema rick", false);
127             ps = con.prepareStatement("select * from bug4579");
128             met = ps.getMetaData();
129             System.out.println("bug 4579 and 5338 : Result meta data for select *");
130             dumpRSMetaData(met);
131             executeStmt(con,"alter table bug4579 add column c12 int");
132             met = ps.getMetaData();
133             System.out.println("bug 4579 and 5338 : Result meta data for select * after alter table but w/o execute query");
134             dumpRSMetaData(met);
135             executeStmt(con,"alter table bug4579 add column c13 int");
136             ps.execute();
137             met = ps.getMetaData();
138             System.out.println("bug 4579 and 5338 : Result meta data for select * after alter table and execute query");
139             dumpRSMetaData(met);
140             ps.close();
141
142             // clean up
143
executeStmt(con,"drop table ab");
144             executeStmt(con,"drop table bug4579");
145
146             con.close();
147         }
148         catch (SQLException JavaDoc e) {
149             dumpSQLExceptions(e);
150             e.printStackTrace(System.out);
151         }
152         catch (Throwable JavaDoc e) {
153             System.out.println("FAIL -- unexpected exception:");
154             e.printStackTrace(System.out);
155         }
156
157         System.out.println("Test prepStmtMetaData finished");
158     }
159     static private void testMetaData(Connection JavaDoc con, String JavaDoc statement, boolean execute) {
160         try {
161             PreparedStatement JavaDoc ps = con.prepareStatement(statement);
162             ResultSetMetaData JavaDoc met = ps.getMetaData();
163             dumpRSMetaData(met);
164             if (execute)
165                 ps.execute();
166             ps.close();
167
168         }
169         catch (SQLException JavaDoc e) {
170             dumpSQLExceptions(e);
171         }
172         catch (Throwable JavaDoc e) {
173             System.out.println("FAIL -- unexpected exception:");
174             e.printStackTrace(System.out);
175         }
176     }
177     static private void executeStmt(Connection JavaDoc con, String JavaDoc statement) {
178         try {
179             PreparedStatement JavaDoc ps = con.prepareStatement(statement);
180             ps.execute();
181             ps.close();
182         }
183         catch (SQLException JavaDoc e) {
184             dumpSQLExceptions(e);
185         }
186         catch (Throwable JavaDoc e) {
187             System.out.println("FAIL -- unexpected exception:");
188             e.printStackTrace(System.out);
189         }
190     }
191     static private void dumpSQLExceptions (SQLException JavaDoc se) {
192         System.out.println("FAIL -- unexpected exception");
193         while (se != null) {
194             System.out.print("SQLSTATE("+se.getSQLState()+"):");
195             se.printStackTrace(System.out);
196             se = se.getNextException();
197         }
198     }
199     static void dumpRSMetaData(ResultSetMetaData JavaDoc rsmd) throws SQLException JavaDoc {
200         if (rsmd == null || rsmd.getColumnCount() == 0)
201         {
202             System.out.println("ResultSetMetaData is Null or empty");
203             return;
204         }
205
206         // Get the number of columns in the result set
207
int numCols = rsmd.getColumnCount ();
208
209         if (numCols <= 0) {
210             System.out.println("(no columns!)");
211             return;
212         }
213         
214         // Display column headings
215
for (int i=1; i<=numCols; i++) {
216             if (i > 1) System.out.print(",");
217             System.out.print(rsmd.getColumnLabel(i));
218         }
219         System.out.println();
220     }
221
222     public static void tstmeth()
223     {
224         // for purpose of test, method may do nothing
225
}
226
227 }
228
Popular Tags