JDBC ResultSet Forward Type Example


 

JDBC ResultSet Forward Type Example

Through this ResultSet type the cursor only move forward in the result set and are non-scrollable.

Through this ResultSet type the cursor only move forward in the result set and are non-scrollable.

JDBC ResultSet Forward Type Example:

Through this ResultSet type the cursor only move forward in the result set and are non-scrollable.

The following syntax are use for initialize the statement object to create a forward-only, read only ResultSet object.

Statement stmt = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

Example:

package ResultSet;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class ResultSetTypeForwordOnly {

  public static void main(String [] args) {
    Connection connection = null;
      String url = "jdbc:mysql://localhost:3306/";
      String dbName = "roseindia_jdbc_tutorials";
      String driverName = "com.mysql.jdbc.Driver";
      String userName = "root";
      String password = "root";
      
      try {
        Class.forName(driverName).newInstance();
        connection = DriverManager.getConnection(url+dbName, userName, password);

        Statement stmt = connection.createStatement(
              ResultSet.TYPE_FORWARD_ONLY,
              ResultSet.CONCUR_READ_ONLY);
        
        ResultSet res = stmt.executeQuery("SELECT * FROM user");
        
        if (res.getType() == ResultSet.TYPE_FORWARD_ONLY) {
          System.out.println("ResultSet Type Forword-Only.");
        else {
          System.out.println("ResultSet scrollable.");
        }
        res.first();
        while (res.next()){
          System.out.print("User id :" + res.getInt("user_id"" ");
            System.out.println("User Name :" + res.getString("user_name"));            
        }
        res.close();
        stmt.close();
        
        connection.close();
      catch (Exception e) {
          System.err.println("Exception: "+ e.getMessage());
      }
    }
}

Now we will run this example and see which type of ResultSet used.

Program output:

The database table is:

The eclipse console output is:

Download source code

Ads