0

I am executing a store procedure in Java. One of procedure returns multiple rows.

int haveResults = Param.callableStatement.executeUpdate();
System.out.println(haveResults);
if (haveResults > 0) {
    System.out.println("I have results"); //This statement is printed
    ResultSet rs = Param.callableStatement.getResultSet();
    while (rs.next()) { //This is where I am getting error:NullPointerException
        //If I comment 'while(rs.next())' then code is giving output but then it can't handle multiple rows
        itr1 = listOutParam.iterator();
        while (itr1.hasNext()) {
            Object obj = null;
            obj = ParameterGetter.getParameter(itr1.next().toString(), counter1);
            list.add(obj);
            counter1++;
        }
        System.out.println(list.toString());
        list1.add(list);
        counter1 = counter2;
    }
}
}

I tried using execute() but then if is returning false value indicating the procedure is not executing successfully.

Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
Abhishek
  • 111
  • 1
  • 3
  • 15
  • do `while(rs.hasNext()){`instead of `while(rs.next()){`. Just like you do in the second while loop – jhamon Apr 07 '16 at 07:15
  • 1
    ResultSet doesn't have hasNext method – Abhishek Apr 07 '16 at 07:25
  • As explained in the linked duplicate a `false` result from `execute` (or `getMoreResults`) does not mean that there are no results; it only means that the current result is an update count and not a result set. Instead of using the approach in the linked question, you could also consider adding `SET NOCOUNT ON` in the first line of your stored procedure. – Mark Rotteveel Apr 07 '16 at 08:08

1 Answers1

1

If you want to retrieve a ResultSet from a CallableStatement you need to use execute() instead of executeUpdate()

if (Param.callableStatement.execute()) {
     ResultSet rs = Param.callableStatement.getResultSet();
     ...
}
wero
  • 32,544
  • 3
  • 59
  • 84