java - value - you will be given 3 integers as input. the inputs may or may not be different from each other.




Why is “while(rs.next())” necessary here? (4)

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.

consider you have something like this.

->1->2->3
^

your "rs" is initially pointed before 1, when you call rs.next() it advances the arrow to point to 1

  ->1->2->3
    ^

so if you do not call the next() method then you do not get the result.

Hope this helps

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?


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 ...
}

There are different types of Executing the Commands. Cursors are used to read the data from your executed queries. When you execute to Read, you using Forward Only Cursor by default hence you are only getting next result after calling Recorset.Next() ! I don't want to go in much deeper here. You can read about cursors here : https://docs.microsoft.com/en-us/sql/ado/guide/data/types-of-cursors-ado?view=sql-server-2017

The best solution in your case is to use Scalar Resultset which will return only ONE CELL thats exactly what you want to implement without having to loop through your result set. Following example shows how you can implement such :

using System;
using System.Data;
using System.Data.SqlClient;

class ExecuteScalar
{
  public static void Main()
  {
    SqlConnection mySqlConnection =new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");
    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
    mySqlCommand.CommandText ="SELECT COUNT(*) FROM Employee";
    mySqlConnection.Open();

    int returnValue = (int) mySqlCommand.ExecuteScalar();
    Console.WriteLine("mySqlCommand.ExecuteScalar() = " + returnValue);

    mySqlConnection.Close();
  }
}

We are using ExecuteScalar to return only ONE Cell. Remember, Even if your Query returns Multiple Rows/Columns, this will only returns VERY FIRST CELL always.


You can convert this to use a simple if statement instead.

if(rs.next()) {
    // other code here
}

You would use while when you have more than one row to bring back.





jdbc