Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is ResultSet Interface in Java?

Published on Sep 27,2019 5.1K Views


The SQL statements that read data from a database query, return the data in a result set. The SELECT statement is the standard way to select rows from a database and view them in a result set. java.sql ResultSet interface in Java represents the result set of a database query. In this article, we will understand the ResultSet interface in Java.

 

What is ResultSet?

A ResultSet object maintains a cursor that points to the current row in the result set.

ResultSet Interface in Java Logo

The term “result set” refers to the row and column data contained in a ResultSet object.

 

Commonly used Methods of ResultSet Interface in Java

MethodsDescription
public boolean next():

Used to move the cursor to the one row next from the current position.

public boolean previous():

Used to move the cursor to the one row previous from the current position.

public boolean first():

Used to move the cursor to the first row in result set object.

public boolean last():

Used to move the cursor to the last row in result set object.

public boolean absolute(int row):

Used to move the cursor to the specified row number in the ResultSet object.

public boolean relative(int row):

Used to move the cursor to the relative row number in the ResultSet object, it may be positive or negative.

public int getInt(int columnIndex):

Used to return the data of specified column index of the current row as int.

public int getInt(String columnName):

Used to return the data of specified column name of the current row as int.

public String getString(int columnIndex):

Used to return the data of specified column index of the current row as String.

public String getString(String columnName):

Used to return the data of specified column name of the current row as String.

 

Example for ResultSet Interface

//STEP 1. Import required packages

import java.sql.*;

public class JDBCExample {

   // JDBC driver name and database URL

   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  

   static final String DB_URL = "jdbc:mysql://localhost/EMP";



   //  Database credentials

   static final String USER = "username";

   static final String PASS = "password";
 

public static void main(String[] args) {

   Connection conn = null;

   Statement stmt = null;

   try{

      //STEP 2: Register JDBC driver

      Class.forName("com.mysql.jdbc.Driver");



      //STEP 3: Open a connection

      System.out.println("Connecting to database...");

      conn = DriverManager.getConnection(DB_URL,USER,PASS);




      //STEP 4: Execute a query to create statment with

      // required arguments for RS example.

      System.out.println("Creating statement...");

      stmt = conn.createStatement(

                           ResultSet.TYPE_SCROLL_INSENSITIVE,

                           ResultSet.CONCUR_READ_ONLY);

      String sql;

      sql = "SELECT id, first, last, age FROM Employees";

      ResultSet rs = stmt.executeQuery(sql);



      // Move cursor to the last row.

      System.out.println("Moving cursor to the last...");

      rs.last();

      

      //STEP 5: Extract data from result set

      System.out.println("Displaying record...");

      //Retrieve by column name

      int id  = rs.getInt("id");

      int age = rs.getInt("age");

      String first = rs.getString("first");

      String last = rs.getString("last");

  

      //Display values

      System.out.print("ID: " + id);

      System.out.print(", Age: " + age);

      System.out.print(", First: " + first);

      System.out.println(", Last: " + last);



      // Move cursor to the first row.

      System.out.println("Moving cursor to the first row...");

      rs.first();

      

      //STEP 6: Extract data from result set

      System.out.println("Displaying record...");

      //Retrieve by column name

      id  = rs.getInt("id");

      age = rs.getInt("age");

      first = rs.getString("first");

      last = rs.getString("last");

  

      //Display values

      System.out.print("ID: " + id);

      System.out.print(", Age: " + age);

      System.out.print(", First: " + first);

      System.out.println(", Last: " + last);

     // Move cursor to the first row.



      System.out.println("Moving cursor to the next row...");

      rs.next();

      

      //STEP 7: Extract data from result set

      System.out.println("Displaying record...");

      id  = rs.getInt("id");

      age = rs.getInt("age");

      first = rs.getString("first");

      last = rs.getString("last");

  

      //Display values

      System.out.print("ID: " + id);

      System.out.print(", Age: " + age);

      System.out.print(", First: " + first);

      System.out.println(", Last: " + last);




      //STEP 8: Clean-up environment

      rs.close();

      stmt.close();

      conn.close();

   }catch(SQLException se){

      //Handle errors for JDBC

      se.printStackTrace();

   }catch(Exception e){

      //Handle errors for Class.forName

      e.printStackTrace();

   }finally{

      //finally block used to close resources

      try{

         if(stmt!=null)

            stmt.close();

      }catch(SQLException se2){

      }// nothing we can do

      try{

         if(conn!=null)

            conn.close();

      }catch(SQLException se){

         se.printStackTrace();

      }//end finally try

   }//end try

   System.out.println("Goodbye!");

}//end main

}//end JDBCExample

Output:

ResultSet Interface in Java

 

With this, we come to an end of this ResultSet Interface in Java Article. I hope you got an understanding of how to use this function.

Check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this “ResultSet interface in Java” blog and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 21st December,2024

21st December

SAT&SUN (Weekend Batch)
View Details
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is ResultSet Interface in Java?

edureka.co