JDBC Scrollable ResultSet Example

By | 2018-04-12T22:02:05+00:00 June 29th, 2016|jdbc|0 Comments

Whenever we create an object of ResultSet by default, it allows us to retrieve in forward direction only and we cannot perform any modifications on ResultSet object. Therefore, by default the ResultSet object is non-scrollable and non-updatable ResultSet.

In this tutorials, I am going to tell you how to make a ResultSet object as scrollable.

JDBC Scrollable ResultSet :

A scrollable ResultSet is one which allows us to retrieve the data in forward direction as well as backward direction but no updations are allowed. In order to make the non-scrollable ResultSet as scrollable ResultSet we must use the following createStatement() method which is present in Connection interface.

public Statement createStatement(int Type, int Mode);

Here type represents the type of scrollability and mode represents either read only or updatable. The
value of Type and the Modes are present in ResultSet interface as constant data members and they are:

  • TYPE_FORWARD_ONLY -> 1
  • TYPE_SCROLL_INSENSITIVE -> 2
  • CONCUR_READ_ONLY  -> 3

We can pass the above constants to ResultSet as below:


Statement st=con.createStatement ( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY );
ResultSet rs=st.executeQuery (“select * from empleyee”);

Whenever we create a ResultSet object, by default, constant-1 as a Type and constant-3 as a
Mode will be assigned.

ResultSet interface provides us several methods to make an ResultSet as Scrollable ResultSet below is the list of methods available in ResultSet interface.

  • public boolean next (); It returns true when rs contains next record otherwise false.
  • public void beforeFirst ();  It is used for making the ResultSet object to point to just before the first record (it is by default)
  • public boolean isFirst ();  It returns true when rs is pointing to first record otherwise false.
  • public void first ();  It is used to point the ResultSet object to first record.
  • public boolean isBeforeFirst (); It returns true when rs pointing to before first record otherwise false.
  • public boolean previous ();  It returns true when rs contains previous record otherwise false.
  • public void afterLast ();  It is used for making the ResultSet object to point to just after the last record.
  • public boolean isLast ();  It returns true when rs is pointing to last record otherwise false.
  • public void last ();  It is used to point the ResultSet object to last record.
  • public boolean isAfterLast ();  It returns true when rs is pointing after last record otherwise false.
  • public void absolute (int);  It is used for moving the ResultSet object to a particular record either in forward direction or in backward direction with respect to first record and last record respectively. If int value is positive, rs move in forward direction to that with respect to first record. If int value is negative, rs move in backward direction to that with respect to last record.
  • public void relative (int);  It is used for moving rs to that record either in forward direction or in backward direction with respect to current record.

JDBC Scrollable ResultSet Example :

package com.onlinetutorialspoint.jdbc;

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

public class ScrollResultSet {

    public static void main(String[] args) throws Exception {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/onlinetutorialspoint", "root",
                "123456");
        Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
        ResultSet rs = st.executeQuery("select * from student");
        System.out.println("RECORDS IN THE TABLE...");
        while (rs.next()) {
            System.out.println(rs.getInt(1) + " -> " + rs.getString(2));
        }
        rs.first();
        System.out.println("FIRST RECORD...");
        System.out.println(rs.getInt(1) + " -> " + rs.getString(2));
        rs.absolute(3);
        System.out.println("THIRD RECORD...");
        System.out.println(rs.getInt(1) + " -> " + rs.getString(2));
        rs.last();
        System.out.println("LAST RECORD...");
        System.out.println(rs.getInt(1) + " -> " + rs.getString(2));
        rs.previous();
        rs.relative(-1);
        System.out.println("LAST TO FIRST RECORD...");
        System.out.println(rs.getInt(1) + " -> " + rs.getString(2));
        con.close();
    }

}

Output:

RECORDS IN THE TABLE...
28 -> Chandra Shekhar
30 -> Chandra Shekhar
101 -> Rajesh
102 -> rajesh
202 -> chandrashekhar
2002 -> Rahul
2003 -> chandrashekhar
2004 -> Rahul
2005 -> chandrashekhar
3002 -> Bhargav
3005 -> Rahul
3006 -> Bharat
FIRST RECORD...
28 -> Chandra Shekhar
THIRD RECORD...
101 -> Rajesh
LAST RECORD...
3006 -> Bharat
FIRST RECORD...
3002 -> Bhargav

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment