Callable Statement:
We can use this for executing procedures in OAF application
CallableStatment is a java class provided by java for executing procedures
Example Code:
OADBTransaction oadbtransaction=[Link]();
OracleCallableStatement oraclecallablestatement ;
String sql = "BEGIN proc_name (:1, :2); END;";
oraclecallablestatement = (OracleCallableStatement)
[Link](sql, -1);
try {
[Link](1, asg_id);
[Link](2,[Link],0);
[Link]();
outvar = [Link](2);
[Link]();
}
catch(SQLException exception){
outvar = "N";
}
Step1: Create a simple page with one text box and one button, set a new controller on the page
Empno:
Id: empno, DataType: NUMBER, Max Lenght: 4, Prompt: Enter Emp Num
Go Button:
Id: gobtn, prompt: GO
Step2: Write the procedure as per requirement.
create or replace procedure xxtestproc(p_emp_no in number,p_emp_sal out number)
is
v_sal number(10);
begin
select sal into v_sal from emp where empno=p_emp_no;
p_emp_sal:=v_sal;
end;
Step3: Code in processFormRequest to handle go button and execute procedure.
I mports:
// TO handle Exceptions of SQLs
import [Link];
// For providing Transaction Context
import [Link];
//Callable Statement
import [Link];
//Data Type to work with callable statement
import [Link];
Code in PFR: after super line
// Getting value from the page and type casting to NUMBER
String empno=[Link]("empno");
String empsalary=null;
// Check the GO button click
if([Link]("gobtn")!=null)
{
OADBTransaction
oadbtransaction=[Link](webBean).getOADBTransaction();
// Creating object of OracleCallableStatement
OracleCallableStatement oraclecallablestatement;
// Build SQL statement
String sql = "BEGIN xxtestproc(:1, :2); END;";
// With the help of oadbtransaction, we are creating callable statement sql
oraclecallablestatement =
(OracleCallableStatement)[Link](sql, -1);
// Keep the execute() statement in try catch block
try {
// Set the in parameter
[Link](1, empno);
// Register the out parameter is number
[Link](2,[Link],0);
[Link]();
empsalary = [Link](2);
[Link]();
}
catch(SQLException exception){
empsalary=null;
}
throw new OAException ("Salary is :"+empsalary, [Link]);
}