Home Tutorial Java Jdbc Jdbcresultset JDBC ResultSet Scroll Sensitive Type Example

 
 

Share on Google+Share on Google+
JDBC ResultSet Scroll Sensitive Type Example
Posted on: October 22, 2010 at 12:00 AM
Advertisement
Through this ResultSet type the cursor can move in any direction.

JDBC ResultSet Scroll Sensitive Type Example:

Through this ResultSet type the cursor can move in any direction. It is sensitive which means result set that reflect changes made.

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

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

package ResultSet;
import java.sql.*;

public class ScrollableResultSet {

  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_SCROLL_SENSITIVE,
              ResultSet.CONCUR_READ_ONLY);
           
           stmt.executeUpdate("insert into user (user_id, user_name ) values (6,'Deepak')");   
           
        ResultSet res = stmt.executeQuery("SELECT * FROM user");
        
        if (res.getType() == ResultSet.TYPE_SCROLL_SENSITIVE) {
          System.out.println("ResultSet TYPE_SCROLL_SENSITIVE.");
        }
        else {
          System.out.println("ResultSet scrollable.");
        }
        res.last();
        
        while (!res.isBeforeFirst()){
          System.out.print("User id :" + res.getInt("user_id"" ");
            System.out.println("User Name :" + res.getString("user_name"));
            res.previous();
        }
        res.close();
        stmt.close();
        connection.close();
      catch (Exception e) {
        System.err.println("Exception: "+ e.getMessage());
      }
    }
 }

If you will run this example with the help of eclipse IDE and see the output that display message related to the ResultSet type.

Program output:

The database table before execute this example is:

The eclipse console output after execute this program is:

The database table after execution of this example:

Download source code

Advertisement

Related Tags for JDBC ResultSet Scroll Sensitive Type Example:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 22, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com