如何在JDBC中從後往前讀取ResultSet的內容?


ResultSet 物件

某些SQL查詢(特別是SELECT)返回表格資料,在JDBC中,**java.sql.ResultSet**介面的物件儲存著由執行查詢資料庫語句的的方法(通常是Statement介面的executeQuery()方法)返回的表格資料。

ResultSet 遊標/指標

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

ResultSet 有兩種型別:只向前和雙向。預設情況下,透過**executeQuery()**方法獲得的ResultSet型別為只向前。使用此型別,您只能向前移動遊標。

雙向ResultSet

雙向ResultSet物件是指其遊標可以向前和向後移動的物件。Connection介面的**createStatement()**方法有一個變體,它接受兩個整數值,分別表示結果集型別和併發型別。

Statement createStatement(int resultSetType, int resultSetConcurrency)

要建立雙向結果集,您需要將型別設定為ResultSet.TYPE_SCROLL_SENSITIVEResultSet.TYPE_SCROLL_INSENSITIVE,以及併發性,傳遞給此方法。

//Creating a Statement object
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

然後,如果您使用此語句呼叫**executeQuery()**方法,它將返回一個雙向的ResultSet物件。

從最後列印內容

ResultSet介面的**previous()**方法將此ResultSet物件的指標從當前位置移動到前一行。

此方法返回一個布林值,指定ResultSet物件是否包含更多行。

如果在其當前位置之前沒有行,則此方法返回false,否則返回true。

ResultSet介面的Getter方法(getInt()、getString()等)接受列索引並返回當前行中指定列的值。

使用這些方法,您可以從前往後檢索ResultSet物件的內容。

while(rs.previous()) {
   System.out.print("Brand: "+rs.getString("Mobile_Brand")+", ");
   System.out.print("Sale: "+rs.getString("Unit_Sale"));
   System.out.println("");
}

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

CREATE TABLE mobile_sales (
   mobile_brand VARCHAR(255),
   unit_sale INT
);

現在,我們將使用INSERT語句在**mobile_sales**表中插入11條記錄:

insert into mobile_sales values('Iphone', 3000);
insert into mobile_sales values('Samsung', 4000);
insert into mobile_sales values('Nokia', 5000);
insert into mobile_sales values('Vivo', 1500);
insert into mobile_sales values('Oppo', 900);
insert into mobile_sales values('MI', 6400);
insert into mobile_sales values('MotoG', 4360);
insert into mobile_sales values('Lenovo', 4100);
insert into mobile_sales values('RedMI', 4000);
insert into mobile_sales values('MotoG', 4360);
insert into mobile_sales values('OnePlus', 6334);

以下示例建立與資料庫的連線,並從後往前檢索**mobile_sales**表的內容。

示例

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class BidirectionalResultSet {
   public static void main(String args[]) throws Exception {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql:///TestDB";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating a Statement object
      Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      //Retrieving the data
      ResultSet rs = stmt.executeQuery("select * from mobile_sales");
      //Moving the cursor to the end of the ResultSet
      rs.afterLast();
      System.out.println("Contents of the table");
      while(rs.previous()) {
         System.out.print("Brand: "+rs.getString("Mobile_Brand")+", ");
         System.out.print("Sale: "+rs.getString("Unit_Sale"));
         System.out.println("");
      }
   }
}

輸出

Connection established......
Contents of the table
Brand: Vivo, Sale: 1500
Brand: Nokia, Sale: 5000
Brand: Samsung, Sale: 4000
Brand: IPhone, Sale: 3000

更新於:2019-07-30

569 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告