EasyManuals Logo

IBM DB2 User Manual

IBM DB2
585 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #138 background imageLoading...
Page #138 background image
Do this by invoking the ExecutionContext.setBatching(false) method. When
you disable batching, you can still execute the batch implicitly or explicitly, but
no more statements are added to the batch. Disabling batching is useful when a
batch already exists, and you want to execute a batch compatible statement,
rather than adding it to the batch.
If you want to clear a batch without executing it, invoke the
ExecutionContext.cancel method.
7. If batch execution was implicit, perform a final, explicit executeBatch to ensure
that all statements have been executed.
In the following code fragment, raises are given to all managers by performing
UPDATEs in a batch. The numbers to the right of selected statements correspond
to the previously-described steps.
Related concepts
“SQL statement execution in SQLJ applications” on page 112
Related tasks
“Making batch updates in JDBC applications” on page 28
“Connecting to a data source using SQLJ” on page 103
“Controlling the execution of SQL statements in SQLJ” on page 142
Related reference
#sql iterator GetMgr(String); // Declare positioned iterator
...
{
GetMgr deptiter; // Declare object of GetMgr class
String mgrnum = null; // Declare host variable for manager number
int raise = 400; // Declare raise amount
int currentSalary; // Declare current salary
String url, username, password; // Declare url, user ID, password
...
TestContext c1 = new TestContext (url, username, password, false); 1
ExecutionContext ec = new ExecutionContext(); 2
ec.setBatching(true); 3
#sql [c1] deptiter =
{SELECT MGRNO FROM DEPARTMENT};
// Assign the result table of the SELECT
// to iterator object deptiter
#sql {FETCH :deptiter INTO :mgrnum};
// Retrieve the first manager number
while (!deptiter.endFetch()) { // Check whether the FETCH returned a row
#sql [c1]
{SELECT SALARY INTO :currentSalary FROM EMPLOYEE
WHERE EMPNO=:mgrnum};
#sql [c1, ec] 4
{UPDATE EMPLOYEE SET SALARY=:(currentSalary+raise)
WHERE EMPNO=:mgrnum};
#sql {FETCH :deptiter INTO :mgrnum };
// Fetch the next row
}
ec.executeBatch(); 5
ec.setBatching(false); 6
#sql [c1] {COMMIT};
deptiter.close(); // Close the iterator
ec.close(); // Close the execution context
c1.close(); // Close the connection
}
Figure 38. Example of performing a batch update
122 Application Programming Guide and Reference for Java

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the IBM DB2 and is the answer not in the manual?

IBM DB2 Specifications

General IconGeneral
DeveloperIBM
Initial Release1983
LicenseProprietary
Written inC, C++
CategoryDatabase Management System (RDBMS)
Operating SystemLinux, Windows
Programming LanguagesC, C++
Data ModelsRelational
EditionsCommunity, Standard, Advanced
Key Featureshigh availability, scalability