KickJava   Java API By Example, From Geeks To Geeks.

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


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

36
37 public class statementJdbc30 {
38
39     public static void main(String JavaDoc[] args) {
40         Connection JavaDoc con;
41         ResultSet JavaDoc rs;
42         Statement JavaDoc stmt;
43
44         System.out.println("Test statementJdbc30 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
54             stmt = con.createStatement();
55
56             //create a table, insert a row, do a select from the table,
57
stmt.execute("create table tab1 (i int, s smallint, r real)");
58             stmt.executeUpdate("insert into tab1 values(1, 2, 3.1)");
59
60             // read the data just for the heck of it
61
rs = stmt.executeQuery("select * from tab1");
62             rs.next();
63
64       System.out.println("trying stmt.getMoreResults(int) :");
65             stmt.getMoreResults(JDBC30Translation.CLOSE_CURRENT_RESULT);
66
67             System.out.println("trying stmt.executeUpdate(String, int) :");
68             stmt.executeUpdate("insert into tab1 values(2, 3, 4.1)", JDBC30Translation.NO_GENERATED_KEYS);
69
70             System.out.println("trying stmt.executeUpdate(String, int[]) :");
71             int[] columnIndexes = new int[2];
72             columnIndexes[0] = 1;
73             columnIndexes[1] = 2;
74             try {
75                 stmt.executeUpdate("insert into tab1 values(2, 3, 4.1)", columnIndexes);
76             } catch (SQLException JavaDoc ex) {
77                 dumpExpectedSQLExceptions(ex);
78             }
79
80             System.out.println("trying stmt.executeUpdate(String, String[]) :");
81             String JavaDoc[] columnNames = new String JavaDoc[2];
82             columnNames[0] = "I";
83             columnNames[1] = "S";
84             try {
85                 stmt.executeUpdate("insert into tab1 values(2, 3, 4.1)", columnNames);
86             } catch (SQLException JavaDoc ex) {
87                 dumpExpectedSQLExceptions(ex);
88             }
89
90             System.out.println("trying stmt.execute(String, int) :");
91             stmt.execute("select * from tab1", JDBC30Translation.NO_GENERATED_KEYS);
92
93             System.out.println("trying stmt.execute(String, int[]) :");
94             try {
95                 stmt.execute("insert into tab1 values(2, 3, 4.1)", columnIndexes);
96             } catch (SQLException JavaDoc ex) {
97                 dumpExpectedSQLExceptions(ex);
98             }
99
100             System.out.println("trying stmt.execute(String, String[]) :");
101             try {
102                 stmt.execute("insert into tab1 values(2, 3, 4.1)", columnNames);
103             } catch (SQLException JavaDoc ex) {
104                 dumpExpectedSQLExceptions(ex);
105             }
106
107             System.out.println("trying stmt.getResultSetHoldability() :");
108             stmt.getResultSetHoldability();
109
110             System.out.println("trying stmt.getGeneratedKeys() :");
111             stmt.getGeneratedKeys();
112
113             rs.close();
114             stmt.close();
115             con.close();
116
117         }
118         catch (SQLException JavaDoc e) {
119             System.out.println("Expected : " + e.getMessage());
120         }
121         catch (Throwable JavaDoc e) {
122             System.out.println("FAIL -- unexpected exception: "+e);
123             e.printStackTrace();
124         }
125
126         System.out.println("Test statementJdbc30 finished");
127     }
128
129     public static void dumpExpectedSQLExceptions (SQLException JavaDoc se) {
130         System.out.println("PASS -- expected exception");
131         while (se != null)
132         {
133             System.out.println("SQLSTATE("+se.getSQLState()+"): "+se);
134             se = se.getNextException();
135         }
136     }
137 }
138
Popular Tags