KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > bug5054


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.lang.bug5054
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.lang;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.sql.SQLException JavaDoc;
28
29 import org.apache.derby.tools.ij;
30 import org.apache.derby.tools.JDBCDisplayUtil;
31
32 /**
33  * Where current of cursorname and case sensitivity
34  */

35 public class bug5054 {
36
37   public static void main (String JavaDoc args[])
38   {
39     try {
40         /* Load the JDBC Driver class */
41         // use the ij utility to read the property file and
42
// make the initial connection.
43
ij.getPropertyArg(args);
44         Connection JavaDoc conn = ij.startJBMS();
45
46         createTables(conn);
47         doUpdates(conn);
48         dumpResult(conn);
49         cleanUp(conn);
50         conn.close();
51     } catch (Exception JavaDoc e) {
52         System.out.println("FAIL -- unexpected exception "+e);
53         JDBCDisplayUtil.ShowException(System.out, e);
54         e.printStackTrace();
55     }
56   }
57
58   private static void createTables(Connection JavaDoc conn) throws SQLException JavaDoc {
59     Statement JavaDoc stmt = conn.createStatement();
60     try {
61         stmt.executeUpdate( "DROP TABLE T1" );
62     }catch (Exception JavaDoc e) {}
63
64     System.out.print("Creating tables...");
65     stmt.executeUpdate( "CREATE TABLE T1 (a integer, b integer)" );
66     stmt.executeUpdate( "INSERT INTO T1 VALUES(1, 1)" );
67     stmt.executeUpdate( "INSERT INTO T1 VALUES(2, 2)" );
68     System.out.println("done.");
69
70     stmt.close();
71   }
72
73   private static void cleanUp(Connection JavaDoc conn) throws SQLException JavaDoc {
74     Statement JavaDoc stmt = conn.createStatement();
75     try {
76         stmt.executeUpdate( "DROP TABLE T1" );
77     }catch (Exception JavaDoc e) {}
78     stmt.close();
79   }
80
81   private static void doUpdates(Connection JavaDoc conn) throws SQLException JavaDoc
82   {
83     int rc;
84     conn.setAutoCommit(false);
85     Statement JavaDoc stmt1 = conn.createStatement();
86     stmt1.setCursorName("aBc");
87     ResultSet JavaDoc rs = stmt1.executeQuery("select * from t1 for update");
88     System.out.println("cursor name is " + rs.getCursorName());
89     rs.next();
90
91     Statement JavaDoc stmt2 = conn.createStatement();
92     stmt2.execute("update t1 set b=11 where current of \"" + rs.getCursorName() + "\"");
93
94     conn.commit();
95     stmt1.close();
96     stmt2.close();
97     conn.setAutoCommit(true);
98   }
99
100   private static void dumpResult(Connection JavaDoc conn) throws SQLException JavaDoc
101   {
102     Statement JavaDoc stmt = conn.createStatement();
103     ResultSet JavaDoc rs = stmt.executeQuery(
104       "SELECT * FROM T1"
105     );
106     System.out.println("T1 contents:");
107     System.out.println("First row should have a b value of 11");
108     while (rs.next()) {
109       System.out.println(rs.getInt(1)+ " " + rs.getInt(2));
110     }
111     rs.close();
112   }
113
114 }
115
Popular Tags