Callable Statements in Java With Example. This Java Callable Statements example describes the basic operations performed on the JDBC Callable Statements database connectivity code.In this example you will learn about how to write java code for Callable Statements with oracle database.
Program:
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
public class CallableStmtDemo {
public static void main(String args[]){
Connection con=null;
CallableStatement cst = null;
try {
Class.forName(“oracle.jdbc.driver.OracleDriver”);
con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:ORCL”,”scott”,”tiger”);
cst = con.prepareCall(“{?=call addfun(?,?)}”);
cst.setInt(2,10);
cst.setInt(3,20);
cst.registerOutParameter(1, Types.INTEGER);
cst.execute();
int result = cst.getInt(1);
System.out.print(“add: “+result);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Stored procedure in oracle:
1 create or replace function addfun(a number,b number)
2 return number is c number(6);
3 begin
4 c:=a+b;
5 return c;
6 end;