0

I'm trying to run a stored procedure that returns a resultSet using oracle jdbc. The procedure is as follows.

create or replace procedure display_players (rset OUT sys_refcursor)
as
Begin
open rset for select * from player_data;
End;
/

The java code is as follows

try {
sql = "{call display_players()}";
call = conn.prepareCall(sql);
call.execute();
rs = call.getResultSet();

while(rs.next()){
    System.out.println(rs.getString("name") + " : " + rs.getString("club"));
}

I tried to register the out parameter as

call = conn.prepareCall("{call display_players(?)}");
call.registerOutParameter(1, OracleTypes.CURSOR);

But that dint work nor is the current code working as i get a null pointer exception which means the result set is not being returned. how do i achieve this?

codepoetly
  • 17
  • 1
  • 6
  • can you post error code. – santosh gore Mar 24 '17 at 04:04
  • Exception in thread "main" java.lang.NullPointerException at tester.main(tester.java:49) – codepoetly Mar 24 '17 at 07:31
  • please post your whole error code. – santosh gore Mar 24 '17 at 07:32
  • That's the error code with oracleTypes.cursor – codepoetly Mar 24 '17 at 07:33
  • java.sql.SQLException: ORA-06550: line 1, column 7: PLS-00306: wrong number or types of arguments in call to 'DISPLAY_PLAYERS' ORA-06550: line 1, column 7: PL/SQL: Statement ignored – codepoetly Mar 24 '17 at 07:36
  • at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447) at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396) at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:951) at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:513) at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:227) at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531) at oracle.jdbc.driver.T4CCallableStatement.doOall8(T4CCallableStatement.java:205) at oracle.jdbc.driver.T4CCallableStatement.executeForRows(T4CCallableStatement.java:1043) – codepoetly Mar 24 '17 at 07:37
  • at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1336) at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3613) at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3714) – codepoetly Mar 24 '17 at 07:38
  • at oracle.jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java:4755) at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:1378) at tester.main(tester.java:42) – codepoetly Mar 24 '17 at 07:38
  • That's the entire code for not registering the output parameter – codepoetly Mar 24 '17 at 07:39

1 Answers1

1

I think you haven't quite worked out how to get the result set from an OUT parameter from a stored procedure call.

Firstly, you need to register the OUT parameter, as in your second code sample:

call = conn.prepareCall("{call display_players(?)}");
call.registerOutParameter(1, OracleTypes.CURSOR);

However, once you've executed the statement, it's not correct to call.getResultSet() to get at the result set in the OUT parameter. For example, suppose you were calling a stored procedure that had two OUT parameters returning cursors. Which one should call.getResultSet() return?

The trick is to use call.getObject(...) to get the value of the parameter from call as an Object and then cast this to a ResultSet. In other words, replace the line

rs = call.getResultSet();

with

rs = (ResultSet)call.getObject(1);
Luke Woodward
  • 63,336
  • 16
  • 89
  • 104