Using DB2ResultSet methods or DB2PreparedStatement constants to provide extended parameter information

Use DB2ResultSet methods or ResultSet methods with DB2PreparedStatement constants to assign default values to target columns in a DB2ResultSet.

About this task

Follow these steps to update a ResultSet with extended client information.

Procedure

  1. Create a PreparedStatement object.

    The SQL statement is a SELECT statement.

  2. Invoke PreparedStatement.setXXX methods to pass values to any input parameters.
  3. Invoke the PreparedStatement.executeQuery method to obtain the result table from the SELECT statement in a ResultSet object.
  4. Position the cursor to the row that you want to update or insert.
  5. Update columns in the ResultSet row.
    • If you are not using updateObject to update a value:
      • To assign the default value to the target column of the ResultSet, cast the ResultSet to a DB2ResultSet, and call DB2ResultSet.updateDBDefault.
    • If you are using updateObject to assign the value:
      • To assign the default value to the target column of the ResultSet, call ResultSet.updateObject with DB2PreparedStatement.DB_PARAMETER_DEFAULT as the assigned value.
  6. Execute ResultSet.updateRow if you are updating an existing row, or ResultSet.insertRow if you are inserting a new row.

Example

The following code inserts a row into a ResultSet with the default value in the second column, and does not modify the value in the first column. The numbers to the right of selected statements correspond to the previously described steps.

import java.sql.*;
import com.ibm.db2.jcc.*;

Connection conn;
…
PreparedStatement p = conn.prepareStatement (          1 
  "SELECT MGRNO, LOCATION " +
  "FROM DEPARTMENT");
ResultSet rs = p.executeQuery ();                      3 
rs.next ();
rs.moveToInsertRow();                                  4 
((DB2ResultSet)rs).updateDBDefault (2);                5 
rs.insertRow();                                        6 
…
rs.close();                           // Close ResultSet
p.close();                            // Close PreparedStatement

The following code uses the ResultSet interface with DB2PreparedStatement constants to perform the same function as in the previous example. The numbers to the right of selected statements correspond to the previously described steps.

import java.sql.*;
import com.ibm.db2.jcc.*;

Connection conn;
…
PreparedStatement p = conn.prepareStatement (          1 
  "SELECT MGRNO, LOCATION " +
  "FROM DEPARTMENT");
ResultSet rs = p.executeQuery ();                      3 
rs.next ();
rs.moveToInsertRow();                                  4 
rs.updateObject (2,                                    5 
 DB2PreparedStatement.DB_PARAMETER_DEFAULT);
rs.insertRow();                                        6 
…
rs.close();                           // Close ResultSet
p.close();                            // Close PreparedStatement