DEVFYI - Developer Resource - FYI

How to move the cursor in scrollable resultsets?(new feature in JDBC 2.0)

JDBC Interview Questions and Answers


(Continued from previous question...)

How to move the cursor in scrollable resultsets?(new feature in JDBC 2.0)

a. create a scrollable ResultSet object.

Statement stmt = con.createStatement
(ResultSet.TYPE_SCROLL_SENSITIVE,
         ResultSet.CONCUR_READ_ONLY);
ResultSet srs = stmt.executeQuery("SELECT COLUMN_1, 
         COLUMN_2 FROM TABLE_NAME");

b. use a built in methods like afterLast(), previous(), beforeFirst(), etc. to scroll the resultset.

    srs.afterLast();
    while (srs.previous()) {
        String name = srs.getString("COLUMN_1");
        float salary = srs.getFloat("COLUMN_2");
        //...

c. to find a specific row, use absolute(), relative() methods.

    srs.absolute(4); // cursor is on the fourth row
    int rowNum = srs.getRow(); // rowNum should be 4
    srs.relative(-3); 
    int rowNum = srs.getRow(); // rowNum should be 1
    srs.relative(2); 
    int rowNum = srs.getRow(); // rowNum should be 3

d. use isFirst(), isLast(), isBeforeFirst(), isAfterLast() methods to check boundary status.

(Continued on next question...)

Other Interview Questions