Tag Archives: ResultSet Tutorial

JAVA JDBC ResultSet Tutorial – Introduction and Methods

In this chapter you will learn:

1. What is ResultSet in JAVA JDBC?
2. How to Use ResultSet in JDBC Programming?
3. Methods of ResultSet
4. Cursor Pointing Example in ResultSet.

What is ResultSet in Java JDBC?

ResultSet is table of data that represents set of results which is mostly generated when you query to database and get set of result from table. When you execute a statement and read data from database it is returned into a resultset. Select statement is the standard way to select rows from a database and view them in a result set. More Fact about ResultSet

1. Default First Row is selected in ResultSet.
2. The next() method move cursor to next row of ResultSet.
3. You can use next() method in while loop to iterate through ResultSet.
4. A Default ResultSet is not updatable and Cursor moves to forward only or next row only. You cannot move cursor backward in Default Result Set.
5. In order to Update or Insert Row in ResultSet make result set TYPE_SCROLL_INSENSITIVE and CONCUR_UPDATABLE.

6. You can select data in row using column index or column name. Column index begin with 1. for example

both will select ID column from specified row. You will see the complete example of all theory below.

7. ResultSet provides getter method to retrieve data from column. Following are the example of getter methods of ResultSet.

8. You access the data in ResultSet object through a cursor. Note that this cursor is not a database cursor. It is a pointer that points one row of data in result set.

Important Methods

boolean absolute(int row)Moves the cursor to the given row number in this ResultSet object.
afterLast()Moves the cursor to the end of this ResultSet object, just after the last row.
beforeFirst()Moves the cursor to the front of this ResultSet object, just before the first row.
cancelRowUpdates()Cancels the updates made to the current row in this ResultSet object.
clearWarnings()Clears all warnings reported on this ResultSet object.
close()Releases this ResultSet object’s database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.
deleteRow()Deletes the current row from this ResultSet object and from the underlying database.
findColumn(String columnLabel)Maps the given ResultSet column label to its ResultSet column index.
first()Moves the cursor to the first row in this ResultSet object.
int getRow()Retrieves the current row number.
RowId getRowId(int columnIndex)Retrieves the value of the designated column in the current row of this ResultSet object as a java.sql.RowId object in the Java programming language.
getStatement()Retrieves the Statement object that produced this ResultSet object.
SQLWarning getWarnings()Retrieves the first warning reported by calls on this ResultSet object.
void insertRow()Inserts the contents of the insert row into this ResultSet object and into the database.
boolean last()Moves the cursor to the last row in this ResultSet object.
void moveToCurrentRow()Moves the cursor to the remembered cursor position, usually the current row.
void moveToInsertRow()Moves the cursor to the insert row.
boolean next()Moves the cursor froward one row from its current position.
boolean previous()Moves the cursor to the previous row in this ResultSet object.
void refreshRow()Refreshes the current row with its most recent value in the database.
boolean rowDeleted()Retrieves whether a row has been deleted.
boolean rowInserted()Retrieves whether the current row has had an insertion.
boolean rowUpdated()Retrieves whether the current row has been updated.
updateArray(int columnIndex, Array x)Updates the designated column with a java.sql.Array value.
updateBinaryStream(int columnIndex, InputStream x)Updates the designated column with a binary stream value.
updateBlob(int columnIndex, Blob x)Updates the designated column with a java.sql.Blob value.
updateBoolean(String columnLabel, boolean x)Updates the designated column with a boolean value.
updateByte(String columnLabel, byte x)Updates the designated column with a byte value.
updateDate(int columnIndex, Date x)Updates the designated column with a java.sql.Date value.
updateDouble(int columnIndex, double x)Updates the designated column with a double value.
updateFloat(int columnIndex, float x)Updates the designated column with a float value.
updateInt(int columnIndex, int x)Updates the designated column with an int value.
updateLong(int columnIndex, long x)Updates the designated column with a long value.
updateNull(String columnLabel)Updates the designated column with a null value.
updateRow()Updates the underlying database with the new contents of the current row of this ResultSet object.
updateShort(int columnIndex, short x)Updates the designated column with a short value.
updateString(int columnIndex, String x)Updates the designated column with a String value.

Simple ResultSet Example

Here, I am doing nothing just executing a SELECT statement and fetch data into a ResultSet. Once you learn how to use ResultSet to get data from database you will be able to insert, update or delete row in database table using ResultSet Object.

Simple ResultSet Example


ID: 1 Product : MousePad Price : 190
ID: 2 Product : Stationary Price : 2870
ID: 3 Product : Books Price : 765
ID: 4 Product : HardDisk Price : 3887
ID: 5 Product : Ram Price : 953
ID: 6 Product : Printer Price : 8746
ID: 7 Product : Keyboard Price : 646
ID: 8 Product : Mouse Price : 947
ID: 9 Product : JoyStick Price : 850
ID: 10 Product : Camera Price : 3252
Closing the connection.

Cursor Movement and ResultSet Methods() Programming Example

Here, I have combined some important methods() of ResultSet that will help you to understand ResultSet objects more clearly. You will also learn how to move cursor in different position of row of RowSet.


Shifting Row to last position : Current Row : 10
Shifting Row to First Position : Current Row : 1
Current row is 10
After deleting a row, now current row is : 9
Current Insert Row : 9
Previous Row : 8
Closing the connection.


In this chapter you learn What is RowSet in JDBC and how to use it in programming. You also learn Cursor Management in RowSet. The next chapter is Updating a Row in Database Table using RowSet.