IBM DB2 Manual page 50

Table of Contents

Advertisement

String empnum, phonenum;
Connection con;
PreparedStatement pstmt;
ResultSet rs;
...
pstmt = con.prepareStatement(
pstmt.setString(1,"000010");
rs = pstmt.executeQuery();
while (rs.next()) {
}
rs.close();
pstmt.close();
Figure 13. Example of using PreparedStatement.executeQuery
Making batch queries in JDBC applications
The IBM Data Server Driver for JDBC and SQLJ provides a IBM Data Server
Driver for JDBC and SQLJ-only DB2PreparedStatement interface that lets you
perform batch queries on a homogeneous batch.
To make batch queries using a single statement with several sets of input
parameters, follow these basic steps:
1. Invoke the prepareStatement method to create a PreparedStatement object for
2. For each set of input parameter values:
3. Cast the PreparedStatement object to a DB2PreparedStatement object, and
4. In a loop, retrieve each ResultSet object that is associated with the
Example: In the following code fragment, two sets of parameters are batched. A
SELECT statement that takes one input parameter is then executed twice, once
with each parameter value. The numbers to the right of selected statements
correspond to the previously described steps.
34
Application Programming Guide and Reference for Java
"SELECT EMPNO, PHONENO FROM EMPLOYEE WHERE EMPNO=?");
empnum = rs.getString(1);
phonenum = rs.getString(2);
System.out.println("Employee number = " + empnum +
"Phone number = " + phonenum);
Related concepts
"JDBC interfaces for executing SQL" on page 24
Related tasks
"Retrieving multiple result sets from a stored procedure in a JDBC application"
on page 46
Related reference
"Driver support for JDBC APIs" on page 252
the SQL statement with input parameters.
a. Execute PreparedStatement.setXXX methods to assign values to the input
parameters.
b. Invoke the PreparedStatement.addBatch method to add the set of input
parameters to the batch.
invoke the DB2PreparedStatement.executeBatch method to execute the
statement with all sets of parameters.
PreparedStatement object.
a. For each ResultSet object, retrieve all rows.
// Create a PreparedStatement object
// Assign value to input parameter
// Get the result table from the query 3
// Position the cursor
// Retrieve the first column value
// Retrieve the first column value
// Print the column values
// Close the ResultSet
// Close the PreparedStatement
1
2
4
5
6

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents