What is Result in JDBC? How to retrieve data from ResultSet object?

JDBCJava 8MySQLMySQLi Database

A ResultSet interface in JDBC represents the tabular data generated by SQL queries. It has a cursor which points to the current row. Initially, this cursor is positioned before the first row.

Moving the pointer throughout result set

The next() method of the ResultSet interface moves the pointer of the current (ResultSet) object to the next row, from the current position. This method returns a boolean value, if there are no rows next to its current position it returns false, else it returns true. Therefore, using this method in the while loop you can iterate the contents of the result set.

while(rs.next()){
}

Getting the column values of each record:

ResultSet interface (also) provides getter methods (getXXX()) to retrieve values in each column of a row. Every getter methods have two variants:

  • getXXX(int columnIndex): This accepts an integer value representing the index of the column and returns its value.

  • getXXX(String columnLabel ): This accepts a String value representing the name of the column and returns its value.

You need to use the respective getter method based on the datatype of the column in the table.

Example

Assume we have a table named dataset with content as shown below:

+--------------+-----------+
| mobile_brand | unit_sale |
+--------------+-----------+
| Iphone       |      3000 |
| Samsung      |      4000 |
| Nokia        |      5000 |
| Vivo         |      1500 |
| Oppo         |       900 |
| MI           |      6400 |
| MotoG        |      4360 |
| Lenovo       |      4100 |
| RedMi        |      4000 |
| MotoG        |      4360 |
| OnePlus      |      6334 |
+--------------+-----------+

Example

The following example retrieves all the records of the Dataset table and prints the results:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class RetrievingData {
   public static void main(String args[]) throws Exception {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/TestDB";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");

      //Creating a Statement object
      Statement stmt = con.createStatement();
      //Retrieving the data
      ResultSet rs = stmt.executeQuery("select * from Dataset");

      System.out.println("Contents of the table");
      while(rs.next()) {
         System.out.print("Brand: "+rs.getString("Mobile_Brand")+", ");
         System.out.print("Sale: "+rs.getString("Unit_Sale"));
         System.out.println("");
      }
   }
}

Output

Connection established......
Contents of the table
Brand: Iphone, Sale: 3000
Brand: Samsung, Sale: 4000
Brand: Nokia, Sale: 5000
Brand: Vivo, Sale: 1500
Brand: Oppo, Sale: 900
Brand: MI, Sale: 6400
Brand: MotoG, Sale: 4360
Brand: Lenovo, Sale: 4100
Brand: RedMi, Sale: 4000
Brand: MotoG, Sale: 4360
Brand: OnePlus, Sale: 6334
raja
Published on 20-Mar-2019 15:52:00
Advertisements