Java ResultSet isBeforeFirst() 方法及示例


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

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

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

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

ResultSet 介面的 isBeforeFirst() 方法用於確定遊標是否位於 ResultSet 的預設位置。

rs.isBeforeFirst();

此方法返回一個布林值,如果遊標位於 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_isBeforeFirst {
   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 (after last).
      boolean bool= rs.isBeforeFirst();
      if(bool) {
         System.out.println("Cursor of the current ResultSet object is at the default poition of the ResultSet");
      } else {
         System.out.println("Cursor of the current ResultSet object is not at the default poition of the ResultSet");
      }
      rs.beforeFirst();
      if(rs.isBeforeFirst()) {
         System.out.println("Cursor of the current ResultSet object is at the default poition of the ResultSet");
      } else {
         System.out.println("Cursor of the current ResultSet object is not at the default poition of the ResultSet");
      }
   }
}

輸出

Connection established......
Cursor of the current ResultSet object is at the default poition of the ResultSet
Cursor of the current ResultSet object is at the default poition of the ResultSet

更新於: 2019-07-30

3K+ 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告