Java Reference
In-Depth Information
As with prepared statements, the parameters need to be set before the
callable procedure can be invoked. If the procedure does not have any para-
meters, it is invoked using the statement:
{call procedure_name }
For example, the following statements create a CallableStatement object for
a procedure named UpdateScores that has one parameter:
CallableStatement c = connection.prepareCall(“{call UpdateScores(?)}”)
Before the statement can be executed, the parameter is set using one of the
set methods in the CallableStatement interface, which are similar to the set
methods of the PreparedStatement interface. For example, the following state-
ment sets the first parameter of a CallableStatement to an int value of 27:
c.setInt(1, 27);
To execute a CallableStatement, invoke one of the execute methods inherited
from PreparedStatement, which is the parent interface of CallableStatement.
For example:
c.execute();
To demonstrate invoking a stored procedure, I added a showByCategory()
method to the MovieDatabase class that uses a CallableStatement to invoke a
stored procedure named SelectByCategory. (Read the sidebar Stored Procedures
in Microsoft Access to see how this stored procedure was created.) Study the
showByCategory() method to determine what it does.
import java.sql.*;
public class MovieDatabase
{
private Connection connection;
private PreparedStatement findByNumber, updateCategory;
private CallableStatement findByCategory;
public MovieDatabase(Connection connection) throws SQLException
{
this.connection = connection;
findByNumber = connection.prepareStatement(
“SELECT * FROM Movies WHERE number = ?”);
Search WWH ::




Custom Search