在JDBC中使用RowSet


原文地址: http://www.yiidian.com/jdbc/jdbc-jdbcrowset.html

1 什么是JDBC的RowSet?

與ResultSet相比,RowSet默認是可滾動、可更新、可序列化的結果集,而且作為JavaBean使用,因此能方便的在網絡上傳輸,用於同步兩端的數據,對於離線RowSet而言,程序在創建RowSet時已經把數據從底層數據庫讀取到了內存,因此可以充分利用計算機的內存,從而降低數據庫服務器的負載,提高程序性能。RowSet接口繼承了ResultSet接口。

RowSet接口的實現類如下:

  • JdbcRowSet
  • CachedRowSet
  • WebRowSet
  • JoinRowSet
  • FilteredRowSet

2 RowSet的好處

使用RowSet的優點如下:

  • RowSet擴展了ResultSet接口,因此它的功能比ResultSet更加強大。
  • RowSet對表數據的遍歷更加靈活,可前后滾動。
  • RowSet支持緩存數據,即在Connection關閉后也可以使用。
  • RowSet支持新的連接方式,無需Connection即可連接數據庫,還支持讀取XML數據源。
  • RowSet支持Filter(過濾數據)。
  • RowSet還支持表的Join操作。

3 RowSet核心代碼示例

JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();
rowSet.setUrl("jdbc:mysql://localhost:3306/test");
rowSet.setUsername("root");
rowSet.setPassword("root");

rowSet.setCommand("select * from t_user");
rowSet.execute();

4 沒有事件監聽的RowSet示例

4.1 編寫測試類

NoListenerRowSetDemo:

package com.yiidian;

import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetProvider;
import java.io.*;
import java.sql.*;

/**
 * 一點教程網 - http://www.yiidian.com
 */
public class NoListenerRowSetDemo {
    public static void main(String args[])throws Exception {
        Class.forName("com.mysql.jdbc.Driver");

        JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();
        rowSet.setUrl("jdbc:mysql://localhost:3306/test");
        rowSet.setUsername("root");
        rowSet.setPassword("root");

        rowSet.setCommand("select * from t_user");
        rowSet.execute();

        //移動光標,獲取記錄
        while (rowSet.next()) {
            System.out.print("編號: " + rowSet.getInt(1)+"\t");
            System.out.print("名稱: " + rowSet.getString(2)+"\t");
            System.out.print("密碼: " + rowSet.getString(3));
            System.out.println();
        }

    }
}

4.2 運行測試

file

5 有事件監聽的RowSet示例

要使用JdbcRowSet執行事件處理,您需要在JdbcRowSet的addRowSetListener() 方法中添加RowSetListener的實例。

RowSetListener接口提供3種必須實現的方法:

public void cursorMoved(RowSetEvent event);
public void rowChanged(RowSetEvent event);
public void rowSetChanged(RowSetEvent event);

5.1 編寫測試類

HasListenerRowSetDemo:

package com.yiidian;

import javax.sql.RowSetEvent;
import javax.sql.RowSetListener;
import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetProvider;
import java.io.*;
import java.sql.*;

/**
 * 一點教程網 - http://www.yiidian.com
 */
public class HasListenerRowSetDemo {
    public static void main(String args[])throws Exception {
        Class.forName("com.mysql.jdbc.Driver");

        JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();
        rowSet.setUrl("jdbc:mysql://localhost:3306/test");
        rowSet.setUsername("root");
        rowSet.setPassword("root");

        rowSet.setCommand("select * from t_user");
        rowSet.execute();

        //給RowSet添加事件監聽處理
        rowSet.addRowSetListener(new MyListener());

        //移動光標,獲取記錄
        while (rowSet.next()) {
            System.out.print("編號: " + rowSet.getInt(1)+"\t");
            System.out.print("名稱: " + rowSet.getString(2)+"\t");
            System.out.print("密碼: " + rowSet.getString(3));
            System.out.println();
        }

    }
}

//事件監聽處理類
class MyListener implements RowSetListener {
    public void cursorMoved(RowSetEvent event) {
        System.out.println("光標移動...");
    }
    public void rowChanged(RowSetEvent event) {
        System.out.println("光標改變...");
    }
    public void rowSetChanged(RowSetEvent event) {
        System.out.println("RowSet改變...");
    }
}

5.2 運行測試

file

file

歡迎關注我的公眾號::一點教程。獲得獨家整理的學習資源和日常干貨推送。
如果您對我的系列教程感興趣,也可以關注我的網站:yiidian.com


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM