Then, in another file, use UpdByName for a positioned UPDATE, as shown in the
following code fragment:
Related concepts
“SQL statement execution in SQLJ applications” on page 112
“Iterators as passed variables for positioned UPDATE or DELETE operations in
an SQLJ application” on page 118
“Data retrieval in SQLJ applications” on page 123
″Authorization IDs and dynamic SQL″ (DB2 SQL Reference)
Related tasks
“Creating and modifying DB2 objects in an SQLJ application” on page 113
“Connecting to a data source using SQLJ” on page 103
import sqlj.runtime.*; // Import files for SQLJ and JDBC APIs
import java.sql.*;
import java.math.*; // Import this class for BigDecimal data type
import UpdByName; // Import the generated iterator class that
// was created by the iterator declaration clause
// for UpdByName in another file
#sql context HSCtx; // Create a connnection context class HSCtx
public static void main (String args[])
{
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection HSjdbccon=
DriverManager.getConnection("jdbc:db2:SANJOSE");
// Create a JDBC connection object
HSjdbccon.setAutoCommit(false);
// Set autocommit off so automatic commits 2
// do not destroy the cursor between updates
HSCtx myConnCtx=new HSCtx(HSjdbccon);
// Create a connection context object
UpdByName upditer; 3
// Declare iterator object of UpdByName class
String empnum; // Declare host variable to receive EmpNo
// column values
#sql [myConnCtx]
upditer = {SELECT EMPNO, SALARY FROM EMPLOYEE 4
WHERE WORKDEPT='D11'};
// Assign result table to iterator object
while (upditer.next()) 5a,5b
// Move cursor to next row and
// check ifon a row
{
empnum = upditer.EmpNo(); // Get employee number from current row
#sql [myConnCtx]
{UPDATE EMPLOYEE SET SALARY=SALARY*1.05
WHERE CURRENT OF :upditer}; 5c
// Perform positioned update
System.out.println("Updating row for " + empnum);
}
upditer.close(); // Close the iterator 6
#sql [myConnCtx] {COMMIT};
// Commit the changes
myConnCtx.close(); // Close the connection context
}
Figure 35. Example of performing a positioned UPDATE with a named iterator
Chapter 4. SQLJ application programming 117