KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.dbMetaDataJdbc30
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.DatabaseMetaData JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.ResultSetMetaData JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.sql.Statement JavaDoc;
30
31 import org.apache.derby.iapi.reference.JDBC30Translation;
32 import org.apache.derby.iapi.services.info.JVMInfo;
33
34 import org.apache.derby.tools.ij;
35 import org.apache.derbyTesting.functionTests.util.TestUtil;
36
37 /**
38  * Test of database meta-data for new methods in jdbc 30. This program simply calls
39  * each of the new meta-data methods in jdbc30, one by one, and prints the results.
40  *
41  * @author mamta
42  */

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

63             s = con.createStatement();
64
65             met = con.getMetaData();
66
67             System.out.println("JDBC Driver '" + met.getDriverName() +
68                                "', version " + met.getDriverMajorVersion() +
69                                "." + met.getDriverMinorVersion() +
70                                " (" + met.getDriverVersion() + ")");
71
72             boolean pass = false;
73             try {
74                 pass = TestUtil.compareURL(met.getURL());
75             }catch (NoSuchMethodError JavaDoc msme) {
76                 // DatabaseMetaData.getURL not present - correct for JSR169
77
if(!TestUtil.HAVE_DRIVER_CLASS)
78                     pass = true;
79             } catch (Throwable JavaDoc err) {
80                 System.out.println("%%getURL() gave the exception: " + err);
81             }
82             
83             if(pass)
84                 System.out.println("DatabaseMetaData.getURL test passed");
85             else
86                 System.out.println("FAIL: DatabaseMetaData.getURL test failed");
87             
88             System.out.println();
89             System.out.println("supportsSavepoints() : " + met.supportsSavepoints());
90
91             System.out.println();
92             System.out.println("supportsNamedParameters() : " + met.supportsNamedParameters());
93
94             System.out.println();
95             System.out.println("supportsMultipleOpenResults() : " + met.supportsMultipleOpenResults());
96
97             System.out.println();
98             System.out.println("supportsGetGeneratedKeys() : " + met.supportsGetGeneratedKeys());
99
100       System.out.println();
101       System.out.println("supportsResultSetHoldability(HOLD_CURSORS_OVER_COMMIT) : " +
102       met.supportsResultSetHoldability(JDBC30Translation.HOLD_CURSORS_OVER_COMMIT));
103
104             System.out.println();
105             System.out.println("supportsResultSetHoldability(CLOSE_CURSORS_AT_COMMIT) : " +
106         met.supportsResultSetHoldability(JDBC30Translation.CLOSE_CURSORS_AT_COMMIT));
107
108             System.out.println();
109             checkJDBCVersion(met);
110
111             System.out.println();
112             System.out.println("getSQLStateType() : " + met.getSQLStateType());
113
114             System.out.println();
115             System.out.println("getResultSetHoldability() : " + met.getResultSetHoldability());
116
117             System.out.println();
118             System.out.println("getDatabaseMajorVersion() : " + met.getDatabaseMajorVersion());
119
120             System.out.println();
121             System.out.println("getDatabaseMinorVersion() : " + met.getDatabaseMinorVersion());
122
123             System.out.println();
124             System.out.println("supportsStatementPooling() : " + met.supportsStatementPooling());
125
126             System.out.println("getMaxColumnNameLength() = "+met.getMaxColumnNameLength());
127             System.out.println("getMaxCursorNameLength() = "+met.getMaxCursorNameLength());
128             System.out.println("getMaxSchemaNameLength() = "+met.getMaxSchemaNameLength());
129             System.out.println("getMaxProcedureNameLength() = "+met.getMaxProcedureNameLength());
130             System.out.println("getMaxTableNameLength() = "+met.getMaxTableNameLength());
131             System.out.println("getMaxUserNameLength() = "+met.getMaxUserNameLength());
132
133             //following will give not implemented exceptions.
134
// JCC will return an empty result set, so either a
135
// result set with no rows or an exception will pass
136
System.out.println();
137             System.out.println("getSuperTypes() with null :");
138             checkEmptyRS(met.getSuperTypes(null,null,null));
139
140             System.out.println();
141             System.out.println("getSuperTables() with null :");
142             checkEmptyRS(met.getSuperTables(null,null,null));
143
144             System.out.println();
145             System.out.println("getAttributes() with null :");
146
147             checkEmptyRS(met.getAttributes(null, null, null, null));
148
149             System.out.println();
150             System.out.println("locatorsUpdateCopy(): ");
151             System.out.println("Returned: " + met.locatorsUpdateCopy());
152         
153             s.close();
154
155             con.close();
156
157         }
158         catch (SQLException JavaDoc e) {
159             dumpSQLExceptions(e);
160         }
161         catch (Throwable JavaDoc e) {
162             System.out.println("FAIL -- unexpected exception:");
163             e.printStackTrace(System.out);
164         }
165
166         System.out.println("Test dbMetaDataJdbc30 finished");
167     }
168
169     static private void dumpSQLExceptions (SQLException JavaDoc se) {
170         System.out.println("FAIL -- unexpected exception");
171         while (se != null) {
172             System.out.print("SQLSTATE("+se.getSQLState()+"):");
173             se.printStackTrace(System.out);
174             se = se.getNextException();
175         }
176     }
177
178     public static void dumpRS(ResultSet JavaDoc s) throws SQLException JavaDoc {
179         ResultSetMetaData JavaDoc rsmd = s.getMetaData ();
180
181         // Get the number of columns in the result set
182
int numCols = rsmd.getColumnCount ();
183
184         if (numCols <= 0) {
185             System.out.println("(no columns!)");
186             return;
187         }
188         
189         // Display column headings
190
for (int i=1; i<=numCols; i++) {
191             if (i > 1) System.out.print(",");
192             System.out.print(rsmd.getColumnLabel(i));
193         }
194         System.out.println();
195     
196         // Display data, fetching until end of the result set
197
while (s.next()) {
198             // Loop through each column, getting the
199
// column data and displaying
200
for (int i=1; i<=numCols; i++) {
201                 if (i > 1) System.out.print(",");
202                 System.out.print(s.getString(i));
203             }
204             System.out.println();
205         }
206         s.close();
207     }
208
209     /**
210      * Check whether <code>getJDBCMajorVersion()</code> and
211      * <code>getJDBCMinorVersion()</code> return the expected version numbers.
212      * @param met the <code>DatabaseMetaData</code> object to test
213      * @exception SQLException if a database error occurs
214      */

215     private static void checkJDBCVersion(DatabaseMetaData JavaDoc met)
216         throws SQLException JavaDoc
217     {
218         final int major, minor;
219         if (TestUtil.isJCCFramework()) {
220             major = 3;
221             minor = 0;
222         } else if (JVMInfo.JDK_ID < JVMInfo.J2SE_16) {
223             major = 3;
224             minor = 0;
225         } else {
226             major = 4;
227             minor = 0;
228         }
229         System.out.print("getJDBCMajorVersion()/getJDBCMinorVersion() : ");
230         int maj = met.getJDBCMajorVersion();
231         int min = met.getJDBCMinorVersion();
232         if (major == maj && minor == min) {
233             System.out.println("AS EXPECTED");
234         } else {
235             System.out.println("GOT " + maj + "." + min +
236                                ", EXPECTED " + major + "." + minor);
237         }
238     }
239
240     /**
241      * In order to be JDBC compliant, all metadata calls must return valid
242      * results, even if it's an empty result set. It should be considered
243      * a failure if we throw an exception
244      */

245     public static void checkEmptyRS(ResultSet JavaDoc rs)
246     {
247         boolean passed = false;
248
249         try {
250             if (rs != null)
251             {
252                 int numrows = 0;
253                 while (rs.next())
254                     numrows++;
255                 // Zero rows is what we want.
256
if (numrows == 0)
257                     passed = true;
258             }
259         }
260         catch (SQLException JavaDoc e)
261         {
262             System.out.println("Unexpected SQL Exception" +
263                                e.getMessage());
264             e.printStackTrace();
265         }
266         finally
267         {
268             if (passed)
269                 System.out.println("EXPECTED: Empty ResultSet");
270             else
271                 System.out.println("FAIL: Should have gotten empty ResultSet");
272         }
273     }
274 }
275
Popular Tags