Users Pricing

articles

home / developersection / articles / callable statement in java

Callable Statement in Java

Anupam Mishra 4887 06 Nov 2015 Updated 07 Sep 2019
The Broad –level steps to use CallableStatement in an application are as follow:

1. Creating the CallableStatement object.

2. Setting the values of parameters.

3.  Registering the OUT parameter type.

4. Executing the stored procedure or function.

5. Retrieving the parameter values.

For Example: We have created two table (‘bank’  and ‘personal_details) in Oracle Database.Now, We want to create the procedure createAccount that can insert a records into both the tables .


Callable Statement in Java


Now, we  used this procedure in java program with the help of JDBC . We create a java file and write code are as follow:

import java.sql.*;

/**
* @author Anupam Mishra
*/
public class CallableStatementEx{
public static void main(String args[]) throws Exception{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Connection con=
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
//Step 1: get CallableStatement
CallableStatement cs= con.prepareCall("{call
createaccount(?,?,?,?,?,?)}");
//Step 2:set IN Parameters
cs.setInt(1,1001);
cs.setInt(2,10);
cs.setString(3,"Anupam");
cs.setDouble(4,10000);
cs.setString(5,"Allahabad");
cs.setInt(6,51663);
//Step 3: register OUT parameters
// In this program, we don't have OUT parameters
//Step 4:
cs.execute();
System.out.println("Account Created
Succesfully!..");
con.close();
}
}

The Output is shown Below:

Callable Statement in Java