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.
// 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); } }
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);