KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.resultsetJdbc30
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.Array JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.sql.Statement JavaDoc;
29
30 import org.apache.derby.tools.ij;
31
32 /**
33  * Test of additional methods in JDBC3.0 result set
34  *
35  */

36
37 public class resultsetJdbc30 {
38     public static void main(String JavaDoc[] args) {
39         Connection JavaDoc con;
40         ResultSet JavaDoc rs;
41         Statement JavaDoc stmt;
42         String JavaDoc[] columnNames = {"i", "s", "r", "d", "dt", "t", "ts", "c", "v", "tn", "dc"};
43
44         System.out.println("Test resultsetJdbc30 starting");
45
46         try
47         {
48             // use the ij utility to read the property file and
49
// make the initial connection.
50
ij.getPropertyArg(args);
51             con = ij.startJBMS();
52
53             stmt = con.createStatement();
54
55       //create a table, insert a row, do a select from the table,
56
stmt.execute("create table t (i int, s smallint, r real, "+
57                 "d double precision, dt date, t time, ts timestamp, "+
58                 "c char(10), v varchar(40) not null, dc dec(10,2))");
59             stmt.execute("insert into t values(1,2,3.3,4.4,date('1990-05-05'),"+
60                          "time('12:06:06'),timestamp('1990-07-07 07:07:07.07'),"+
61                          "'eight','nine', 11.1)");
62
63             rs = stmt.executeQuery("select * from t");
64             rs.next();
65
66             //following will give not implemented exceptions.
67
try {
68               System.out.println();
69               System.out.println("trying rs.getURL(int) :");
70               rs.getURL(8);
71               System.out.println("Shouldn't reach here. Method not implemented yet.");
72             } catch (SQLException JavaDoc ex) {
73               System.out.println("Expected : " + ex.getMessage());
74             }
75
76             try {
77               System.out.println();
78               System.out.println("trying rs.getURL(String) :");
79               rs.getURL("c");
80               System.out.println("Shouldn't reach here. Method not implemented yet.");
81             } catch (SQLException JavaDoc ex) {
82               System.out.println("Expected : " + ex.getMessage());
83             }
84
85             try {
86               System.out.println();
87               System.out.println("trying rs.updateRef(int, Ref) :");
88               rs.updateRef(8,null);
89               System.out.println("Shouldn't reach here. Method not implemented yet.");
90             } catch (NoSuchMethodError JavaDoc nsme) {
91                 System.out.println("ResultSet.updateRef not present - correct for JSR169");
92             }
93             catch (SQLException JavaDoc ex) {
94               System.out.println("Expected : " + ex.getMessage());
95             }
96
97             try {
98               System.out.println();
99               System.out.println("trying rs.updateRef(String, Ref) :");
100               rs.updateRef("c",null);
101               System.out.println("Shouldn't reach here. Method not implemented yet.");
102             } catch (NoSuchMethodError JavaDoc nsme) {
103                 System.out.println("ResultSet.updateRef not present - correct for JSR169");
104             } catch (SQLException JavaDoc ex) {
105               System.out.println("Expected : " + ex.getMessage());
106             }
107
108             try {
109               System.out.println();
110               System.out.println("trying rs.updateBlob(int, Blob) :");
111               rs.updateBlob(8,null);
112               System.out.println("Shouldn't reach here because method is being invoked on a read only resultset");
113             } catch (SQLException JavaDoc ex) {
114               System.out.println("Expected : " + ex.getMessage());
115             }
116
117             try {
118               System.out.println();
119               System.out.println("trying rs.updateBlob(String, Blob) :");
120               rs.updateBlob("c",null);
121               System.out.println("Shouldn't reach here because method is being invoked on a read only resultset");
122             } catch (SQLException JavaDoc ex) {
123               System.out.println("Expected : " + ex.getMessage());
124             }
125
126             try {
127               System.out.println();
128               System.out.println("trying rs.updateClob(int, Clob) :");
129               rs.updateClob(8,null);
130               System.out.println("Shouldn't reach here because method is being invoked on a read only resultset");
131             } catch (SQLException JavaDoc ex) {
132               System.out.println("Expected : " + ex.getMessage());
133             }
134
135             try {
136               System.out.println();
137               System.out.println("trying rs.updateClob(String, Clob) :");
138               rs.updateClob("c",null);
139               System.out.println("Shouldn't reach here because method is being invoked on a read only resultset");
140             } catch (SQLException JavaDoc ex) {
141               System.out.println("Expected : " + ex.getMessage());
142             }
143
144             try {
145               System.out.println();
146               System.out.println("trying rs.updateArray(int, Array) :");
147               rs.updateArray(8,null);
148               System.out.println("Shouldn't reach here. Method not implemented yet.");
149             } catch (NoSuchMethodError JavaDoc nsme) {
150                 System.out.println("ResultSet.updateArray not present - correct for JSR169");
151             } catch (SQLException JavaDoc ex) {
152               System.out.println("Expected : " + ex.getMessage());
153             }
154
155             try {
156               System.out.println();
157               System.out.println("trying rs.updateArray(String, Array) :");
158               rs.updateArray("c",null);
159               System.out.println("Shouldn't reach here. Method not implemented yet.");
160             } catch (NoSuchMethodError JavaDoc nsme) {
161                 System.out.println("ResultSet.updateArray not present - correct for JSR169");
162             }catch (SQLException JavaDoc ex) {
163               System.out.println("Expected : " + ex.getMessage());
164             }
165
166             rs.close();
167             stmt.close();
168             
169             //
170
// Check our behavior around closing result sets when auto-commit
171
// is true. Test with both holdable and non-holdable result sets
172
//
173
con.setAutoCommit(true);
174             
175             // Create a non-updatable holdable result set, and then try to
176
// update it
177
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
178                 ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
179             
180             rs = stmt.executeQuery("select * from t");
181             rs.next();
182             
183             checkForCloseOnException(rs, true);
184             
185             rs.close();
186             stmt.close();
187
188             // Create a non-updatable non-holdable result set, and then try to
189
// update it
190
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
191                 ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT);
192             
193             rs = stmt.executeQuery("select * from t");
194             rs.next();
195             
196             checkForCloseOnException(rs, false);
197                 
198             rs.close();
199             stmt.close();
200             con.close();
201
202         }
203         catch (SQLException JavaDoc e) {
204             dumpSQLExceptions(e);
205             e.printStackTrace();
206         }
207         catch (Throwable JavaDoc e) {
208             System.out.println("FAIL -- unexpected exception: "+e);
209             e.printStackTrace();
210         }
211
212         System.out.println("Test resultsetJdbc30 finished");
213     }
214     
215     static private void checkForCloseOnException(ResultSet JavaDoc rs, boolean holdable)
216             throws Exception JavaDoc {
217         try {
218             rs.updateBlob("c",null);
219             throw new Exception JavaDoc("rs.updateBlob() on a read-only result set" +
220                 "should not have succeeded");
221         } catch (SQLException JavaDoc ex) {
222         }
223
224         try {
225             rs.beforeFirst();
226             String JavaDoc holdableStr = holdable ? "holdable" : "non-holdable";
227             System.out.println(holdableStr + " result set was not closed on exception");
228         }
229         catch ( SQLException JavaDoc ex) {
230             String JavaDoc state = ex.getSQLState();
231             if ( state.equals("XCL16"))
232             {
233                 System.out.println("Holdable result set was closed on exception");
234             }
235             else
236             {
237                 throw ex;
238             }
239         }
240
241     }
242
243     static private void dumpSQLExceptions (SQLException JavaDoc se) {
244         System.out.println("FAIL -- unexpected exception");
245         while (se != null) {
246             System.out.println("SQLSTATE("+se.getSQLState()+"): "+se);
247             se = se.getNextException();
248         }
249     }
250
251 }
252
Popular Tags