JDBC Scrollable ResultSet Demo
VIDEO
Enjoying Java & Database Tutorials?
Subscribe to Ram N Java on YouTube for beginner-friendly coding lessons and hands-on demos!
🔔 Subscribe to Channel
How to Use Scrollable ResultSet in Java JDBC
By default, a standard ResultSet in JDBC only allows forward iteration using the next() method. However, many real-world applications require navigating records backward, jumping to a specific row, or moving directly to the first or last record. With JDBC (Java Database Connectivity) , you can enable bidirectional navigation by creating a Scrollable ResultSet .
Key ResultSet Types for Scrolling
When creating a Statement, you can specify the scroll type by passing constants:
ResultSet.TYPE_SCROLL_INSENSITIVE: The cursor moves forwards and backwards. The result set is generally insensitive to changes made to the database while it is open.
ResultSet.TYPE_SCROLL_SENSITIVE: The cursor is scrollable and reflects changes made to the database in real-time.
Step-by-Step Java Implementation
Follow these standard steps to create and navigate a scrollable result set in JDBC:
Establish Connection: Connect to the database via DriverManager.getConnection().
Create Scrollable Statement: Create a statement passing scroll type and concurrency mode:
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
Execute Query: Run stmt.executeQuery("SELECT * FROM students") to get the scrollable ResultSet.
Navigate the Rows: Use built-in navigation methods like previous(), first(), last(), absolute(int row), or relative(int rows).
Release Resources: Cleanly close the result set and statement objects using try-with-resources.
Watch More JDBC Tutorials
Click here to watch in Youtube : https://www.youtube.com/watch?v=JbEJ-K1yKvI
Click the below Image to Enlarge
JDBCScrollableResultSetDemo Project Dir Structure
JDBCScrollableResultSetDemo.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCScrollableResultSetDemo
{
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver" ;
static final String DB_URL = "jdbc:mysql://localhost:3306/world" ;
// Database credentials
static final String USERNAME = "root" ;
static final String PASSWORD = "root" ;
public static void main (String[] args )
{
JDBCScrollableResultSetDemo jdbcScrollableResultSetDemo =
new JDBCScrollableResultSetDemo();
jdbcScrollableResultSetDemo. getCityInformation();
}
private void getCityInformation ()
{
Connection connection = null ;
PreparedStatement preparedStatement = null ;
try
{
/*
* Register the JDBC driver in DriverManager
*/
Class. forName(JDBC_DRIVER );
/*
* Establish connection to the Database using DriverManager
*/
connection = DriverManager
.getConnection(DB_URL , USERNAME , PASSWORD );
String sql = "select * from city limit ?" ;
/*
* Execute the query
*/
preparedStatement = connection
.prepareStatement(sql, ResultSet. TYPE_SCROLL_SENSITIVE ,
ResultSet. CONCUR_UPDATABLE );
preparedStatement. setInt(1 , 10 );
ResultSet rs = preparedStatement. executeQuery();
/*
* ResultSet Move Forward
*/
System. out
.println("------------- ResultSet Move Forward -----------------" );
while (rs. next())
{
int id = rs. getInt("ID" );
String name = rs. getString("Name" );
String countryCode = rs. getString("CountryCode" );
String district = rs. getString("District" );
int population = rs. getInt("Population" );
/*
* Display values
*/
System. out. print("ID: " + id);
System. out. print(", Name: " + name);
System. out. print(", CountryCode: " + countryCode);
System. out. print(", District: " + district);
System. out. println(", Population: " + population);
}
System. out
.println("\n ------------- ResultSet Move Backward ----------------" );
/*
* Move the ResultSet cursor to the last row
*/
rs. last();
while (rs. previous())
{
int id = rs. getInt(1 );
String name = rs. getString(2 );
String countryCode = rs. getString(3 );
String district = rs. getString(4 );
int population = rs. getInt(5 );
/*
* Display values
*/
System. out. print("ID: " + id);
System. out. print(", Name: " + name);
System. out. print(", CountryCode: " + countryCode);
System. out. print(", District: " + district);
System. out. println(", Population: " + population);
}
System. out
.println("\n ---- ResultSet Move to Particular position --------" );
// Getting the record of 3rd row
rs. absolute(3 );
int id = rs. getInt(1 );
String name = rs. getString(2 );
String countryCode = rs. getString(3 );
String district = rs. getString(4 );
int population = rs. getInt(5 );
/*
* Display values
*/
System. out. print("ID: " + id);
System. out. print(", Name: " + name);
System. out. print(", CountryCode: " + countryCode);
System. out. print(", District: " + district);
System. out. println(", Population: " + population);
rs. close();
}
catch (SQLException se)
{
/*
* Handle errors for JDBC
*/
se. printStackTrace();
}
catch (ClassNotFoundException e)
{
/*
* Handle errors for Class.forName
*/
e. printStackTrace();
}
finally
{
/*
* finally block used to close resources
*/
try
{
if (preparedStatement != null )
{
preparedStatement. close();
}
}
catch (SQLException sqlException)
{
sqlException. printStackTrace();
}
try
{
if (connection != null )
{
connection. close();
}
}
catch (SQLException sqlException)
{
sqlException. printStackTrace();
}
}
}
}
Output
-- -- -- -- -- -- -- -- - ResultSet Move Forward -- -- -- -- -- -- -- -- -- -- -- -
ID : 1 , Name: Kabul, CountryCode: AFG , District: Kabol, Population: 1780000
ID : 2 , Name: Qandahar, CountryCode: AFG , District: Qandahar, Population: 237500
ID : 3 , Name: Herat, CountryCode: AFG , District: Herat, Population: 186800
ID : 4 , Name: Mazar- e- Sharif, CountryCode: AFG , District: Balkh, Population: 127800
ID : 5 , Name: Amsterdam, CountryCode: NLD , District: Noord- Holland, Population: 731200
ID : 6 , Name: Rotterdam, CountryCode: NLD , District: Zuid- Holland, Population: 593321
ID : 7 , Name: Haag, CountryCode: NLD , District: Zuid- Holland, Population: 440900
ID : 8 , Name: Utrecht, CountryCode: NLD , District: Utrecht, Population: 234323
ID : 9 , Name: Eindhoven, CountryCode: NLD , District: Noord- Brabant, Population: 201843
ID : 10 , Name: Tilburg, CountryCode: NLD , District: Noord- Brabant, Population: 193238
-- -- -- -- -- -- -- -- - ResultSet Move Backward -- -- -- -- -- -- -- -- -- -- -- -
ID : 9 , Name: Eindhoven, CountryCode: NLD , District: Noord- Brabant, Population: 201843
ID : 8 , Name: Utrecht, CountryCode: NLD , District: Utrecht, Population: 234323
ID : 7 , Name: Haag, CountryCode: NLD , District: Zuid- Holland, Population: 440900
ID : 6 , Name: Rotterdam, CountryCode: NLD , District: Zuid- Holland, Population: 593321
ID : 5 , Name: Amsterdam, CountryCode: NLD , District: Noord- Holland, Population: 731200
ID : 4 , Name: Mazar- e- Sharif, CountryCode: AFG , District: Balkh, Population: 127800
ID : 3 , Name: Herat, CountryCode: AFG , District: Herat, Population: 186800
ID : 2 , Name: Qandahar, CountryCode: AFG , District: Qandahar, Population: 237500
ID : 1 , Name: Kabul, CountryCode: AFG , District: Kabol, Population: 1780000
-- -- -- -- -- -- -- -- - ResultSet Move to Particular position -- -- -- -- -- -- -- -- -- -- -- -
ID : 3 , Name: Herat, CountryCode: AFG , District: Herat, Population: 186800
No comments:
Post a Comment