Java Reference
In-Depth Information
try (CallableStatement cstmt = con.prepareCall("{ call
FIRE(?)}"))
{
cstmt.setInt(1, 2);
cstmt.execute();
}
The
cstmt.setInt(1, 2)
methodcallassigns2totheleftmoststoredprocedure
parameter—parameter index
1
corresponds to the leftmost parameter (or to a single
parameterwhenthere'sonlyone).The
cstmt.execute()
methodcallexecutesthe
stored procedure, which results in a callback to the application's
public static
void fire(int id)
method.
I've created another version of the
EmployeeDB
application that demonstrates this
callable statement.
Listing 9-18
presents its source code.
Listing 9-18.
Firing an employee via a stored procedure
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public
class EmployeeDB
{
public static void main(String[] args)
{
String url = "jdbc:derby:employee;create=true";
try
(Connection
con
=
DriverMan-
ager.getConnection(url))
{
try (Statement stmt = con.createStatement())
{
String sql = "CREATE PROCEDURE FIRE(IN ID
INTEGER)"+
"
PARAMETER STYLE JAVA"+