You can 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.
You can use a batch update facility to associate a single CallableStatement object with multiple sets of input parameter values. This unit then can be sent to the database for processing as a single entity. You may get better performance with batch updates because it is usually faster to process a group of update operations than one update operation at a time. If you want to use the batch update facility, you need JDBC 2.0 and JDK 1.2.
The following example shows how to use the CallableStatement interface.
// Connect to the AS/400. 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();