ResultSet Interface in JDBC

ResultSet Interface

  • The result of the query after execution of database statement is returned as table of data according to rows and columns. This data is accessed using the ResultSet interface.
  • A default ResultSet object is not updatable and the cursor moves only in forward direction.

Creating ResultSet Interface

To execute a Statement or PreparedStatement, we create ResultSet object.

Example

Statement stmt = connection.createStatement();
ResultSet result = stmt.executeQuery("select * from Students");

Or

String sql = "select * from Students";
PreparedStatementstmt = con.prepareStatement(sql);
ResultSet result = stmt.executeQuery();


ResultSet Interface Methods

MethodsDescription
public boolean absolute(int row)Moves the cursor to the specified row in the ResultSet object.
public void beforeFirst( )It moves the cursor just before the first row i.e. front of the ResultSet.
public void afterLast()Moves the cursor to the end of the ResultSet object, just after the last row.
public boolean first()Moves the cursor to first value of ResultSet object.
public boolean last( )Moves the cursor to the last row of the ResultSet object.
public boolean previous ( )Just moves the cursor to the previous row in the ResultSet object.
public boolean next( )It moves the curser forward one row from its current position.
public intgetInt(intcolIndex)It retrieves the value of the column in current row as int in given ResultSet object.
public String getString( intcolIndex)It retrieves the value of the column in current row as int in given ResultSet object.
public void relative(int rows)It moves the cursor to a relative number of rows.

Types of ResultSet Interface

1. ResultSet.TYPE_FORWARD_ONLY
The ResultSet can only be navigating forward.

2. ResultSet.TYPE_SCROLL_INSENSITIVE
The ResultSet can be navigated both in forward and backward direction. It can also jump from current position to another position. The ResultSet is not sensitive to change made by others.

3. ResultSet.TYPE_SCROLL_SENSITIVE
The ResultSet can be navigated in both forward and backward direction. It can also jump from current position to another position. The ResultSet is sensitive to change made by others to the database.

Example : Program to illustrate ResultSet interface with scrollable

import java.sql.*;
classResultSetTest
{
     public static void main(String args[])
     {
         Connection con = null;
         Statement stmt = null;
         ResultSetrs = null;
         try
         {
              Class.forName("oracle.jdbc.driver.OracleDriver");
              con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "scott", "tiger");
              stmt = con.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
              rs = stmt.executeQuery("Select * from Student");
              rs.absolute(5);     //Accessing 5th row
             System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
             rs.close();
             stmt.close();
             con.close();
         }
         catch(Exception e)
         {
             e.printStackTrace();
         }
     }
}

ResultSetMetaData Interface

ResultSetMetaData is an interface which provides information about a result set that is returned by an executeQuery() method. The ResultSetMetaData extends the Wrapper interface.

This interface provides the metadata about the database. It includes the information about the names of the columns, number of columns etc.

ResultSetMetaData Interface Methods

MethodsDescription
public String getColumnClassName(int column)It returns the name of the Java class whose instances are created.
public intgetColumnCount()It returns the number of columns in the ResultSet object.
public String getColumnName(int column)Returns the column name from the ResutlSet object.
public intgetColumnType(int column)It retrieves the column’s which is designated in SQL.
public String getSchemaName(int column)Return the table’s schema which designed with column.
public String getTableName(int column)Returns the designed SQL table’s name.

Example : Illustrating the ResultSetMetaData Interface

//RSMDTest.java

import java.sql.*;
classRSMDTest
{
     public static void main(String args[])
     {
         try
         {
             Class.forName("oracle.jdbc.driver.OracleDriver");
             Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "scott", "tiger");
             PreparedStatementps = con.prepareStatement("select * from Students");
             ResultSetrs = ps.executeQuery();
             ResultSetMetaDatarsmd = rs.getMetaData();
             for (int i = 1; i <= cols; i++)
             {
                  String colName = rsmd.getColumnName(i);
                  String colType = rsmd.getColumnTypeName(i);
                  System.out.println(colName+" of type "+colType);
             }
             rs.close();
             ps.close();
             con.close();
         }
         catch(Exception e)
         {
             e.printStackTrace();
         }
     }
}

DatabaseMetaData Interface

The DatabaseMetaData is an interface that tells us the type of driver we are using, database product version, driver name, total number of table etc. It also provides all details about database providers.

DatabaseMetaData Interface Methods

MethodsDescription
public Connection getConnection( )It retrieves the connection that produced the given metadata object.
public intgetDatabaseMajorVersion()Returns the major version number of the database.
public intgetDatabaseMinorVersion()Returns the minor version number of the database.
public String getDatabaseProductName( )Used to retrieve the name of this database product.
public String getDriverName( )Retrieves the name of the JDBC driver which is used in application.
public String getURL( )It returns the URL of the current DBMS.

Example : Different methods of DatabaseMetaData Interface

import java.sql.*;
classDatabaseMetaDataDemo
{
    public static void main(String args[])
    {
        Connection conn = null;
        try
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","scott","tiger");
            DatabaseMetaDatadbmd = conn.getMetaData();
            System.out.println("Driver name "+dbmd.getDriverName());
            System.out.println("Driver Version "+dbmd.getDriverVersion());
            System.out.println("Database Product name "+dbmd.getDatabaseProductName());
            System.out.println("Database product version "+dbmd.getDatabaseProductVersion());
            System.out.println("Username "+dbmd.getUserName());
            Sysetm.out.println("URL of database "+dbmd.getURL());
        }
        catch(SQLException e)
        {
             e.printStackTrace();
        }
        finally
        {
             if(conn != null)
             {
                 try
                 {
                     conn.close();
                 }
                 catch(SQLException e)
                 {
                     e.printStackTrace();
                 }
             }
        }
    }
}