Home > DeveloperSection > Beginner > Callable Statement in Java

Callable Statement in Java


Java Database  Java  J2EE  Database Design  Database Connection  Database Schema  Database Table 
Ratings:
0 Comment(s)
 731  View(s)
Rate this:



Using Cursor in CallableStatement




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 .




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:




Don't want to miss updates? Please click the below button!

Follow MindStick