Thursday, 31 March 2016

How to move the cursor in scrollable resultset ?


Create a scrollable ResultSet object
Statement stmt = connection.createStatement( 
       ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = 
       stmt.executeQuery("SELECT COLUMN_1, COLUMN_2 FROM TABLE");

Use a built in methods to scroll the resultset like afterLast(), previous(),  beforeFirst() etc.
rs.afterLast();
while (rs.previous()) {
  String name =  rs.getString("COLUMN_1");
  float salary = rs.getFloat("COLUMN_2");
  // processing here
}

Find a specific row - use absolute(), relative() methods.
  rs.absolute(4);    // cursor is on the fourth row
  int rowNum = rs.getRow();      // rowNum should be 4
  rs.relative(-3);
  int rowNum = rs.getRow();      // rowNum should be 1
  rs.relative(2);
  int rowNum = rs.getRow();   // rowNum should be 3

Use methods to check boundary status
Like : isFirst(), isLast(), isBeforeFirst(), isAfterLast()

No comments:

Post a Comment

Note: only a member of this blog may post a comment.