Thursday, September 20, 2012

Calling Database Package/Procedure in ADF

Define execute procedure Operation in Application Module Impl:   

import java.sql.Types;
import oracle.jdbc.OracleCallableStatement;

public String executeProc(String value1,String value2,String value3) {
       
       String output=null;
       Connection conn;
             
        try {
            conn = getDBTransaction().createStatement(1).getConnection();
                
            String executeProcStatement=
                "begin SAMPLE_PKG.SAMPLE_PROC(:inputParam1," +
                ":inputParam2,"+
                ":inputParam3," +
                ":outputParam1," +
                ":outputParam2);
                end;";              
         
           CallableStatement st = null;
            st =
            getDBTransaction().createCallableStatement(executeProcStatement, this.getDBTransaction().DEFAULT);
            try{
                st =
                (OracleCallableStatement)getDBTransaction().createCallableStatement(executeProcStatement,0);
               
                st.setObject("inputParam1", value1);
                st.setObject("inputParam2", value2);
                st.setObject("inputParam3", value3);

                st.registerOutParameter("outputParam1", Types.VARCHAR);
                st.registerOutParameter("outputParam2", Types.VARCHAR);
               
               st.execute();
                output=(String)st.getObject("outputParam1");
                System.out.println(st.getObject("outputParam1"));
            }catch (SQLException e) {
                     .....................................................
            } finally {
                if (st != null) {
                    try {
                        st.close();
                    } catch (SQLException e) {
                     .....................................................
                    }
                }
            }
        }
        catch (SQLException e) {
                    System.out.println(e);
                }
       
        return output;
    }
   
Define Method in backing or managed bean and specify it in ADF faces components action listener property:

    public void executeProcListener(ActionEvent actionEvent) {
   
    OperationBinding executeProc = ADFUtils.findOperation("executeProc");
    Map params=executeProc.getParamsMap();
        params.put("inputParam1", value1);
        params.put("inputParam2", value2);
        params.put("inputParam3", value3);
        String output=(String)executeProc.execute();
       
        FacesContext fc=FacesContext.getCurrentInstance();
       
        if(output!=null) {
                    .................................................
        }
        else
        {
                    .................................................
        }
        log.severe("Output "+output);
    }

1 comment:

  1. What about the "outputParam2" how do you return both params?

    ReplyDelete

Provide your thoughts !