JDBC ResultSet Interface

ResultSet interface maintains a cursor pointing to move forward and backward direction by passing either TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE in createStatement(int,int) method as well as we can make this object as updatable by.


                        Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);  
                    
ResultSet Interface methods are:
  1. 1. public boolean next(): is used to move the cursor to the one row next from the current position.
  2. 2. public boolean previous(): is used to move the cursor to the one row previous from the current position.
  3. 3. public boolean first(): is used to move the cursor to the first row in result set object.
  4. 4. public boolean last(): is used to move the cursor to the last row in result set object.
  5. 5. public boolean absolute(int row): is used to move the cursor to the specified row number in the ResultSet object.
  6. 6. public boolean relative(int row): is used to move the cursor to the relative row number in the ResultSet object, it may be positive or negative.
  7. 7. public int getInt(int columnIndex): is used to return the data of specified column index of the current row as int.
  8. 8. public int getInt(String columnName): is used to return the data of specified column name of the current row as int.
  9. 9. public String getString(int columnIndex): is used to return the data of specified column index of the current row as String.
  10. 10. public String getString(String columnName): is used to return the data of specified column name of the current row as String.
Example Program

                        package com.java.session.ninteen;

                        import java.sql.*;

                        public class ResultSetInterfaceExample {
                            static final String DB_URL = "jdbc:mysql://localhost/emp";
                            static final String USER = "root";
                            static final String PASS = "PASSWORD";
                            static final String QUERY = "SELECT id, first, last, age FROM employee";

                            public static void main(String[] args) {
                                Connection conn = null;
                                Statement stmt = null;
                                ResultSet rs = null;
                                try {
                                    conn = DriverManager.getConnection(DB_URL, USER, PASS);
                                    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                                    rs = stmt.executeQuery(QUERY);
                                    rs.absolute(3);
                                    System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));

                                } catch (SQLException e) {
                                    e.printStackTrace();
                                }
                                try {
                                    conn.close();
                                    stmt.close();
                                    rs.close();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }