JDBC Scrollable ResultSet Example | JDBC ResultSet Scrollable Tutorials

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
Whenever we create an object of ResultSet by default, it allows us to retrieve in the 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.
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 u0001 -> 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 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.
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 🙂

JDBC Updatable ResultSet Example
JDBC Interview Questions and Answers
JDBC Select Program Example
JDBC Insert Program Example
JDBC Update Program Example
JDBC Delete Program Example
JDBC PreparedStatement Example Program
Insert an Image using JDBC in Mysql DB
Read an Image in JDBC Example
CallableStatement in jdbc Example
ResultSetMetaData in JDBC Example
DatabaseMetaData in JDBC Example
Transaction Management in JDBC Example
Batch Processing in JDBC Example
JDBC Connection with Properties file

JDBC Updatable ResultSet Example
JDBC Interview Questions and Answers
JDBC Select Program Example
JDBC Insert Program Example
JDBC Update Program Example
JDBC Delete Program Example
JDBC PreparedStatement Example Program
Insert an Image using JDBC in Mysql DB
Read an Image in JDBC Example
CallableStatement in jdbc Example
ResultSetMetaData in JDBC Example
DatabaseMetaData in JDBC Example
Transaction Management in JDBC Example
Batch Processing in JDBC Example
JDBC Connection with Properties file






Suhana
November 5, 2019 at 1:19 pm - Reply 

It’s too good and helpful. So,  thanks sir









Jagadeeswaran Rangappan
October 12, 2021 at 10:48 am - Reply 

try to avoid using “SELECT * FROM”. It wont work for UPDATABLE resultset. In this example, it is insignificant because, it is READONLY. so no issues.










Suhana
November 5, 2019 at 1:19 pm - Reply 

It’s too good and helpful. So,  thanks sir




It’s too good and helpful. So,  thanks sir





Jagadeeswaran Rangappan
October 12, 2021 at 10:48 am - Reply 

try to avoid using “SELECT * FROM”. It wont work for UPDATABLE resultset. In this example, it is insignificant because, it is READONLY. so no issues.




try to avoid using “SELECT * FROM”. It wont work for UPDATABLE resultset. In this example, it is insignificant because, it is READONLY. so no issues.
 


Δ
 JDBC Driver Types
 Step by Step JDBC Program
 JDBC Select Program
 JDBC Insert Program
 JDBC Update Program
 JDBC Delete Program
 JDBC Connection – Properties File
 JDBC PreparedStatement Program
 JDBC – CallableStatement Example
 JDBC – Read an Image from DB
 JDBC – Insert an Image in DB
 JDBC – Updatable ResultSet
 JDBC – Scrollable ResultSet
 JDBC – ResultSetMetaData
 JDBC – DatabaseMetaData
 JDBC – Transaction Management
 JDBC – Batch Processing
 JDBC Interview Questions