KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > cases > TestUpdatableResultSet


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.cases;
6
7 import java.sql.*;
8
9 class TestUpdatableResultSet {
10
11     Connection conn;
12
13     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
14         new TestUpdatableResultSet().test();
15     }
16
17     int insertStudentUsingResultSet(String JavaDoc name) throws Exception JavaDoc {
18         Statement stmt = conn.createStatement(ResultSet.FETCH_FORWARD, ResultSet.CONCUR_UPDATABLE);
19         ResultSet rs = stmt.executeQuery("SELECT * FROM students FOR UPDATE");
20 // rs.moveToInsertRow();
21
// rs.updateString("name", name);
22
// rs.insertRow();
23
int id = 0;
24         try {
25             rs.last();
26             id = rs.getInt("student_id");
27         } catch (Exception JavaDoc e) {
28             e.printStackTrace();
29         }
30         return id;
31     }
32
33     void test() throws Exception JavaDoc {
34          Class.forName("org.h2.Driver");
35          conn = DriverManager.getConnection("jdbc:h2:test", "sa", "sa");
36 // Class.forName("org.postgresql.Driver");
37
// conn = DriverManager.getConnection("jdbc:postgresql:jpox2", "sa", "sa");
38
// Class.forName("com.mysql.jdbc.Driver");
39
// conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "sa", "sa");
40

41         try {
42             conn.createStatement().executeUpdate("DROP TABLE students");
43         } catch (SQLException e) {
44             //
45
}
46 // conn.createStatement().executeUpdate("CREATE TABLE students (student_id int PRIMARY KEY, name char(20))");
47
conn.createStatement().executeUpdate("CREATE TABLE students (student_id int AUTO_INCREMENT PRIMARY KEY, name char(20))");
48         System.out.println("student id: " + insertStudentUsingResultSet("name-1")); // output must be 1,it will print 0,
49
System.out.println("student id: " + insertStudentUsingResultSet("name-2")); // output must be 2, it will print 1
50
conn.close();
51     }
52 }
53
Popular Tags