Question

For single output types my code is working fine but I couldn't get how to execute Oracle Procedure which has multiple output parameters and what will be the Output i.e ResultSet, Number etc.

 abcProcedure(param1 OUT NUMBER,param2 OUT NUMBER,param3 OUT NUMBER,param4 OUT NUMBER,param5 IN OUT NUMBER, param6 IN NUMBER,param7 IN NUMBER)

Hibernate Code:

 session.doWork(new Work(){

public void execute(Connection connection) throws SQLException {

CallableStatement callableStatement=connection.prepareCall("{call abcProcedure(?,?,?,?,?,?,?)}");


callableStatement.registerOutParameter(registerOutParameter,types.Numeric);

.......
callableStatement.execute();



}
}

Does anyone know how to do that?

Was it helpful?

Solution

Finally I have Solved it myself. Here is the solution

callableStatement.registerOutParameter(1,types.Numeric); //1   index of Out parameter
callableStatement.registerOutParameter(2,types.Numeric); //2   index of Out parameter
callableStatement.registerOutParameter(3,types.Numeric); //3   index of Out parameter
callableStatement.registerOutParameter(2,types.Numeric); //4   index of Out parameter
callableStatement.registerOutParameter(5,types.Numeric); //5   index of Out parameter
callableStatement.setLong(5, (Long)value); // 5 is also  input parameter
callableStatement.setLong(6, (Long)value); // 6    input parameter
callableStatement.setLong(7, (Long)value); // 7    input parameter
callableStatement.execute();
callableStatement.getLong(1);// out put for param1 
callableStatement.getLong(2);// out put for param2
callableStatement.getLong(3);// out put for param3
callableStatement.getLong(4);// out put for param4
callableStatement.getLong(5);// out put for param5

In case of multiple resultsets

callableStatement.getResultSet(); // will get the first ResultSet
callableStatement.getMoreResults();// will point to the next ResultSet
callableStatement.getResultSet();// will get the second ResultSet
..... So on

Thats it. :-)

OTHER TIPS

Using the above comments I was able to solve the issue related to my project. I was able to invoke stored procedures using the session factory. The code is as follows.

    public double getQuant(String Cd, String Num) {
          double vTotRet = 0;
          double vTotCst = 0;
          double onOrdQty = 0;
                 Session sess = sessionFactory.openSession();
                 Connection conn = sess.connection();
                 CallableStatement callableStatement = conn.prepareCall("{call MyProcedure(?,?,?,?,?)}");
                 //Setting the first two parameters
                 callableStatement.setString(1,Num);
                 callableStatement.setString(2,Cd);
                 //Initializing the out parameters
                 callableStatement.registerOutParameter(3, Types.DOUBLE);                   
                 callableStatement.registerOutParameter(4, Types.DOUBLE);
                 callableStatement.registerOutParameter(5, Types.DOUBLE);
                 //Execute the stored procedure
                 callableStatement.execute();
                 vTotRet = callableStatement.getDouble(3);
                 vTotCst = callableStatement.getDouble(4);
                 onOrdQty = callableStatement.getDouble(5);
                 logger.info(onOrdQty);
                 return onOrdQty;
          }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top