Program related to callable statement using Oracle
//Oracle Procedure using in/out parameters- create under scott user
Create or replace procedure proc4( sreg in number, sn out varchar2)
is
begin
select sname into sn from student where regno = sreg;
end;
//Java Code
import java.sql.*;
import java.util.Scanner;
public class CallableOut {
public static void main(String[] args) {
try{
Class.forName("oracle.jdbc.OracleDriver"); // Programs work without this line also.
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","scott","tiger");
// thin drivers are used when you have database on local system and oci drivers are used when you have database on remote system.
CallableStatement cst = conn.prepareCall("{call proc4(?,?)}");
Scanner input = new Scanner(System.in);
System.out.print("Enter regno :: ");
int rno = input.nextInt();
cst.setInt(1, rno);
cst.registerOutParameter(2, Types.VARCHAR);
cst.execute();
System.out.println("The name of student is "+cst.getString(2));
conn.close();
}catch(ClassNotFoundException | SQLException ex){ex.printStackTrace();}
}
}
Execution:
Enter regno :: 4 (return)
The name of student is Abcd
//Oracle Procedure using in/out parameters- create under scott user
Create or replace procedure proc4( sreg in number, sn out varchar2)
is
begin
select sname into sn from student where regno = sreg;
end;
//Java Code
import java.sql.*;
import java.util.Scanner;
public class CallableOut {
public static void main(String[] args) {
try{
Class.forName("oracle.jdbc.OracleDriver"); // Programs work without this line also.
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","scott","tiger");
// thin drivers are used when you have database on local system and oci drivers are used when you have database on remote system.
CallableStatement cst = conn.prepareCall("{call proc4(?,?)}");
Scanner input = new Scanner(System.in);
System.out.print("Enter regno :: ");
int rno = input.nextInt();
cst.setInt(1, rno);
cst.registerOutParameter(2, Types.VARCHAR);
cst.execute();
System.out.println("The name of student is "+cst.getString(2));
conn.close();
}catch(ClassNotFoundException | SQLException ex){ex.printStackTrace();}
}
}
Execution:
Enter regno :: 4 (return)
The name of student is Abcd