devxlogo

How to Create and Use CallableStatement Objects

How to Create and Use CallableStatement Objects

A CallableStatement object provides a standard way for all RDBMS’ to call stored procedures. A stored procedure is stored in a database; the call to the stored procedure is what a CallableStatement object contains. This call is written in an escape syntax that can take one of two forms: with a result parameter, and the other without one. A result parameter, a kind of OUT parameter, is the return value for the stored procedure. Both forms may have a variable number of parameters used for input (IN parameters), output (OUT parameters), or both (INOUT parameters). A question mark serves as a placeholder for a parameter.
Creating a CallableStatement object:

 CallableStatement callStmt = con.prepareCall(	"{call getData(?, ?)}");

The variable callStmt contains a call to the stored procedure getData, which has two input parameters and no result parameter. Whether the “?” placeholders are IN, OUT, or INOUT parameters depends on the stored procedure getTestData.
Suppose getTestData accepts two OUT parameters. Then you have to register the OUT parameter:

 cstmt.registerOutParameter(1, java.sql.Types.TINYINT);cstmt.registerOutParameter(2, java.sql.Types.DECIMAL, 3);

This is how to get it back:

 byte x = cstmt.getByte(1);

devx-admin

Share the Post: