Creating a cursor  Positioned updates and deletes using JDBC 2.0 methods

Chapter 2: Programming Information

Positioned updates and deletes using JDBC 1.x methods

The following example shows how to use methods in JDBC 1.x to do a positioned update. The example creates two Statement objects, one for selecting rows into a cursor result set, and the other for updating the database from rows in the result set.

NoteAlthough this manual provides sample code relating to JDBC 1.0 and 2.0 methods, Sybase strongly suggests that you use JDBC 2.0 for ease of use and portability.

// Create two statement objects and create a cursor
// for the result set returned by the first 
// statement, stmt1. Use stmt1 to execute a query 
// and return a cursor result set.
Statement stmt1 = conn.createStatement();
Statement stmt2 = conn.createStatement();
stmt1.setCursorName("author_cursor");
ResultSet rs = stmt1.executeQuery("SELECT
    au_id,au_lname, au_fname
    FROM authors WHERE city = 'Oakland'
    FOR UPDATE OF au_lname");
 
// Get the name of the cursor created for stmt1 so 
// that it can be used with stmt2.
String cursor = rs.getCursorName();
 
// Use stmt2 to update the database from the 
// result set returned by stmt1.
String last_name = new String("Smith");
while(rs.next())
{
    if (rs.getString(1).equals("274-80-9391"))
     {
       stmt2.executeUpdate("UPDATE authors "+
       "SET au_lname = "+last_name +
       "WHERE CURRENT OF " + cursor);
    }
}

Deletions in a result set

The following example uses Statement object stmt2, from the preceding code, to perform a positioned deletion:

stmt2.executeUpdate("DELETE FROM authors
          WHERE CURRENT OF " + cursor);




Copyright © 2003. Sybase Inc. All rights reserved. Positioned updates and deletes using JDBC 2.0 methods

View this book as PDF