In this example we shall show you how to create and use a scrollable ResultSet. To use a scrollable ResultSet one should perform the following steps:
- Load the JDBC driver, using the
forName(String className)
API method of the Class. In this example we use the MySQL JDBC driver.
- Create a Connection to the database. Invoke the
getConnection(String url, String user, String password)
API method of the DriverManager to create the connection.
- Create a Statement, using the
createStatement()
API method of the Connection. The Statement must have the type ResultSet.TYPE_SCROLL_INSENSITIVE or ResultSet.TYPE_SCROLL_SENSITIVE and the concurrency ResultSet.CONCUR_UPDATABLE, in order to return scrollable result sets.
- Execute the query to the database, using the
executeQuery(String sql)
API method. The data produced by the given query is a ResultSet.
- Get the cursor position, with the
getRow()
API method and check if it is before the first row, with the isBeforeFirst()
API method.
- Invoke the
next()
API method to move the cursor to next row, and last()
API method to move cursor to the last row. In order to check if it is in the last row, we can call the isLast()
API method.
- Move the cursor to the end of this ResultSet object, just after the last row, with the
afterLast()
API method and use the isAfterLast()
API method to check if it is after the last row.
- Move cursor to other rows, with the
absolute(int row)
API method and check again its position.
- Invoke the
relative(int rows)
API method to move the cursor
as described in the code snippet below.
001 |
package com.javacodegeeks.snippets.core; |
003 |
import java.sql.Connection; |
004 |
import java.sql.DriverManager; |
005 |
import java.sql.ResultSet; |
006 |
import java.sql.SQLException; |
007 |
import java.sql.Statement; |
009 |
public class ScrollableResultSetExample { |
011 |
public static void main(String[] args) { |
013 |
Connection connection = null ; |
018 |
String driverName = "com.mysql.jdbc.Driver" ; |
020 |
Class.forName(driverName); |
025 |
String serverName = "localhost" ; |
027 |
String schema = "test" ; |
029 |
String url = "jdbc:mysql://" + serverName + "/" + schema; |
031 |
String username = "username" ; |
033 |
String password = "password" ; |
035 |
connection = DriverManager.getConnection(url, username, password); |
039 |
System.out.println( "Successfully Connected to the database!" ); |
042 |
} catch (ClassNotFoundException e) { |
044 |
System.out.println( "Could not find the database driver " + e.getMessage()); |
045 |
} catch (SQLException e) { |
047 |
System.out.println( "Could not connect to the database " + e.getMessage()); |
072 |
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
074 |
ResultSet results = statement.executeQuery( "SELECT * FROM test_table" ); |
079 |
System.out.println( "Cursor position " + results.getRow() + ", is before first ? " + results.isBeforeFirst()); |
089 |
System.out.println( "Cursor position " + results.getRow() + ", is first ? " + results.isFirst()); |
099 |
System.out.println( "Cursor position " + results.getRow() + ", is last ? " + results.isLast()); |
109 |
System.out.println( "Cursor position " + results.getRow() + ", is after last ? " + results.isAfterLast()); |
119 |
System.out.println( "Cursor position " + results.getRow()); |
124 |
results.absolute(- 1 ); |
129 |
System.out.println( "Cursor position " + results.getRow() + ", is last ? " + results.isLast()); |
134 |
results.absolute(- 4 ); |
139 |
System.out.println( "Cursor position " + results.getRow()); |
151 |
System.out.println( "Cursor position " + results.getRow() + ", is after last ? " + results.isAfterLast()); |
158 |
results.relative(- 13 ); |
163 |
System.out.println( "Cursor position " + results.getRow() + ", is before first ? " + results.isBeforeFirst()); |
166 |
} catch (SQLException e) { |
168 |
System.out.println( "Could not retrieve data from the database " + e.getMessage()); |
Example Output:
Successfully Connected to the database!
Cursor position 0, is before first ? true
Cursor position 1, is first ? true
Cursor position 11, is last ? true
Cursor position 0, is after last ? true
Cursor position 3
Cursor position 11, is last ? true
Cursor position 8
Cursor position 0, is after last ? true
Cursor position 0, is before first ? true
This was an example of how to create and use a scrollable ResultSet in Java.