IBM DB2 Manual page 49

Table of Contents

Advertisement

"JDBC interfaces for executing SQL" on page 24
Related tasks
"Learning about a ResultSet using ResultSetMetaData methods" on page 35
"Specifying updatability, scrollability, and holdability for ResultSets in JDBC
applications" on page 38
"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
Retrieving data from tables using the
PreparedStatement.executeQuery method
To retrieve data from a table using a SELECT statement with parameter markers,
you use the PreparedStatement.executeQuery method.
This method returns a result table in a ResultSet object. After you obtain the result
table, you need to use ResultSet methods to move through the result table and
obtain the individual column values from each row.
With the IBM Data Server Driver for JDBC and SQLJ, you can also use the
PreparedStatement.executeQuery method to retrieve a result set from a stored
procedure call, if that stored procedure returns only one result set and has only
input parameters. If the stored procedure returns multiple result sets, you need to
use the Statement.execute method. See "Retrieve multiple result sets from a stored
procedure in a JDBC application" for more information.
You can also use the PreparedStatement.executeQuery method for statements that
have no parameter markers. When you execute a query many times, you can get
better performance by creating the SQL statement as a PreparedStatement.
To retrieve rows from a table using a SELECT statement with parameter markers,
you need to perform these steps:
1. Invoke the Connection.prepareStatement method to create a PreparedStatement
object.
2. Invoke PreparedStatement.setXXX methods to pass values to the input
parameters.
3. Invoke the PreparedStatement.executeQuery method to obtain the result table
from the SELECT statement in a ResultSet object.
4. In a loop, position the cursor using the ResultSet.next method, and retrieve
data from each column of the current row of the ResultSet object using getXXX
methods.
5. Invoke the ResultSet.close method to close the ResultSet object.
6. Invoke the PreparedStatement.close method to close the PreparedStatement
object when you have finished using that object.
The following code demonstrates how to retrieve rows from the employee table for
a specific employee. The numbers to the right of selected statements correspond to
the previously-described steps.
Chapter 3. JDBC application programming
33

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents