Java Reference
In-Depth Information
msg :=text;
end;
The CallableStatement in the following code executes this stored procedure
that is contained within the database, passing the necessary parameters. The results of
the OUT parameter are then displayed back to the user.
try(CallableStatement cs = conn.prepareCall("{call
DUMMY_PROC(?,?)}");) {
cs.setString(1, "This is a test");
cs.registerOutParameter(2, Types.VARCHAR);
cs.executeQuery();
System.out.println(cs.getString(2));
} catch (SQLException ex){
ex.printStackTrace();
}
Running the example class for this recipe will display the following output, which
is the same as the input. This is because the DUMMY_PROC procedure simply assigns
the contents if the IN parameter to the OUT parameter.
Successfully connected
This is a test
How It Works
It is not uncommon for an application to use database-stored procedures for logic that
can be executed directly within the database. In order to call a database-stored proced-
ure from Java, you must create a CallableStatement object, rather than use a
PreparedStatement . In the solution to this recipe, a CallableStatement in-
vokes a stored procedure named DUMMY_PROC . The syntax for instantiating the
CallableStatement is similar to that of using a PreparedStatement . Use
the Connection object's prepareCall() method, passing the call to the stored
procedure. The stored procedure call must be enclosed in curly braces {} or the applic-
ation will throw an exception.
Search WWH ::




Custom Search