Java ResultSet isLast() 方法及示例


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

java.sql.ResultSet 介面表示 SQL 語句返回的此類表格資料,即ResultSet物件儲存由執行語句的方法返回的表格資料,這些語句需要資料庫(通常是 Statement 介面的 executeQuery() 方法)。

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

Java ResultSet isLast() 方法

isLast()方法的ResultSet 介面用於確定遊標是否位於 ResultSet 的最後一行。

語法

以下是 isLast() 方法的語法

rs.isLast();

返回值

此方法返回一個布林值,如果遊標位於 ResultSet 的最後一行,則此值為 true,否則返回 false。

在 Java 中使用 isLast() 方法和 ResultSet

讓我們使用如下所示的 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 程式

下面的 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_isLast {
   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 end of the ResultSet.
      boolean bool = rs.isLast();
      if(bool) {
         System.out.println("Cursor of the current ResultSet object is on the last row of the ResultSet");
      } else {
         System.out.println("Cursor of the current ResultSet object is not on the last row of the ResultSet");
      }
      rs.last();
      if(rs.isLast()) {
         System.out.println("Cursor of the current ResultSet object is on the last row of the ResultSet");
      } else {
         System.out.println("Cursor of the current ResultSet object is not on the last row of the ResultSet");
      }
   }
}

輸出

Connection established......
Cursor of the current ResultSet object is not on the last row of the ResultSet
Cursor of the current ResultSet object is on the last row of the ResultSet

更新於:2024年8月28日

1K+ 次檢視

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告