Java stored procedures invoked from JDBC must be encapsulated in
CallableStatement
objects.
To invoke a Java stored procedure using JDBC:
private CallableStatement
checkIn;
Initialize the callable statement object by
calling prepareCall
on the connection with a SQL
CALL
statement for the stored procedure. For example:
checkIn = connection.prepareCall(quot;{call NPL.CHECKIN(?, ?, ?)}");
Note that the number of parameters in the stored procedure is represented by
the number place-holders in the SQL call.
registerOutParameter
for each output parameter, identifying it
by position, and declaring its type. For example, if the second
parameter is an SQL INTEGER
(which maps to a Java
int)
, and the third is a SQL VARCHAR
( which maps
to a Java String
), then:
newCustomer.registerOutParameter(2, Types.INTEGER);
newCustomer.registerOutParameter(3, Types.VARCHAR);
set
method, identifying the parameter by position, and assigning it a
value. For example, if the first parameter is an int
input parameter:checkIn.setInt(1, bookID);
Execute the callable statement object. For example:
checkIn.execute();
Extract the callable statement
object's output parameters. Call a get
method for each
output parameter, identifying the parameter by position. The get
methods return values of corresponding Java types. For example:
int daysLate = checkIn.getInt(2);
String title = checkIn.getString(3);
Invoking a Java Stored Procedure
Copyright © 1997, 2004, Oracle. All rights reserved.