Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "while (rs.next())" necessary here?

Tags:

java

jdbc

I want to select the maximum line number from my database "Logs" and store it in a variable m.

Here's my code:

ResultSet rs = stmt.executeQuery("Select max(Line) as L from logs");  while (rs.next()) {          // Why do I need this     int m = rs.getInt("L");     System.out.println(m); } 

But it doesn't work unless I use while(rs.next()).


If I understand correctly, rs.next() moves the cursor to the next row, but here, in this result, I only have one row.

So, can someone explain why the loop is necessary? The only thing I can think of is that the first cursor is set on the column name, am I right?

like image 857
JadedBeast Avatar asked Aug 08 '18 20:08

JadedBeast


People also ask

What is the use of while RS next ()) in Java?

As to the concrete question about rs. next() , it shifts the cursor to the next row of the result set from the database and returns true if there is any row, otherwise false .

What is purpose of next method in JDBC?

The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set. A default ResultSet object is not updatable and has a cursor that moves forward only.

Can we use RS next inside loop?

This method returns a boolean value specifying whether the ResultSet object contains more rows. If there are no rows next to its current position this method returns false, else it returns true. Using this method in the while loop you can iterate the contents of the result set.

What does next do in SQL?

Introduction to SQL FETCH NEXT. FETCH is a command in standard query language (SQL) that is used to retrieve rows from a SELECT query based on the position of a cursor. When we use NEXT as direction in conjugation with FETCH, we get FETCH NEXT that retrieves the next single row.


2 Answers

Why?

The cursor is initially placed before the first element. You need to advance it once to access the first element.

This was obviously done because traversing the results using a loop is very convenient then, as you see. From the official documentation:

Moves the cursor forward one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.


Solution

So, while you don't need any loop, you need to advance the cursor once. A single rs.next(); would technically be enough:

rs.next(); // Access the result 

However, you probably want to account for the case where there was no match at all, since:

When a call to the next method returns false, the cursor is positioned after the last row. Any invocation of a ResultSet method which requires a current row will result in a SQLException being thrown.

So the code would fail in this case.

Because of that, you should account for the case and use the returned boolean to guard your access:

if (rs.next()) {     // Access result ... } else {     // No match ... } 
like image 179
Zabuzard Avatar answered Sep 19 '22 12:09

Zabuzard


From the official documentation (which you should have read btw):

Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.

You basically need to move it, in your case moving it once is enough:

 rs.next();   System.out.println(rs.getInt("L"));  
like image 33
Eugene Avatar answered Sep 23 '22 12:09

Eugene