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?
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 .
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.
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.
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.
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.
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 returnsfalse
, the cursor is positioned after the last row. Any invocation of aResultSet
method which requires a current row will result in aSQLException
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 ... }
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"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With