• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Jdbc » How to Working With Resultset Interface
Next →
← Prev

How to Working With Resultset Interface

By Dinesh Thakur

ResultSet is an interface present in java.sql package and provided under JDBC core API. The java.sql. ResultSet interface is defined to describe an object called ResultSet object, implemented by third-party vendor as a part of JDBC driver.

The ResultSet object represent the data in a tabular form that is retrieved using SQL query. It implies that a ResultSet object represents a data in a table whose cursor is returned by executing an SQL query. We can obtain a ResultSet by using the executeQuery() or getResultSet() method of JDBC Statement object.

Only a single ResultSet object can be opened at a time, however we can obtain multiple ResultSet objects by using one statement. When we try to open a ResultSet using a statement that is already associated with an open ResultSet, the existing ResultSet is implicitly closed. A ResultSet is also closed when the JDBC statement used to generate it is closed.

                         Resultset Interface

We’ll be covering the following topics in this tutorial:

  • Methods of ResultSet
  • Using ResultSet

Methods of ResultSet

The java.sql.ResultSet interface provides certain methods to work with the ResultSet object.

• public boolean absolute(int row)

This method is used to move the cursor to the given number of rows in a ResultSet object.

• public void afterLast()

This method is used to move the cursor just after the last row in ResultSet object.

• public void beforeFirst()

This method is used to move the cursor before the first row in ResultSet object.

• getXXX()

XXX=int, long, byte, character, String, double or any large object type. This method retrieves the column value of the specified types from the current row. The type can be any of the Java predefined data type such as int, long, byte etc.

• public ResultSetMetadata getMetaData()

This method retrieves the number, type and properties of the ResultSet object.

• public boolean first()

Move the cursor to the first row in a ResultSet object.

• public boolean last()

This methods move the cursor to the last

• public int getRow()

This method gives the current row number associated with the ResultSet object.

• public void beforeFirst()

This method moves the cursor before first row.

• public void previous()

This method moves cursor backward one row. It returns true if the cursor is newly positioned on a row and false if the cursor is positioned before the first row.

Using ResultSet

After obtaining a ResultSet object, you can use it to read the data encapsulated in it, that is the data stored in the ResultSet.

                     

You can retrieve data from a ResultSet in two simple steps:

• Move the cursor position to the required row.

• Read the column data using the getxxx() methods.

 

Moving the Cursor Position

When you obtain a ResultSet, initially the cursor is positioned before the first row, that is beforeFirst, you use the next() method of ResultSet to move the cursor positioned to the next record in ResultSet.

 

Reading the Column Value

After moving the cursor to the respective row, you can use the getter method of ResultSet to retrieve the data from the row where the cursor is positioned. The methods of ResultSet are overloaded to support the navigation of column by using its index or name.

The ResultSet object supports two versions for each of the JDBC type. One of these two indexes takes column index of int type, where column index start with 1 and other takes column name of java .lang. String type. Column name passed into getter method are not case sensitive. If a select list contains same column more than the first instance of column returns.

                            

By default Result Set is non scrollable. it means the cursor can be moved in forward direction only. if we want to move the cursor randomly ,that is ,in forward and in backward direction, then we need a scrollable result set

                      

Example: Retrieving all the row from table (Using Non scrollable ResultSet)We have to retrieve all records from emp table.

 

           

Empno

Ename

Sal

7000

smith

7000

7005

raj

8000

7009

ravi

9000

 

import java.sql.*;

public class AllRow

{

                 public static void main (String arg[ ])throws Exception

                 {

                        //load the type-4 driver

                        Class.forName (“oracle.jdbc.driver.OracleDriver”) ;

                        //get connection

                        Connection cn=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:XE”,”system”,”manager”);

                        //create CallableStatement object

                        Statement st=cn.createStatement();

                        //execute sgl query

                        ResultSet rs=st.executeQuery(“Select * from emp”);

                        //print the result

                        while(rs.next())

                               {

                                    int empno =rs.getInt(1);

                                    String name=rs.getString(2);

                                    int sal = rs.getInt(3);

                                    System.out.println(“empno” +empno+”ename”+name+”sal”+sal);

                                }

                                 rs.close();

                                 st.close();

                                 cn.close();

                  }

}

 

Output

 

empno 7000 ename Smith sal    7000

empno 7005 ename Raj     sal    8000

empno 7009 ename Ravi   sal    9000

 

Example:2 Scrollable ResultSet

                          

Empno

Ename

Sal

7000

smith

7000

7005

raj

8000

7009

ravi

9000

 

import java.sql.*;

public class ScrollableTest

{

                        public static void main (String args[]) throws Exception

                        {

                                //load the type-4 driver

                                Class.forName(“oracle.jdbc.driver.oracleDriver”);

                                //getconnection

                                Connection cn=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:XE”,”system”,”manager”);

                                //create Statement object

                                Statement st=cn.createStatement(1004,1007);

                                //execute sql query

                                ResultSet rs=st.executeQuery(“Select * from emp”);

                                rs.afterLast(); //Move the cursor after the last row

                                //print the result

                                while(rs.previous()) //Move in backward direction

                                       {

                                             System.out.println(rs.getInt(1)+”\t”+rs.getString(2)+”\t”+rs.getInt(3));

                                       }

                                             rs.close();

                                             st.close();

                                             cn.close();

                          }

}

 

Output:

 

7009 Ravi   9000

7005 Raj     8000

7000 Smith 7000

You’ll also like:

  1. JDBC ResultSet Interface Example
  2. How to Working with CallableStatement
  3. How to Working With Prepared Statement
  4. Working with Applets
  5. How to Working with JDBC
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

SQL Tutorials

SQL Tutorials

  • SQL - Home
  • SQL - Select
  • SQL - Create
  • SQL - View
  • SQL - Sub Queries
  • SQL - Update
  • SQL - Delete
  • SQL - Order By
  • SQL - Select Distinct
  • SQL - Group By
  • SQL - Where Clause
  • SQL - Select Into
  • SQL - Insert Into
  • SQL - Sequence
  • SQL - Constraints
  • SQL - Alter
  • SQL - Date
  • SQL - Foreign Key
  • SQL - Like Operator
  • SQL - CHECK Constraint
  • SQL - Exists Operator
  • SQL - Drop Table
  • SQL - Alias Syntax
  • SQL - Primary Key
  • SQL - Not Null
  • SQL - Union Operator
  • SQL - Unique Constraint
  • SQL - Between Operator
  • SQL - Having Clause
  • SQL - Isnull() Function
  • SQL - IN Operator
  • SQL - Default Constraint
  • SQL - Minus Operator
  • SQL - Intersect Operator
  • SQL - Triggers
  • SQL - Cursors

Advanced SQL

  • SQL - Joins
  • SQL - Index
  • SQL - Self Join
  • SQL - Outer Join
  • SQL - Join Types
  • SQL - Cross Join
  • SQL - Left Outer Join
  • SQL - Right Join
  • SQL - Drop Index
  • SQL - Inner Join
  • SQL - Datediff() Function
  • SQL - NVL Function
  • SQL - Decode Function
  • SQL - Datepart() Function
  • SQL - Count Function
  • SQL - Getdate() Function
  • SQL - Cast() Function
  • SQL - Round() Function

Other Links

  • SQL - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW