Advance Java

Learn New things from Rixosys

JDBC ResultSet Scroll Sensitive Type Example:

JDBC ResultSet Scroll Sensitive Type Example:

Through this ResultSet type the cursor can move in any direction. It is sensitive which means result set that reflect changes made.

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

Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);

package ResultSet;
import java.sql.*;

public class ScrollableResultSet {

  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 {
        connection = DriverManager.getConnection(url+dbName, userName, password);
           Statement stmt = connection.createStatement(
           stmt.executeUpdate("insert into user (user_id, user_name ) values (6,'Deepak')");   
        ResultSet res = stmt.executeQuery("SELECT * FROM user");
        if (res.getType() == ResultSet.TYPE_SCROLL_SENSITIVE) {
          System.out.println("ResultSet TYPE_SCROLL_SENSITIVE.");
        else {
          System.out.println("ResultSet scrollable.");
        while (!res.isBeforeFirst()){
          System.out.print("User id :" + res.getInt("user_id") + " ");
            System.out.println("User Name :" + res.getString("user_name"));
      } catch (Exception e) {
        System.err.println("Exception: "+ e.getMessage());

If you will run this example with the help of eclipse IDE and see the output that display message related to the ResultSet type.

Program output:

The database table before execute this example is:

The eclipse console output after execute this program is:

The database table after execution of this example: