JDBC ResultSet afterLast() Example


 

JDBC ResultSet afterLast() Example

This ResultSet method set cursor to after last record. It sets the cursor position to last + 1.

This ResultSet method set cursor to after last record. It sets the cursor position to last + 1.

JDBC ResultSet afterLast() Example:

This ResultSet method set cursor to after last record. It sets the cursor position to last + 1. The void afterLast() moves the cursor to the end of this ResultSet object, just after the last row. This method has no effect if the result set contains no rows.

Syntax:

ResultSet rs;

void rs.afterLast();

Now we will create example using this method. In this example, the afterLast() point the last position in the resultset object and the resultset previous() moves the cursor to the previous row in this ResultSet object.

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 AfterLastMethodExample {

  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_id, user_name from user";
          ResultSet rs = stmt.executeQuery(selectquery);
          
          rs.afterLast();
          
          while (rs.previous()) {
              System.out.print("User id :" + rs.getInt("user_id"" ");
              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:

Download source code

Ads