Executing stored procedures v42.7.3.1
A stored procedure is a module that's written in EDB’s SPL and stored in the database. A stored procedure can define input parameters to supply data to the procedure and output parameters to return data from the procedure. Stored procedures execute in the server and consist of database access commands (SQL), control statements, and data structures that manipulate the data obtained from the database.
Stored procedures are especially useful when extensive data manipulation is required before storing data from the client. It's also efficient to use a stored procedure to manipulate data in a batch program.
Invoking stored procedures
The CallableStatement
class provides a way for a Java program to call stored procedures. A CallableStatement
object can have a variable number of parameters used for input (IN
parameters), output (OUT
parameters), or both (IN OUT
parameters).
The syntax for invoking a stored procedure in JDBC is shown below. The square brackets indicate optional parameters. They aren't part of the command syntax.
The syntax to invoke a procedure that returns a result parameter is:
Each question mark serves as a placeholder for a parameter. The stored procedure determines if the placeholders represent IN
, OUT
, or IN OUT
parameters and the Java code must match.
Executing a simple stored procedure
The following shows a stored procedure that increases the salary of each employee by 10%. increaseSalary
expects no arguments from the caller and doesn't return any information:
The following shows how to invoke the increaseSalary
procedure:
To invoke a stored procedure from a Java application, use a CallableStatement
object. The CallableStatement
class is derived from the Statement
class and, like the Statement
class, you obtain a CallableStatement
object by asking a Connection
object to create one for you. To create a CallableStatement
from a Connection
, use the prepareCall()
method:
As the name implies, the prepareCall()
method prepares the statement but doesn't execute it. As Executing stored procedures with IN parameters shows, an application typically binds parameter values between the call to prepareCall()
and the call to execute()
. To invoke the stored procedure on the server, call the execute()
method.
This stored procedure (increaseSalary
) didn't expect any IN
parameters and didn't return any information to the caller (using OUT
parameters), so invoking the procedure is a matter of creating a CallableStatement
object and then calling that object’s execute()
method.
Executing stored procedures with IN parameters
The code in the next example first creates and then invokes a stored procedure named empInsert
. empInsert
requires IN
parameters that contain employee information: empno
, ename
, job
, sal
, comm
, deptno
, and mgr
. empInsert
then inserts that information into the emp
table.
The following creates the stored procedure in the EDB Postgres Advanced Server database:
The following shows how to invoke the stored procedure from Java:
Each placeholder (?) in the command (commandText
) represents a point in the command that's later replaced with data:
The setObject()
method binds a value to an IN
or IN
OUT
placeholder. Each call to setObject()
specifies a parameter number and a value to bind to that parameter:
After supplying a value for each placeholder, this method executes the statement by calling the execute()
method.
Executing stored procedures with OUT parameters
The next example creates and invokes an SPL stored procedure called deptSelect
. This procedure requires one IN
parameter (department number) and returns two OUT
parameters (the department name and location) corresponding to the department number:
The following shows the Java code required to invoke the deptSelect
stored procedure:
Each placeholder (?) in the command (commandText
) represents a point in the command that's later replaced with data:
The setObject()
method binds a value to an IN
or IN OUT
placeholder. When calling setObject()
, you must identify a placeholder (by its ordinal number) and provide a value to substitute in place of that placeholder:
Register the JDBC type of each OUT
parameter before executing the CallableStatement
objects. Registering the JDBC type is done with the registerOutParameter()
method.
After executing the statement, the CallableStatement
getter method retrieves the OUT
parameter values. To retrieve a VARCHAR
value, use the getString()
getter method.
In this example, GetDeptInfo()
registers two OUT
parameters and (after executing the stored procedure) retrieves the values returned in the OUT
parameters. Since both OUT
parameters are defined as VARCHAR
values, GetDeptInfo()
uses the getString()
method to retrieve the OUT
parameters.
Executing stored procedures with IN OUT parameters
The code in the next example creates and invokes a stored procedure named empQuery
defined with one IN
parameter (p_deptno
), two IN OUT
parameters (p_empno
and p_ename
) and three OUT
parameters (p_job
,p_hiredate
and p_sal
). empQuery
then returns information about the employee in the two IN OUT
parameters and three OUT
parameters.
This code creates a stored procedure named empQuery
:
The following code shows invoking the empQuery
procedure, providing values for the IN
parameters, and handling the OUT
and IN OUT
parameters:
Each placeholder (?) in the command (commandText
) represents a point in the command that's later replaced with data:
The setInt()
method is a type-specific setter
method that binds an Integer
value to an IN
or IN OUT
placeholder. The call to setInt()
specifies a parameter number and provides a value to substitute in place of that placeholder:
The setString()
method binds a String
value to an IN
or IN OUT
placeholder:
Before executing the CallableStatement
, you must register the JDBC type of each OUT
parameter by calling the registerOutParameter()
method.
Before calling a procedure with an IN
parameter, you must assign a value to that parameter with a setter method. Before calling a procedure with an OUT
parameter, you register the type of that parameter. Then you can retrieve the value returned by calling a getter method. When calling a procedure that defines an IN OUT
parameter, you must perform all three actions:
- Assign a value to the parameter.
- Register the type of the parameter.
- Retrieve the value returned with a getter method.