Followers

JDBC: Scrollable Result Set

package com.pack1;

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

public class ScrollResultSet {
   
    public static void main(String[] args){
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            String url = "jdbc:derby://localhost:1527/hari";
            con = DriverManager.getConnection(url, "app","app");
            stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            String query = "select * from MYEMP";
            rs = stmt.executeQuery(query);
            rs.absolute(2);
            System.out.println("Data of second row...");
                System.out.println("Employee number : "+rs.getInt(1));          
                System.out.println("Employee Name : "+rs.getString(2));          
                System.out.println("Employee Salary : "+rs.getFloat(3));          
                System.out.println("Employee Address : "+rs.getString(4));          
             System.out.println("================");
             rs.relative(1);
             rs.last();
             System.out.println("The current row position "+rs.getRow());
             rs.afterLast();
             System.out.println("Data in reverse order is ...");
             while(rs.previous()){
                System.out.println("Employee number : "+rs.getInt(1));          
                System.out.println("Employee Name : "+rs.getString(2));          
                System.out.println("Employee Salary : "+rs.getFloat(3));          
                System.out.println("Employee Address : "+rs.getString(4));
           
             }
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }catch (SQLException ex1) {
            ex1.printStackTrace();
        }
    }
}

No comments:

Post a Comment