IBM DB2 Manual page 133

Table of Contents

Advertisement

Then, in another file, use UpdByName for a positioned UPDATE, as shown in the
following code fragment:
import sqlj.runtime.*;
import java.sql.*;
import java.math.*;
import UpdByName;
#sql context 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");
HSjdbccon.setAutoCommit(false);
HSCtx myConnCtx=new HSCtx(HSjdbccon);
UpdByName upditer;
String empnum;
#sql [myConnCtx]
upditer = {SELECT EMPNO, SALARY FROM EMPLOYEE
WHERE WORKDEPT='D11'};
while (upditer.next())
{
empnum = upditer.EmpNo(); // Get employee number from current row
#sql [myConnCtx]
{UPDATE EMPLOYEE SET SALARY=SALARY*1.05
WHERE CURRENT OF :upditer};
System.out.println("Updating row for " + empnum);
}
upditer.close();
#sql [myConnCtx] {COMMIT};
myConnCtx.close();
}
Figure 35. Example of performing a positioned UPDATE with a named iterator
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 files for SQLJ and JDBC APIs
// Import this class for BigDecimal data type
// Import the generated iterator class that
// was created by the iterator declaration clause
// for UpdByName in another file
// Create a connnection context class HSCtx
// Create a JDBC connection object
// Set autocommit off so automatic commits 2
// do not destroy the cursor between updates
// Create a connection context object
// Declare iterator object of UpdByName class
// Declare host variable to receive EmpNo
// column values
// Assign result table to iterator object
// Move cursor to next row and
// check ifon a row
// Perform positioned update
// Close the iterator
// Commit the changes
// Close the connection context
Chapter 4. SQLJ application programming
3
4
5a,5b
5c
6
117

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents