WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java JDBC · Intermediate · question 28 of 100

Explain the differences between forward-only and scrollable ResultSet in JDBC.?

📕 Buy this interview preparation book: 100 Java JDBC questions & answers — PDF + EPUB for $5

In JDBC, a ResultSet is an object that represents the data returned by an SQL query statement. There are two types of ResultSets: forward-only and scrollable.

A forward-only ResultSet can only go forward in the result set, one row at a time. Once a row is read, it cannot be returned to or accessed again. This type of ResultSet is faster and uses less memory than scrollable ResultSet since it only needs to keep track of the current row being read. It is suitable for use cases where you only need to read the result set once in a sequential manner, without the need to jump to a specific row or changing the order of the rows.

Here’s an example code snippet for a forward-only ResultSet:

    String sql = "SELECT * FROM students";
    Statement stmt = connection.createStatement();
    ResultSet rs = stmt.executeQuery(sql);
    
    while (rs.next()) {
        // process each row
    }

On the other hand, a scrollable ResultSet allows the user to move forward and backward through the result set as needed, and access any row at any time. It can be used to retrieve a subset of rows or to update or delete a specific row. However, scrollable ResultSet requires more memory and can be slower than forward-only ResultSet.

To create a scrollable ResultSet, you need to set the ResultSet type to ResultSet.TYPE_SCROLL_INSENSITIVE or
ResultSet.TYPE_SCROLL_SENSITIVE when you create the statement:

    String sql = "SELECT * FROM students";
    Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet rs = stmt.executeQuery(sql);

With scrollable ResultSet, you can move to a specific row using methods such as ‘rs.absolute()‘, ‘rs.first()‘, ‘rs.last()‘, ‘rs.next()‘, ‘rs.previous()‘. You can also get the data from a specific column of the current row using methods such as ‘getInt()‘, ‘getString()‘, etc.

    rs.absolute(3); // move to the third row
    int id = rs.getInt("id"); // get the value of the 'id' column of the current row
    rs.next(); // move to the next row

In summary, a forward-only ResultSet is faster and less memory-intensive but only allows sequential access to the result set, while a scrollable ResultSet allows for more flexibility but requires more memory and can be slower. The choice between the two depends on the specific use case and the requirements of the application.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java JDBC interview — then scores it.
📞 Practice Java JDBC — free 15 min
📕 Buy this interview preparation book: 100 Java JDBC questions & answers — PDF + EPUB for $5

All 100 Java JDBC questions · All topics