1 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 [] args) throws Exception { 14 new TestUpdatableResultSet().test(); 15 } 16 17 int insertStudentUsingResultSet(String name) throws Exception { 18 Statement stmt = conn.createStatement(ResultSet.FETCH_FORWARD, ResultSet.CONCUR_UPDATABLE); 19 ResultSet rs = stmt.executeQuery("SELECT * FROM students FOR UPDATE"); 20 int id = 0; 24 try { 25 rs.last(); 26 id = rs.getInt("student_id"); 27 } catch (Exception e) { 28 e.printStackTrace(); 29 } 30 return id; 31 } 32 33 void test() throws Exception { 34 Class.forName("org.h2.Driver"); 35 conn = DriverManager.getConnection("jdbc:h2:test", "sa", "sa"); 36 41 try { 42 conn.createStatement().executeUpdate("DROP TABLE students"); 43 } catch (SQLException e) { 44 } 46 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")); System.out.println("student id: " + insertStudentUsingResultSet("name-2")); conn.close(); 51 } 52 } 53 | Popular Tags |