Common ResultSet result set and offline results set the RowSet (IV)

Query the database will get a series of data, JDBC API provides related object to receive a query result set.

A, ResultSet

java.sql.ResultSet interface represents the result set of a database query.

JDBC provides the following methods to create a connection with the required statement of ResultSet:

  • createStatement(int RSType, int RSConcurrency);
  • prepareStatement(String SQL, int RSType, int RSConcurrency);
  • prepareCall(String sql, int RSType, int RSConcurrency);

RSType ResultSet parameter indicates the type of object, RSConcurrency ResultSet is one of two constants used to specify the result set is read-only or updatable.

1. ResultSet type

ResultSet.TYPE_FORWARD_ONLY: The cursor can only move forward in the result set.
ResultSet.TYPE_SCROLL_INSENSITIVE: The cursor can scroll forward and backward, the result set is not sensitive to the changes occurred after creating a database result set.
ResultSet.TYPE_SCROLL_SENSITIVE: The cursor can scroll forward and backward, sensitive to changes result other databases set to occur after the creation of the result set.

If you do not specify any ResultSet type, default value is TYPE_FORWARD_ONLY.

2. ResultSet concurrency of

ResultSet.CONCUR_READ_ONLY create read-only result set
ResultSet.CONCUR_UPDATABLE create updatable result sets
if you do not specify any concurrent type, default value is CONCUR_READ_ONLY.

3. Check the result set

ResultSet interface comprising dozens of current line data acquisition method.
Each data type may have a get method, each get method has two versions, one is using a column name, a column index is the use of (starting from 1).

Note: ResultSet After the database connection is closed (close out) can not be used after, must remain state of the connection to use, or need to use to set RowSet offline results.

Two, RowSet

RowSet interface inherited from the ResultSet interface. Compared with ResultSet, RowSet default is scrollable, updatable, serializable result set can be conveniently transmitted over the network as a JavaBean, for synchronizing data ends. For offline RowSet, the program has been from when creating a RowSet data load into memory, so you can make better use of memory performance, reduce the load on the database server, improve program performance.

1. What is the result set offline

If the direct use ResultSet, programs need to use immediately after getting ResultSet record, or if Connection closed is no longer available to resolve this situation either convert the result into a ResultSet JavaBean storage, either complete all operations before Connection closed, but these approach is not very convenient.

But was able to solve this problem by RowSet offline. RowSet the ResultSet result set may be packaged as RowSet object, the data stored in the memory operation, and can be disconnected Connection. Until after the data operation is completed, re-connect to the database, data synchronization can be performed.

That sum up, the RowSet database data can be performed off-line on the memory read and write operations, the resynchronization operation is complete after the database.

Under RowSet interface contains JdbcRowSet, CachedRowSet, FilteredRowSet, JoinRowSet, WebRowSet, except JdbcRowSet, the next four are RowSet offline.

2. RowSetFactory

In previous versions of JDK1.6 and, if you want to use JdbcRowSet, you must use JdbcRowSetImpl constructor to construct an object, but a warning at compile time, so JdbcRowSetImpl internal proprietary API, may be removed in future versions. This acquisition JdbcRowSet way is not recommended, because the use of internal API, may not be compatible with future versions, but such a program is directly coupled JdbcRowSetImpl specific implementation class, is not conducive to the maintenance and upgrades.

In JDK1.7, the introduction of RowSetFactory and RowSetProvider interface, RowSetProvider responsible for creating RowSetFactory, and RowSetFactory RowSet instance can be created by the following method.

  • createCachedRowSet()
  • createFilteredRowSet()
  • createJdbcRowSet()
  • createJoinRowSet()
  • createWebRowSet()

Example incoming filled RowSet ResultSet can create objects, execute (sql) method may also be obtained by the data filling RowSet JdbcRowSet instances after creation.

// create RowSetFactory 
RowSetFactory RowSetFactory = RowSetProvider.newFactory ();
 // create the specified RowSet 
the CachedRowSet rowSet = rowSetFactory.createCachedRowSet ();
 // the ResultSet RowSet put in 
rowSet.populate (resultSet);

3. off-line query page

The so-called page is loaded once only a few records of ResultSet, avoid CachedRowSet memory footprint is too large.

CachedRowSet paging control method:

  • populate (ResultSet rs, int startRow), loading starts from the first row startRow
  • setPageSize (int pageSize), the size of each page is provided
  • previousPage (); ResultSet availability at the bottom, so that previous recorded reading CachedRowSet
  • nextPage () ResultSet availability at the bottom, so that the next record is read CachedRowSet
import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;
import java.sql.*;

public class ConnectionManager {
    private static final String URL = "jdbc:mysql://192.168.178.5:12345/cloudDB01";
    private static final String USER_NAME = "root";
    private static final String PASSWORD = "123456";
    
    
    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println ( "MySQL database driver failed to load" );
        }
    }

    public static void main(String[] args) throws Exception{
        String sql = "select * from dept";
        Connection connection = DriverManager.getConnection(URL,USER_NAME, PASSWORD);
        PreparedStatement psmt = connection.prepareStatement(sql);
        ResultSet resultSet = psmt.executeQuery();

        RowSetFactory rowSetFactory = RowSetProvider.newFactory();
        CachedRowSet rowSet = rowSetFactory.createCachedRowSet();
        rowSet.setPageSize(2);
        rowSet.populate (the resultSet, 2); // starting from the second row 
        the while (rowSet.next ()) {
            System.out.println(rowSet.getString(2));
        }
    }
}

Note: CachedRowSet is a one-time load all the data, the memory paging, paging is not physical, but also less developed actually used.

 

Guess you like

Origin www.cnblogs.com/myitnews/p/11846087.html