Use a CallableStatement object to run SQL stored procedures. The stored procedure being called must already be stored in the database. CallableStatement does not contain the stored procedure, it only calls the stored procedure.
A stored procedure can return one or more ResultSet objects and can use IN parameters, OUT parameters, and INOUT parameters. Use Connection.prepareCall() to create new CallableStatement objects.
The CallableStatement object allows you to submit multiple SQL commands as a single group to a database through the use of batch support. You may get better performance by using batch support because processing a group of operations is usually faster than processing them one at a time. For more information about using batch support, see Enhancements to JDBC support.
CallableStatement allows you to get and set parameters and columns by name, although using the column index results in better performance.
The following example shows how to use the CallableStatement interface.
// Connect to the server. Connection c = DriverManager.getConnection("jdbc:as400://mySystem"); // Create the CallableStatement // object. It precompiles the // specified call to a stored // procedure. The question marks // indicate where input parameters // must be set and where output // parameters can be retrieved. // The first two parameters are // input parameters, and the third // parameter is an output parameter. CallableStatement cs = c.prepareCall("CALL MYLIBRARY.ADD (?, ?, ?)"); // Set input parameters. cs.setInt (1, 123); cs.setInt (2, 234); // Register the type of the output // parameter. cs.registerOutParameter (3, Types.INTEGER); // Run the stored procedure. cs.execute (); // Get the value of the output // parameter. int sum = cs.getInt (3); // Close the CallableStatement and // the Connection. cs.close(); c.close();