Java Reference
In-Depth Information
cs = conn.prepareCall("{call DUMMY_PROC(?,?)}");
Once the
CallableStatement
has been instantiated, it can be used just like a
PreparedStatement
for setting the values of parameters. However, if a parameter
is registered within the database-stored procedure as an
OUT
parameter, you must call a
special method,
registerOutParameter()
, passing the parameter position and
database type of the
OUT
parameter that you want to register. In the solution to this re-
cipe, the
OUT
parameter is in the second position and it has a
VARCHAR
type.
cs.registerOutParameter(2, Types.VARCHAR);
To execute the stored procedure, call the
executeQuery()
method on the
CallableStatement
. Once this has been done, you can see the value of the
OUT
parameter by making a call to the
CallableStatement getXXX()
method that
corresponds to the data type:
System.out.println(cs.getString(2));
A NOTE REGARDING STORED FUNCTIONS
Calling a stored database function is essentially the same as calling a stored
procedure. However, the syntax to
prepareCall()
is slightly modified.
To call a stored function, change the call within the curly braces to entail a
returned value using a
?
character. For instance, suppose that a function
named
DUMMY_FUNC
accepted one parameter and returned a value. The fol-
lowing code would be used to make the call and return the value:
cs = conn.prepareCall("{? = call DUMMY_FUNC(?)}");
cs.registerOutParameter(1, Types.VARCHAR);
cs.setString(2, "This is a test");
cs.execute();
A call to
cs.getString(1)
would then retrieve the returned value.
13-15. Obtaining Dates for Database Use