0

I have been given PL SQL stored Function with the following signature and body description.

Create or Replace function Get_Employee(user_id in Number, res_out out sys_refcursor)
return  sys_refcursor is
v_result sys_refcursor;
begin
//here goes function body
return v_result
end;

Basically, I need to call this function from Java. I have tried all possible ways to register out parameter as well as return value which both are refcursor type. Here is the way which I believe is the most correct way.

    String employeeSQL = "{ ? = call Get_Employee(?,?)}";
            CallableStatement statement = null;
            Employee employee = null;
            ResultSet result = null;
    try{
    statement = conn.prepareCall(employeeSQL);
    statement.registerOutParameter(1, OracleTypes.Cursor); //registering return value
    statement.registerOutParamter(2, OracleTypes.Cursor); // registering out parameter
    statement .setLong(3, userId); // setting input parameter
    statement.execute();
    resultSet = (ResultSet) statement.getObject(1); // getting return value
    }finally{
    statement.close();
    trsultSet.close();
    }

The problem is whenever the statement is executed, it throws NullPointer Exception. So I suspect I do not register parameters in a correct way. Any help appreciated!

devgirl
  • 59
  • 2
  • 8
  • Welcome to Stack Overflow! Please take the [tour](/tour), have a look around, and read through the [help center](/help), in particular [How do I ask a good question?](/help/how-to-ask) and [What topics can I ask about here?](/help/on-topic). Please provide the *complete* StackTrace or compiler errorr message you get. – Timothy Truckle Feb 05 '18 at 08:42
  • Thanks for the comment, but it seems like, your answer has nothing to do with my problem @TimothyTruckle . – devgirl Feb 05 '18 at 08:44
  • 1
    well: you state you get an NPE and you did not provide the stacktrace, so I think my comments very well relate to your question. – Timothy Truckle Feb 05 '18 at 08:46
  • If I understood you correctly, you suggested statement.setLong(1,userId); statement.registerOutParameter(2, OracleTypes.Cursor); statement.registerOutParamter(3, OracleTypes.Cursor); . If so, I have already tried it, the same error happens. If not pls correct me. @STaefi – devgirl Feb 05 '18 at 08:48
  • @devgirl: `statement.setLong(2,userId); statement.registerOutParameter(1, OracleTypes.Cursor); statement.registerOutParamter(3, OracleTypes.Cursor);` – STaefi Feb 05 '18 at 08:56
  • @STaefi Lots of thanks!!!! It definitely resolved my problem. You might want to post it as an answer. – devgirl Feb 05 '18 at 11:42

0 Answers0