JDBC ResultSet next() Example


 

JDBC ResultSet next() Example

In this example, we are discuss about resultset next() method that moves the cursor forward one row.

In this example, we are discuss about resultset next() method that moves the cursor forward one row.

JDBC ResultSet next() Example:

In this example, we are discuss about resultset next() method that moves the cursor forward one row. It returns true if the move succeeds and the now-current row of data exists and false if the cursor is positioned after the last row.

Syntax:

ResultSet rs;

Boolean rs.next();

Now the following code snippet show the use of next().

Example:

package ResultSet;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class NextMethodExample {

  public static void main(String[] args) {
    
    Connection connection = null;
      String url = "jdbc:mysql://localhost:3306/";
      String dbName = "roseindia_jdbc_tutorials";
      String driverName = "com.mysql.jdbc.Driver";
      String userName = "root";
      String password = "root";
      try{
        Class.forName(driverName).newInstance();
        connection = DriverManager.getConnection(url+dbName, userName, password);
        try{
          Statement stmt = connection.createStatement();
          String selectquery = "select user_name from user";
          ResultSet rs = stmt.executeQuery(selectquery);
          while(rs.next()){
            System.out.println("User Name :" + rs.getString("user_name"));
          }
        }
        catch(SQLException s){
          System.out.println(s);
        }
        connection.close();
      }
      catch (Exception e){
        e.printStackTrace();
      }  
  }
}

Now run this example using eclipse IDE and see the output.

Program output:

Database table is:

The eclipse console output is:

Program source code

Ads