Using RowSet in JDBC

Original address: http://www.yiidian.com/jdbc/jdbc-jdbcrowset.html

1What is a RowSet for JDBC?

Compared with ResultSet, RowSet is a scrollable, updatable, serializable result set by default, and is used as a JavaBean, so it can be easily transmitted over the network to synchronize data on both ends. For offline RowSet, programs have read data from the underlying database to memory when they create RowSet, so they can make full use of the computer's memory and reduce the cost.Load on the database server to improve program performance.The RowSet interface inherits the ResultSet interface.

The implementation classes for the RowSet interface are as follows:

  • JdbcRowSet
  • CachedRowSet
  • WebRowSet
  • JoinRowSet
  • FilteredRowSet

Benefits of 2 RowSet

The advantages of using RowSet are as follows:

  • RowSet extends the ResultSet interface, so it is more powerful than ResultSet.
  • RowSet is more flexible in traversing table data and can scroll back and forth.
  • RowSet supports caching data, which is also available when Connection is closed.
  • RowSet supports a new way of connecting, connecting to databases without a Connection, and reading XML data sources.
  • RowSet supports Filter.
  • RowSet also supports Join operations on tables.

3 RowSet Core Code Example

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 example without event monitoring

4.1 Writing test classes

NoListenerRowSetDemo:

package com.yiidian;

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

/**
 * A little tutorial web - 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();

        //Move the cursor to get a record
        while (rowSet.next()) {
            System.out.print("number: " + rowSet.getInt(1)+"\t");
            System.out.print("Name: " + rowSet.getString(2)+"\t");
            System.out.print("Password: " + rowSet.getString(3));
            System.out.println();
        }

    }
}

4.2 Running tests

5 RowSet example with event monitoring

To perform event handling using JdbcRowSet, you need to add an instance of RowSetListener in the addRowSetListener() method of JdbcRowSet.

The RowSetListener interface provides three methods that must be implemented:

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

5.1 Writing test classes

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.*;

/**
 * A little tutorial web - 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();

        //Add event listening processing to RowSet
        rowSet.addRowSetListener(new MyListener());

        //Move the cursor to get a record
        while (rowSet.next()) {
            System.out.print("number: " + rowSet.getInt(1)+"\t");
            System.out.print("Name: " + rowSet.getString(2)+"\t");
            System.out.print("Password: " + rowSet.getString(3));
            System.out.println();
        }

    }
}

//Event Listening Handling Class
class MyListener implements RowSetListener {
    public void cursorMoved(RowSetEvent event) {
        System.out.println("Cursor movement...");
    }
    public void rowChanged(RowSetEvent event) {
        System.out.println("Cursor Change...");
    }
    public void rowSetChanged(RowSetEvent event) {
        System.out.println("RowSet change...");
    }
}

5.2 Running tests

Welcome to my public number: A little tutorial.Get exclusively organized learning resources and daily dry delivery. If you are interested in my series of tutorials, you can also follow my website: yiidian.com

Tags: Programming JDBC SQL MySQL Java

Posted on Thu, 19 Mar 2020 19:25:31 -0400 by garmann