KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.rsgetXXXcolumnNames
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 package org.apache.derbyTesting.functionTests.tests.jdbcapi;
22
23
24 import java.sql.*;
25
26 import org.apache.derby.tools.ij;
27 import org.apache.derby.tools.JDBCDisplayUtil;
28
29 import org.apache.derbyTesting.functionTests.util.TestUtil;
30
31 public class rsgetXXXcolumnNames {
32
33     public static void main(String JavaDoc[] args) {
34         test1(args);
35     }
36     
37         public static void test1(String JavaDoc []args) {
38                 Connection con;
39                 ResultSet rs;
40                 Statement stmt = null;
41                 PreparedStatement stmt1 = null;
42
43                 System.out.println("Test rsgetXXXcolumnNames starting");
44
45                 try
46                 {
47                         // use the ij utility to read the property file and
48
// make the initial connection.
49
ij.getPropertyArg(args);
50                         con = ij.startJBMS();
51                     
52
53                         stmt = con.createStatement();
54
55                         // first cleanup in case we're using useprocess false
56
String JavaDoc[] testObjects = {"table caseiscol"};
57                         TestUtil.cleanUpTest(stmt, testObjects);
58
59             con.setAutoCommit(false);
60
61             // create a table with two columns, their names differ in they being in different cases.
62
stmt.executeUpdate("create table caseiscol(COL1 int ,\"col1\" int)");
63
64             con.commit();
65             
66             stmt.executeUpdate("insert into caseiscol values (1,346)");
67
68             con.commit();
69
70                         // select data from this table for updating
71
stmt1 = con.prepareStatement("select COL1, \"col1\" from caseiscol FOR UPDATE",ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
72                 rs = stmt1.executeQuery();
73
74             // Get the data and disply it before updating.
75
System.out.println("Before updation...");
76             while(rs.next()) {
77                System.out.println("ResultSet is: "+rs.getObject(1));
78                System.out.println("ResultSet is: "+rs.getObject(2));
79             }
80                         rs.close();
81             rs = stmt1.executeQuery();
82             while(rs.next()) {
83                // Update the two columns with different data.
84
// Since update is case insensitive only the first column should get updated in both cases.
85
rs.updateInt("col1",100);
86                rs.updateInt("COL1",900);
87                rs.updateRow();
88             }
89             rs.close();
90
91             System.out.println("After update...");
92             rs = stmt1.executeQuery();
93
94             // Display the data after updating. Only the first column should have the updated value.
95
while(rs.next()) {
96                System.out.println("Column Number 1: "+rs.getInt(1));
97                System.out.println("Column Number 2: "+rs.getInt(2));
98             }
99             rs.close();
100             rs = stmt1.executeQuery();
101             while(rs.next()) {
102                // Again checking for case insensitive behaviour here, should display the data in the first column.
103
System.out.println("Col COL1: "+rs.getInt("COL1"));
104                System.out.println("Col col1: "+rs.getInt("col1"));
105             }
106             rs.close();
107         } catch(SQLException sqle) {
108            dumpSQLExceptions(sqle);
109            sqle.printStackTrace();
110         } catch(Throwable JavaDoc e) {
111            System.out.println("FAIL -- unexpected exception: "+e.getMessage());
112                    e.printStackTrace();
113
114         }
115      }
116      
117      static private void dumpSQLExceptions (SQLException se) {
118                 System.out.println("FAIL -- unexpected exception");
119                 while (se != null) {
120                         System.out.println("SQLSTATE("+se.getSQLState()+"): "+se.getMessage());
121                         se = se.getNextException();
122                 }
123         }
124 }
125
Popular Tags