JDBC ResultSet Scroll Insensitive Type Example


 

JDBC ResultSet Scroll Insensitive Type Example

Through this ResultSet type the cursor can move in any direction.

Through this ResultSet type the cursor can move in any direction.

JDBC ResultSet Scroll Insensitive Type Example:

Through this ResultSet type the cursor can move in any direction. It is insensitive which means result set that does not reflect changes made while it is still open. It is default resultset type for MySql.

The following syntax are use for initialize the statement object to create a Scroll-Insensitive, read only ResultSet object.

Statement stmt = connection.createStatement

(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

Example:

package ResultSet;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class InsensitiveResultSet {

  public static void main(String [] args) {
    Connection connection = null;
      String url = "jdbc:mysql://localhost:3306/";
      String dbName = "roseindia_jdbc_tutorials";
      String driverName = "com.mysql.jdbc.Driver";
      String userName = "root";
      String password = "root";
      
      try {
        Class.forName(driverName).newInstance();
        connection = DriverManager.getConnection(url+dbName, userName, password);

        Statement stmt = connection.createStatement(
              ResultSet.TYPE_SCROLL_INSENSITIVE,
              ResultSet.CONCUR_READ_ONLY);
           
        ResultSet res = stmt.executeQuery("SELECT * FROM user");
        
        if (res.getType() == ResultSet.TYPE_SCROLL_INSENSITIVE) {
          System.out.println("ResultSet Type Scroll Insensitive.");
        }
        else {
          System.out.println("ResultSet no Scroll Insensitive.");
        }
        
        res.last();
  
        while (!res.isBeforeFirst()){
          System.out.print("User id :" + res.getInt("user_id"" ");
            System.out.println("User Name :" + res.getString("user_name"));
            res.previous();
        }
        res.close();
        stmt.close();

        connection.close();
      catch (Exception e) {
        System.err.println("Exception: "+e.getMessage());
      }
    }
}

Now we will run this example and see which type of ResultSet used.

Program output:

The database table is:

The eclipse console output is:

Download source code

Ads