Java ResultSet isAfterLast() 方法及示例


當我們執行某些 SQL 查詢(通常是 SELECT 查詢)時,它們會返回表格資料。

java.sql.ResultSet 介面表示 SQL 語句返回的此類表格資料。

ResultSet 物件儲存由執行查詢資料庫語句的方法(通常是 Statement 介面的 executeQuery() 方法)返回的表格資料。

ResultSet 物件有一個游標/指標,指向當前行。最初,此游標位於第一行之前。

ResultSet 介面的 isAfterLast() 方法用於確定游標是否位於 ResultSet 的末尾之後。

rs.isAfterLast();

此方法返回一個布林值,如果游標位於 ResultSet 的末尾之後,則此值為 true,否則返回 false。

讓我們使用 CREATE 語句在 MySQL 資料庫中建立一個名為 MyPlayers 的表,如下所示:

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)
);

現在,我們將使用 INSERT 語句在 MyPlayers 表中插入 7 條記錄:

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');

以下 JDBC 程式建立與資料庫的連線,將表 MyPlayers 的內容檢索到 ResultSet 物件中,並確定游標是否位於 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:///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");
      }
   }
}

輸出

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

更新於:2019-07-30

662 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.