KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.statementJdbc20
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.ResultSetMetaData JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Types JavaDoc;
31
32 import org.apache.derby.tools.ij;
33 import org.apache.derby.tools.JDBCDisplayUtil;
34
35 /**
36  * Test of additional methods in JDBC2.0 methods in statement and
37  * resultset classes.
38  *
39  */

40
41 public class statementJdbc20 {
42     
43     public static void main(String JavaDoc[] args) {
44         Connection JavaDoc con;
45         ResultSet JavaDoc rs;
46         Statement JavaDoc stmt;
47
48         System.out.println("Test statementJdbc20 starting");
49
50         try
51         {
52             // use the ij utility to read the property file and
53
// make the initial connection.
54
ij.getPropertyArg(args);
55             con = ij.startJBMS();
56
57
58             stmt = con.createStatement();
59
60             //create a table, insert a row, do a select from the table,
61
stmt.execute("create table tab1 (i int, s smallint, r real)");
62             stmt.executeUpdate("insert into tab1 values(1, 2, 3.1)");
63
64             // set all the peformance hint parameters
65
stmt.setFetchSize(25);
66             stmt.setFetchDirection(ResultSet.FETCH_REVERSE);
67             stmt.setEscapeProcessing(true);
68
69            //Error testing : set wrong values ..
70
try{
71               stmt.setFetchSize(-1000);
72            }
73            catch(SQLException JavaDoc e){
74               dumpExpectedSQLExceptions(e);
75            }
76
77            try{
78               stmt.setFetchDirection(-1000);
79            }
80            catch(SQLException JavaDoc e){
81               dumpExpectedSQLExceptions(e);
82            }
83             
84
85             System.out.println("Fetch Size " + stmt.getFetchSize());
86             System.out.println("Fetch Direction " + stmt.getFetchDirection());
87
88             // read the data just for the heck of it
89
rs = stmt.executeQuery("select * from tab1");
90             while (rs.next())
91             {
92                System.out.println(rs.getInt(1) + " " + rs.getShort(2) +
93                                    " " + rs.getFloat(3));
94             }
95
96             // Get the constatnts for a result set
97
System.out.println("Result Set Fetch Size " + rs.getFetchSize());
98             System.out.println("Result Set Fetch Direction " + rs.getFetchDirection());
99
100            // change values local to result set and get them back
101
rs.setFetchSize(250);
102             try{
103                rs.setFetchDirection(ResultSet.FETCH_FORWARD);
104             }catch(SQLException JavaDoc e){
105               dumpExpectedSQLExceptions(e);
106             }
107
108             System.out.println("Result Set Fetch Size " + rs.getFetchSize());
109             System.out.println("Result Set Fetch Direction " + rs.getFetchDirection());
110
111           // exception conditions
112
stmt.setMaxRows(10);
113            try{
114               rs.setFetchSize(100);
115            }
116            catch(SQLException JavaDoc e){
117               dumpExpectedSQLExceptions(e);
118            }
119
120            //Error testing : set wrong values ..
121
try{
122               rs.setFetchSize(-2000);
123            }
124            catch(SQLException JavaDoc e){
125               dumpExpectedSQLExceptions(e);
126            }
127
128            try{
129               rs.setFetchDirection(-2000);
130            }
131            catch(SQLException JavaDoc e){
132               dumpExpectedSQLExceptions(e);
133            }
134
135            // set the fetch size values to zero .. to ensure
136
// error condtions are correct !
137

138             rs.setFetchSize(0);
139             stmt.setFetchSize(0);
140        
141             rs.close();
142
143             //RESOLVE - uncomment tests in 3.5
144
// executeQuery() not allowed on statements
145
// that return a row count
146
try
147             {
148                 stmt.executeQuery("create table trash(c1 int)");
149             }
150             catch (SQLException JavaDoc e)
151             {
152               dumpExpectedSQLExceptions(e);
153             }
154
155             // verify that table was not created
156
try
157             {
158                 rs = stmt.executeQuery("select * from trash");
159                 System.out.println("select from trash expected to fail");
160             }
161             catch (SQLException JavaDoc e)
162             {
163               dumpExpectedSQLExceptions(e);
164             }
165
166             // executeUpdate() not allowed on statements
167
// that return a ResultSet
168
try
169             {
170                 stmt.executeUpdate("values 1");
171             }
172             catch (SQLException JavaDoc e)
173             {
174               dumpExpectedSQLExceptions(e);
175             }
176
177             stmt.close();
178             con.close();
179
180         }
181         catch (SQLException JavaDoc e) {
182             dumpSQLExceptions(e);
183             e.printStackTrace();
184         }
185         catch (Throwable JavaDoc e) {
186             System.out.println("FAIL -- unexpected exception: "+e);
187             e.printStackTrace();
188         }
189
190         System.out.println("Test statementJdbc20 finished");
191     }
192
193     static private void dumpSQLExceptions (SQLException JavaDoc se) {
194         System.out.println("FAIL -- unexpected exception");
195         while (se != null) {
196             System.out.println("SQLSTATE("+se.getSQLState()+"): "+se);
197             se = se.getNextException();
198         }
199     }
200
201     static private void dumpExpectedSQLExceptions (SQLException JavaDoc se) {
202         System.out.println("PASS -- expected exception");
203         while (se != null) {
204             System.out.println("SQLSTATE("+se.getSQLState()+"): "+se);
205             se = se.getNextException();
206         }
207     }
208
209 }
210
Popular Tags