JDBC ResultSet interface example

A java.sql.ResultSet object contains the rows (record/s) from the database based on SQL statement executed. It has results that satisfy the conditions of the query. The number of records ca be zero, one or more. the record can be accessed through ResultSet object pointer from first record to last record. The current record depends on where the resultset pointer points. The records can access in forward and backward both the direction. And even we can update the records in base table through ResultSet object type.

The data in a ResultSet object is organized in rows and columns.
The ResultSet object can be created by calling executeQuery() method from Statement/PreparedStatement/CallableStatement object.The cursor of a newly created ResultSet object is positioned before the first row.

The following are common methods for accessing data from ResultSet object :  

getter methods type Method description
BigDecimal getBigDecimal() Get java.math.BigDecimal with full precision
Blob getBlob() get Blob object in the Java programming language
boolean getBoolean() get boolean value from ResultSet
byte getByte() Get byte type from ResultSet object
Date getDate() Get java.sql.Date type object from ResultSet
double getDouble() Get double type from ResultSet object
float getFloat() Get float type from ResultSet object
int getInt() Get int type from ResultSet object
long getLong() Get long type from ResultSet object
short getShort() Get short type from ResultSet object
String getString() Get String type from ResultSet object

The following methods are used to navigate the ResultSet obejct -

Navigation methods Description
boolean next() Moves the cursor to next record from its current position.
boolean previous() Moves the cursor to the previous record from its current position.
boolean first() Moves the cursor to the first row in this ResultSet object.
boolean last() Moves the cursor to the last row in this ResultSet object.
boolean relative(int rows) Moves the cursor a relative number of rows, either positive or negative.
boolean absolute(int row) Moves the cursor to the given row number in this ResultSet object.
void afterLast() Moves the cursor to the end of this ResultSet object, just after the last row.
void beforeFirst() Moves the cursor to the front of this ResultSet object, just before the first row.

Following Code demostrates the ResultSet object -

Display all records and columns
public void displayAll() {
try {
    stmt = con.createStatement();
    ResultSet res = stmt.executeQuery("select * from customer");
    disp(res);
catch (SQLException e) {
    e.printStackTrace();
}
}

Display all records and columns based on single condition
public void displayACustomer(String custId) {
String sql = "select * from Customer where custid='" + custId + "'";
try {
    stmt = con.createStatement();
    ResultSet res = stmt.executeQuery(sql);
    disp(res);
catch (SQLException e) {
    e.printStackTrace();
}
}

Display all records and columns based on multiple conditions
public void displayACustomer1(String custId, String contact) {
 String sql = "select * from Customer where custid='" + custId + 
"' and custcontact='"+contact+"'";
try {
    stmt = con.createStatement();
    ResultSet res = stmt.executeQuery(sql);
    disp(res);
catch (SQLException e) {
    e.printStackTrace();
}
}

Display specific columns from table
public void displaySpecficCoulmns() {
String sql = "select custname,custcontact from Customer";
try {
    stmt = con.createStatement();
    ResultSet res = stmt.executeQuery(sql);
    while (res.next()) {
        System.out.print(res.getString(1));
        System.out.print(res.getString(2));
        }
catch (SQLException e) {
    e.printStackTrace();
}
}

Display specific columns and rows (conditions)
public void displaySpecficCoulmnsOnCondition(String custId) {
   String sql = "select custname,custcontact from Customer 
                where custid='" + custId + "'";
try {
    stmt = con.createStatement();
    ResultSet res = stmt.executeQuery(sql);
    while (res.next()) {
        System.out.print(res.getString(1));
        System.out.print(res.getString(2));
        }
catch (SQLException e) {
    e.printStackTrace();
}
}

This method is used to display columns from ResultSet object. It takes ResultSet as parameter
public void disp(ResultSet res)
{
try {
    while (res.next()) {
        System.out.print(res.getString(1));
        System.out.print("\t" + res.getString(2));
        System.out.print("\t" + res.getString(3));
        System.out.println("\t" + res.getString(4));
    }
catch (Exception e) {
}
}
Download Source Code

Previous Next


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code