Sie sind auf Seite 1von 10

Callable Statement

OADBTransactionImpl oadbtransactionimpl1 =
(OADBTransactionImpl)oaapplicationmodule.getOADBTransaction();
String s1 = "begin xxww_oaf_util_pkg.check_loc_for_iproc (:1,:2); end;";
OracleCallableStatement oraclecallablestatement =
(OracleCallableStatement)oadbtransactionimpl1.createCallableStatement(s1, 1);
String s3;
try
{
oraclecallablestatement.registerOutParameter(2, 12, 0);
oraclecallablestatement.setString(1, s);
oraclecallablestatement.execute();
s3 = oraclecallablestatement.getString(2);
oraclecallablestatement.close();
}
catch(SQLException sqlexception)
{
throw OAException.wrapperException(sqlexception);
}

Callable Statement Cont..


According to Oracle Standards whenever it is possible we should use View Objects for Database
operation but in some situations we have to call PL/SQL Procedures and Functions using JDBC.
In general, to invoke a stored procedure/Function from within an entity object or an application
module, you need to:
1. Create a JDBC CallableStatement with the PL/SQL block containing the stored procedure
invocation
2. Register OUT Parameter if any
3. Bind any variables.
4. Execute the statement.
5. Retrieve the values of any OUT parameters if any
6. Close the statement

Callable Statement Cont..


Create Callable Statement:
To create callable statement we should import java.sql.CallableStatement package
If we are using PL/SQL Procedure, then String that we will pass to callable statement will be in
following form:
BEGIN Package_name.procedure_name(:1,:2) END; where :1 and :2 are Parameters which may be
either IN or OUT Parameters.
Ex:
String sqlString = "begin test_package.prc_Test(:1, :2);end;"
If we are using PL/SQL Function then it should be in following form:
BEGIN :1 := Package_name.function_name(:2) END; where :1 will be the contain the results
returned by database function and will be treated as OUT parameter and :2 is IN Parameter.
Example:
Ex:
BEGIN :1= Test_Package.func_test(:2) END;
where :1 will be considered as OUT parameter and needs to be registered while :2 is IN Parameter.

Callable Statement Cont..


Following piece of code creates a callable atement:
OADBTransaction txn = getDBTransaction();
String sqlString = "begin dbms_application_info.set_module(:1, :2);end;"
CallableStatement cs = txn.createCallableStatement(sqlString);
Register OUT Parameters:
Before registering OUT parameters we need to import import java.sql.Types; package
And after that we need to use following statement to register OUT parameter:
((OracleCallableStatement)cs.registerOutParameter(1, Types.VARCHAR, 0, 2000);
//Types.NUMERIC
Types.VARCHAR can be replaced by other datatypes as per your requirement
Bind IN Parameters:
To bind in parameters we can use following statement: cs.setString(1, value); where setString can be
replaced by SetNumber/SetDate as per your requirement.

Callable Statement Cont..


Execute Callable Statement:
To execute following statement: we can use execute method of callable statement.
Retriving OUT Parameter Value:
we can get the value of OUT parameter using getString/getNumber/getDate methods of callable
statement.
Example:
messageBuffer = cs.getString(1);
Close Statement:
To close the statement we can use close()method of callable statement

Callable Statement Cont..


Example 1.
Callable Statement for Procedure with IN and OUT parameter.
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.sql.Types;
...
String outParamValue;
OADBTransaction txn = getDBTransaction();
CallableStatement cs =
txn.createCallableStatement("begin Test_Package.test_prc(:1, :2);
end;");
try
{
((OracleCallableStatement)cs.registerOutParameter(2, Types.VARCHAR, 0, 2000);
cs.setString(1, Sanaari);
cs.execute();
outParamValue = cs.getString(1);

Callable Statement Cont..

cs.close();
}
catch (SQLException sqle)
{
cs.close();
}

Callable Statement Cont..


Example2: Calling PL/SQL Function
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.sql.Types;
...
String outParamValue;
OADBTransaction txn = getDBTransaction();
CallableStatement cs =
txn.createCallableStatement("begin :1 := Test_Package.test_func(:2);
end;");
try
{
((OracleCallableStatement)cs.registerOutParameter(2, Types.VARCHAR, 0, 2000);
cs.setString(1,Sanaari);
cs.execute();
outParamValue = cs.getString(1);

Callable Statement Cont..

cs.close();
}
catch (SQLException sqle)
{
cs.close();
}

Callable Statement Cont..


oapagecontext.writeDiagnostics(this,"Value of DeliverToLocation is
"+s,OAFwkConstants.STATEMENT);
import oracle.apps.fnd.framework.OAFwkConstants;

Das könnte Ihnen auch gefallen