Wednesday, 8 February 2017

Call procedure/function from OAF page

we can call function/procedure from oaf page using Callable statement or Prepared statement.
If the function/procedure returns result set , then we can use preparedStatement, otherwise we can go for CallableStatement.
Use createCallableStatement method in OADBTransaction class to call function/procedure

find the below code to call function which has one input parameter and returns one output parameter:
--------------------------------------------------------------------------------------------------------------------


OAApplicationModule am=pageContext.getApplicationModule(webBean); //create an object for ApplicationModule
pageContext -- is the Object for OAPageContext class
webBean -- is the object for OAWebbean class.

int Total=0; //declare return variable and initialize
 try
          {
                 CallableStatement cal = am.getOADBTransaction().createCallableStatement("begin select                   FUNCTION_NAME(:1) into :2 from dual; end;", 1);
                 cal.setInt(1, InputParameter);  //pass inputParameter
               cal.registerOutParameter(2, Types.VARCHAR2); //set Output parameter
               cal.execute();  //execute
               Total = cal.getInt(2); //return result of function to variable
           
             }
              catch (Exception e)
             {
                 throw OAException.wrapperException(e); // This will display the exception details
             }
finally
{
  cal.close(); //close callable statement
}

close callableStatement in finally block. It will close even if the callableStatement return an exception.

No comments:

Post a Comment