Java JDBC ResultSet create scrollable result set from Connection

Description

Java JDBC ResultSet create scrollable result set from Connection

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {
   /*from   ww w  .  j a v a 2  s.  co m*/
   public static void queryDbRecipes(Connection conn){
       String sql = "SELECT ID, RECIPE_NUM, NAME, DESCRIPTION " +
                    "FROM RECIPES";
       PreparedStatement pstmt = null;
       try {
           
           pstmt = conn.prepareStatement(sql, 
                   ResultSet.TYPE_SCROLL_INSENSITIVE, 
                   ResultSet.CONCUR_READ_ONLY);
           ResultSet rs = pstmt.executeQuery();
           rs.first();
           System.out.println(rs.getString(2) + ": " + rs.getString(3) + 
                           " - " + rs.getString(4));
           rs.next();
           System.out.println(rs.getString(2) + ": " + rs.getString(3) + 
                           " - " + rs.getString(4));
           rs.previous();
           System.out.println(rs.getString(2) + ": " + rs.getString(3) + 
                           " - " + rs.getString(4));
           rs.last();
           System.out.println(rs.getString(2) + ": " + rs.getString(3) + 
                           " - " + rs.getString(4));
       } catch (SQLException ex) {
           ex.printStackTrace();
       } finally {
           if (pstmt != null){
               try {
                   pstmt.close();
               } catch (SQLException ex) {
                   ex.printStackTrace();
               }
           }
       }
       
   }
}



PreviousNext

Related