Tuesday, 31 March 2015

OAF: Sample code to call PL/SQL Procedure from OA Framework

Please find below sample code to call PL/SQL procedure from OA Framework page.

In the below example first 4 parameters are IN parameters and the last 3 parameters are OUT parameters.This example code is a function defined in the ApplicationModule.

 import oracle.jdbc.OracleCallableStatement;   
 import oracle.jdbc.OracleTypes;
 import oracle.jbo.domain.Number;
 import oracle.jbo.domain.Date;
    
 public void callPLSQLProc(Number id,String code,Date currentDate)   
 {   
      OracleCallableStatement callableStatement = null;   
      try   
      {   
           String callProc = " BEGIN xxaj_pkg.xxaj_proc "+   
                                         "(p_id             => :1," +   
                                         " p_code           => :2," +
                                         " p_date           => :3," +
                                         " p_commit         => :4," +                                                  
                                         " x_return_status  => :5," +   
                                         " x_msg_data       => :6," +   
                                         " x_msg_count      => :7);"+   
                                    " END; ";   
           callableStatement = (OracleCallableStatement)getOADBTransaction().createCallableStatement(callProc,1);   
           callableStatement.setNUMBER(1, id);   
           callableStatement.setString(2, code);           
           callableStatement.setDATE(3, currentDate);     
           callableStatement.setString(4, "Y");      
           callableStatement.registerOutParameter(5,OracleTypes.VARCHAR,255);   
           callableStatement.registerOutParameter(6,OracleTypes.VARCHAR,255);   
           callableStatement.registerOutParameter(7,OracleTypes.NUMBER,255);     
           callableStatement.execute();   
           String resultMessage = (String)callableStatement.getString(5);   
           String msgData  = (String)callableStatement.getString(6);   
      }   
      catch(Exception e)   
      {   
           e.printStackTrace();   
           throw new OAException(e.toString(),OAException.ERROR);   
      }   
      finally   
      {   
           try   
           {   
                callableStatement.close();   
           }   
           catch(Exception exception2)   
           {   
                throw OAException.wrapperException(exception2);   
           }   
      }   
 }         

Package Spec is as below:
 create or replace package xxaj_pkg as  
 procedure xxaj_proc (p_id            in number  
                     ,p_code          in varchar2  
                     ,p_date          in date  
                     ,p_commit        in varchar2  
                     ,x_return_status out varchar2  
                     ,x_msg_data      out varchar2  
                     ,x_msg_count     out number);  
 end xxaj_pkg;  


Feel free to point out if anything is missing/wrong in this blog.

6 comments:

  1. This is very helpful demonstration of how to use OracleCallableStatement in OAF. You have covered the three major variable types - Number, Date and Varchar2. Can you also show how to setNull for these 3 kinds of variables? It will be useful to avoid Null Pointer Exceptions

    ReplyDelete
    Replies
    1. Sorry, didnt understand your question. Can you please give an example ?

      Delete
  2. Hi Johny, Let me say this first - your OAF Blogs are the best. They are like Gold standard for Oracle OAF info.

    For OracleCallableStatement in OAF let's say I have to pass 5 input parameters. Before passing them to my pl/sql proc, I want to check each input parameter to make sure they have a value in it. This is to avoid the dreaded nullpointer exception. If I find any parameter doesn't have value, how do I handle that?

    ReplyDelete
    Replies
    1. Sorry, I am not very clear with your requirement. What do you want to do if any of the parameters are null ?

      Delete
  3. Hi
    i am getting Nullpointer exception while calling the procedure. is there any specific reason for that

    With Regards,
    Suneetha L

    ReplyDelete
    Replies
    1. You may not have initialized some variables. If you can attach the code, I can comment.

      Delete