0

I have a single row query output loks like below

Product | Process | s_int
--------------------------
W01     | AP_03   | 1

To access the data I am using result set (rSet)

rSet.first(); 
do{
     product = rSet.getString("PRODUCT");
     process = rSet.getString("PROCESS");
     s_int = rSet.getInt("S_INT");

     System.out.println("Hello Product["+rSet.getString("PRODUCT")+"] Process["+rSet.getString("PROCESS")+"] S_Int["+rSet.getInt("sint")+"]");
}while(rSet.next());

I am getting this error:

The result set has no current row and the returns are null and 0.

What I am doing wrong?

2
  • you need to check the result of your first() call (with an if), see docs.oracle.com/javase/8/docs/api/java/sql/… The easy way is to replace the do / while by a while here
    – user180100
    Apr 1, 2016 at 15:19
  • you need to call next() before you access the data. Your code calls it after that Apr 4, 2016 at 6:32

1 Answer 1

1

ResultSet.first() returns a boolean indicating if it is on a row or not:

true if the cursor is on a valid row; false if there are no rows in the result set

Check this before attempting to access the ResultSet. An alternate structure to code:

//final ResultSet rSet = stmt.executeQuery("...");
while (rSet.next())
{
}
2
  • I have tried while (rSet.next()) { } I am getting the same error. but rSet is not empty as a counter inside the while getting to 1
    – Dhiman
    Apr 2, 2016 at 7:48
  • @Dhiman You need to do things in the while loop, otherwise when the while loop exits, you are after the last row. You only need to replace your do-while-loop with a while-loop and remove the unnecessary call to first(). Apr 2, 2016 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.