Java ResultSet isAfterLast() Method with Examples




The ResultSet interface in Java is used to retrieve data from a database in a tabular form. The isAfterLast() method is one of the navigation methods in the ResultSet that helps in checking the position of the cursor. Specifically, isAfterLast() checks whether the cursor is positioned after the last row of the result set.

Method Definition

The isAfterLast() method of the ResultSet interface is used to determine whether the cursor is after the end of the ResultSet.This method returns a boolean this value is true, if the cursor is after the end of the ResultSet else, it returns false.

rs.isAfterLast();
  • Parameters: This method does not take any parameters.
  • Returns:
    • true: If the cursor is positioned after the last row.
    • false: If the cursor is not positioned after the last row.
  • Throws: SQLException If there is an issue accessing the data.

Let us create a table with the name MyPlayers in the MySQL database using the CREATE statement as shown below ?

CREATE TABLE MyPlayers(
   ID INT,
   First_Name VARCHAR(255),
   Last_Name VARCHAR(255),
   Date_Of_Birth date,
   Place_Of_Birth VARCHAR(255),
   Country VARCHAR(255),
   PRIMARY KEY (ID)
);

Now, we will insert 7 records in the MyPlayers table using INSERT statements ?

insert into MyPlayers values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India');
insert into MyPlayers values(2, 'Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica');
insert into MyPlayers values(3, 'Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka');
insert into MyPlayers values(4, 'Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India');
insert into MyPlayers values(5, 'Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India');
insert into MyPlayers values(6, 'Ravindra', 'Jadeja', DATE('1988-12-06'), 'Nagpur', 'India');
insert into MyPlayers values(7, 'James', 'Anderson', DATE('1982-06-30'), 'Burnley', 'England');

The table will appear like this ?

| ID  | First_Name | Last_Name | Date_Of_Birth | Place_Of_Birth | Country     |
|-----|------------|-----------|---------------|----------------|-------------|
| 1   | Shikhar    | Dhawan    | 1981-12-05    | Delhi          | India       |
| 2   | Jonathan   | Trott     | 1981-04-22    | CapeTown       | SouthAfrica |
| 3   | Kumara     | Sangakkara| 1977-10-27    | Matale         | SriLanka    |
| 4   | Virat      | Kohli     | 1988-11-05    | Delhi          | India       |
| 5   | Rohit      | Sharma    | 1987-04-30    | Nagpur         | India       |
| 6   | Ravindra   | Jadeja    | 1988-12-06    | Nagpur         | India       |
| 7   | James      | Anderson  | 1982-06-30    | Burnley        | England     |

Following are the steps to set a cursor pointer at the end of the row ?

  • Database Connection: Connect to MySQL using JDBC.
  • Statement Execution: Execute a SELECT query to fetch data from the students table.
  • Iteration through ResultSet: Use next() to iterate and print the id and name.
  • Checking isAfterLast(): Verify if the cursor is after the last row to confirm all rows are processed.
  • Closing Resources: Close ResultSet, Statement, and Connection to prevent resource leaks.

Verifying whether the cursor is at the next row to the end of the ResultSet (after last).

boolean bool= rs.isAfterLast();
      if(bool) {
         System.out.println("Cursor is at the next row to the end of the ResultSet");
      }

Example

Following the JDBC program establishes a connection with the database, retrieves the contents of the table MyPlayers into a ResultSet object, and, determines whether the cursor is after the end of the ResultSet ?

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ResultSet_isAfterLast {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating the Statement
      Statement stmt = con.createStatement();
      //Query to retrieve records
      String query = "Select * from MyPlayers";
      //Executing the query
      ResultSet rs = stmt.executeQuery(query);
      //Verifying whether the cursor is at the next row to the end of the ResultSet (after last).
      boolean bool= rs.isAfterLast();
      if(bool) {
         System.out.println("Cursor is at the next row to the end of the ResultSet");
      } else {
         System.out.println("Cursor is not at the next row to the end of the ResultSet");
      }
      rs.afterLast();
      if(rs.isAfterLast()) {
         System.out.println("Cursor is at the next row to the end of the ResultSet");
      } else {
         System.out.println("Cursor is not at the next row to the end of the ResultSet");
      }
   }
}

Output

Connection established......
Cursor is not at the next row to the end of the ResultSet
Cursor is at the next row to the end of the ResultSet

Conclusion

The isAfterLast() method in JDBC allows you to efficiently check whether the cursor has passed the last row in a ResultSet. This method is particularly useful when iterating through result sets to confirm that all rows have been processed or to handle scenarios when the result set is empty. It is a simple yet effective tool for managing the navigation of a ResultSet.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-01-20T19:19:15+05:30

820 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements