ResultSet Types
In this tutorial, we are going to discuss about the ResultSet Types. In JDBC (Java Database Connectivity), a ResultSet
object represents the result set of a database query. It provides methods to traverse and access the data returned by a SELECT query. ResultSet types define different characteristics and capabilities of a ResultSet object.
Division-1
Based on operations performed on ResultSet, we can divide ResultSet into 2 types
1. Read Only ResultSets (Static ResultSets)
2. Updatable ResultSets (Dynamic ResultSets)
1.Read Only ResultSets
- We can perform only read operations on the ResultSet by using corresponding getter methods and we cannot perform any updations.
- By default ResultSet is Read Only.
- We can specify explicitly ResultSet as Read only by using the following constant of ResultSet.
public static final int CONCUR_READ_ONLY âž” 1007
2. Updatable ResultSets
- The ResultSet which allows programmer to perform updations, such type of ResultSets are called Updatable ResultSets.
- In this case we can perform select, insert, delete and update operations.
- We can specify ResultSet explicitly as Updatable by using the following constant of ResultSet.
public static final int CONCUR_UPDATABLE âž” 1008
Division-2
Based on Cursor movement, ResultSets will be divided into the following 2 types.
- Forward only (Non-Scrollable) ResultSet
- Scrollable ResultSets
1. Forward Only ResultSets
- It allows the programmers to iterate records only in forward direction i.e., from top to bottom sequentially.
- By default every ResultSet is forward only.
- We can specify explicitly ResultSet as Forward only by using the following constant of ResultSet.
public static final int TYPE_FORWARD_ONLY âž” 1003
2. Scrollable ResultSets
- It allows the programmers to iterate in both forward and backward directions.
- We can also jump to a particular position randomly, or relative to current position. Here we can move to anywhere. There are two types of Scrollable ResultSets.
- Scroll Insensitive ResultSet
- Scroll Sensitive ResultSet
1. Scroll Insensitive ResultSet
- After getting ResultSet if we are performing any change in Database and if those changes are not reflecting to the ResultSet, such type of ResultSets are called scroll insensitive ResultSets.
- i.e., ResultSet is insensitive to database operations.
- We can specify explicitly ResultSet as Scroll insensitive by using the following constant
public static final int TYPE_SCROLL_INSENSITIVE âž” 1004
- After getting the ResultSet if we perform any change in the database and if those changes are visible to ResultSet, such type of ResultSet is called Scroll sensitive ResultSet.
- i.e., ResultSet is sensitive to database operations
- We can specify explicitly ResultSet as scroll sensitive by using the following constant.
public static final int TYPE_SCROLL_SENSITIVE âž” 1005
Differences Between Scroll Insensitive And Scroll Sensitive ResultSets
Differences between Forward only and Scrollable ResultSets
How to get Required ResultSet
We can create Statement objects as follows to get desired ResultSets.
Statement st = con.createStatement(int type,int mode);
PreparedStatement pst = con.prepareStatement(query,int type,int mode);
Allowed values for type are
ResultSet.TYPE_FORWARD_ONLY âž” 1003
ResultSet.TYPE_SCROLL_INSENSITIVE âž” 1004
ResultSet.TYPE_SCROLL_SENSITIVE âž” 1005
Allowed values for mode are
ResultSet.CONCUR_READ_ONLY âž” 1007
ResultSet.CONCUR_UPDATABLE âž” 1008
E.g: for Scroll sensitive and updatable ResultSet
Statement st = con.createStatement(1005, 1008);
Note
- Default type is forward only and default mode is read only.
- To use various types of ResultSets underlying database support and driver support must be required
- Some databases and some driver software’s won’t provide proper support.
- We can check whether the database supports a particular type of ResultSet or not by using the following methods of DatabaseMetaData.
1. public boolean supportsResultSetConcurrency(int type, int concurrency)
Retrieves whether this database supports the given concurrency type in combination with the given result set type.
2. public boolean supportsResultSetType(int type)
Retrieves whether this database supports the given ResultSet type.
Check whether database supports particular type of ResultSet or not
import java.sql.*;
/**
*
* @author ashok.mariyala
*
*/
class ResultSetTypeTest {
public static void main(String[] args) throws Exception {
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","scott","tiger");
DatabaseMetaData dbmd = con.getMetaData();
System.out.println(dbmd.supportsResultSetConcurrency(1003,1007));
System.out.println(dbmd.supportsResultSetConcurrency(1003,1008));
System.out.println(dbmd.supportsResultSetType(1003));
System.out.println(dbmd.supportsResultSetType(1004));
System.out.println(dbmd.supportsResultSetType(1005));
}
}
That’s all about the ResultSet Types. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!