×
☰ See All Chapters

JDBC  ResultSet

ResultSet is an interface present in java.SQL package.

ResultSet is used to accommodate multiple rows returned by the SQL statement.

Below table lists the commonly used methods of ResultSet.

public boolean next()

Moves the cursor froward one row from its current position.

public boolean previous()

Moves the cursor to the previous row in this ResultSet object.

public boolean first()

Moves the cursor to the first row in this ResultSet object.

public boolean last()

Moves the cursor to the last row in this ResultSet object.

public boolean absolute(int row)

Moves the cursor to the given row number in this ResultSet object.

public boolean relative(int row)

Moves the cursor a relative number of rows, either positive or negative.

public int getInt(int columnIndex)

Retrieves the value of the designated column in the current row of this ResultSet object as an int in the Java programming language.

public int getInt(String columnName)

Retrieves the value of the designated column in the current row of this ResultSet object as an int in the Java programming language.

public String getString(int columnIndex)

Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.

public String getString(String columnName)

Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.

public Object getObject(int columnIndex)

Gets the value of the designated column in the current row of this ResultSet object as an Object in the Java programming language.

Different types of ResultSet

ResultSet can be divided into 3 types.

  1. Scrollable ResultSet. 

  2. Updatable ResultSet. 

  3. Sensitive ResultSet 

To get a scrollable, updatable or sensitive ResultSet you have to use the following constants present in the ResultSet interface.

jdbc-resultset-0
 

Navigating a Result Set

The cursor is movable based on the properties of the ResultSet. These properties are designated when the corresponding Statement that generated the ResultSet is created. (See the createStatement() methods in the section Simple Statements.) The possible properties of a result set cursor are:

  1. ResultSet.TYPE_FORWARD_ONLY. The cursor can only move forward in the result set. 

  2. ResultSet.TYPE_SCROLL_INSENSITIVE. The cursor can scroll forwards and backwards, and the result set is not sensitive to changes made by others to the database that occur after the result set was created. 

  3. ResultSet.TYPE_SCROLL_SENSITIVE. The cursor can scroll forwards and backwards, and the result set is sensitive to changes made by others to the database that occur after the result set was created. 

If the type is forward only, you can only navigate through the result set once, and only moving the cursor forward. Scrollable cursors can be moved forward and backward in the result set. If a result set is marked as insensitive, the result set is a snapshot of the corresponding data in the database at the time the result set was created, and subsequent changes to the database do not affect

ResultSet Concurrency

The ResultSet concurrency determines whether the ResultSet can be updated, or only read. Some databases and JDBC drivers support that the ResultSet is updated, but not all databases and JDBC drivers do. The DatabaseMetaData.supportsResultSetConcurrency(int concurrency) method returns true or false depending on whether the given concurrency mode is supported or not. The DatabaseMetaData class is covered in a later text.

A ResultSet can have one of two concurrency levels:

  1. ResultSet.CONCUR_READ_ONLY 

  2. ResultSet.CONCUR_UPDATABLE 

CONCUR_READ_ONLY means that the ResultSet can only be read.

CONCUR_UPDATABLE means that the ResultSet can be both read and updated.

 


All Chapters
Author