001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.statementJdbc20
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.jdbcapi;
023:
024: import java.sql.Connection;
025: import java.sql.DriverManager;
026: import java.sql.ResultSetMetaData;
027: import java.sql.ResultSet;
028: import java.sql.Statement;
029: import java.sql.SQLException;
030: import java.sql.Types;
031:
032: import org.apache.derby.tools.ij;
033: import org.apache.derby.tools.JDBCDisplayUtil;
034:
035: /**
036: * Test of additional methods in JDBC2.0 methods in statement and
037: * resultset classes.
038: *
039: */
040:
041: public class statementJdbc20 {
042:
043: public static void main(String[] args) {
044: Connection con;
045: ResultSet rs;
046: Statement stmt;
047:
048: System.out.println("Test statementJdbc20 starting");
049:
050: try {
051: // use the ij utility to read the property file and
052: // make the initial connection.
053: ij.getPropertyArg(args);
054: con = ij.startJBMS();
055:
056: stmt = con.createStatement();
057:
058: //create a table, insert a row, do a select from the table,
059: stmt
060: .execute("create table tab1 (i int, s smallint, r real)");
061: stmt.executeUpdate("insert into tab1 values(1, 2, 3.1)");
062:
063: // set all the peformance hint parameters
064: stmt.setFetchSize(25);
065: stmt.setFetchDirection(ResultSet.FETCH_REVERSE);
066: stmt.setEscapeProcessing(true);
067:
068: //Error testing : set wrong values ..
069: try {
070: stmt.setFetchSize(-1000);
071: } catch (SQLException e) {
072: dumpExpectedSQLExceptions(e);
073: }
074:
075: try {
076: stmt.setFetchDirection(-1000);
077: } catch (SQLException e) {
078: dumpExpectedSQLExceptions(e);
079: }
080:
081: System.out.println("Fetch Size " + stmt.getFetchSize());
082: System.out.println("Fetch Direction "
083: + stmt.getFetchDirection());
084:
085: // read the data just for the heck of it
086: rs = stmt.executeQuery("select * from tab1");
087: while (rs.next()) {
088: System.out.println(rs.getInt(1) + " " + rs.getShort(2)
089: + " " + rs.getFloat(3));
090: }
091:
092: // Get the constatnts for a result set
093: System.out.println("Result Set Fetch Size "
094: + rs.getFetchSize());
095: System.out.println("Result Set Fetch Direction "
096: + rs.getFetchDirection());
097:
098: // change values local to result set and get them back
099: rs.setFetchSize(250);
100: try {
101: rs.setFetchDirection(ResultSet.FETCH_FORWARD);
102: } catch (SQLException e) {
103: dumpExpectedSQLExceptions(e);
104: }
105:
106: System.out.println("Result Set Fetch Size "
107: + rs.getFetchSize());
108: System.out.println("Result Set Fetch Direction "
109: + rs.getFetchDirection());
110:
111: // exception conditions
112: stmt.setMaxRows(10);
113: try {
114: rs.setFetchSize(100);
115: } catch (SQLException e) {
116: dumpExpectedSQLExceptions(e);
117: }
118:
119: //Error testing : set wrong values ..
120: try {
121: rs.setFetchSize(-2000);
122: } catch (SQLException e) {
123: dumpExpectedSQLExceptions(e);
124: }
125:
126: try {
127: rs.setFetchDirection(-2000);
128: } catch (SQLException e) {
129: dumpExpectedSQLExceptions(e);
130: }
131:
132: // set the fetch size values to zero .. to ensure
133: // error condtions are correct !
134:
135: rs.setFetchSize(0);
136: stmt.setFetchSize(0);
137:
138: rs.close();
139:
140: //RESOLVE - uncomment tests in 3.5
141: // executeQuery() not allowed on statements
142: // that return a row count
143: try {
144: stmt.executeQuery("create table trash(c1 int)");
145: } catch (SQLException e) {
146: dumpExpectedSQLExceptions(e);
147: }
148:
149: // verify that table was not created
150: try {
151: rs = stmt.executeQuery("select * from trash");
152: System.out
153: .println("select from trash expected to fail");
154: } catch (SQLException e) {
155: dumpExpectedSQLExceptions(e);
156: }
157:
158: // executeUpdate() not allowed on statements
159: // that return a ResultSet
160: try {
161: stmt.executeUpdate("values 1");
162: } catch (SQLException e) {
163: dumpExpectedSQLExceptions(e);
164: }
165:
166: stmt.close();
167: con.close();
168:
169: } catch (SQLException e) {
170: dumpSQLExceptions(e);
171: e.printStackTrace();
172: } catch (Throwable e) {
173: System.out.println("FAIL -- unexpected exception: " + e);
174: e.printStackTrace();
175: }
176:
177: System.out.println("Test statementJdbc20 finished");
178: }
179:
180: static private void dumpSQLExceptions(SQLException se) {
181: System.out.println("FAIL -- unexpected exception");
182: while (se != null) {
183: System.out.println("SQLSTATE(" + se.getSQLState() + "): "
184: + se);
185: se = se.getNextException();
186: }
187: }
188:
189: static private void dumpExpectedSQLExceptions(SQLException se) {
190: System.out.println("PASS -- expected exception");
191: while (se != null) {
192: System.out.println("SQLSTATE(" + se.getSQLState() + "): "
193: + se);
194: se = se.getNextException();
195: }
196: }
197:
198: }
|