Karan Parikh
Karan Parikh

Reputation: 321

If resultset is not scrollable then absolute function should not work

Hi I am new to JDBC concept and as per my knowledge to move the resultset pointer in both direction resultset needs to be set scrollable

But since the resultset is not set to scrollable absolute function should not allow the pointer to move back

but the below code is working fine

Statement stmt=con.createStatement();  
    ResultSet rs=stmt.executeQuery("select * from studentinfo");  
    int counter=0;
    while(rs.next()) 
    {
        System.out.println(rs.getInt(1)+"  "+rs.getString(2));
        int row = rs.getRow();
        if(row==5)
        {
            if(counter==0)
            {
                counter++;
                rs.absolute(4);
                System.out.println(rs.getInt(1)+"  "+rs.getString(2));
            }
        }
    }
    con.close();

In this code, I am moving the pointer back to the 4th row if the current row is equal to 5th

and as you can see I haven't given any parameter in createStatement() method i.e. by default it should be ** nonscrollable** and hence above code should not fetch results however its working fine how so?

Upvotes: 0

Views: 744

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 246788

The JDBC specification says:

The default ResultSet type is TYPE_FORWARD_ONLY.

The method DatabaseMetaData.supportsResultSetType returns true if the specified type is supported by the driver and false otherwise.

If the driver does not support the type supplied to the methods createStatement, prepareStatement, or prepareCall, it generates an SQLWarning on the Connection object that is creating the statement. When the statement is executed, the driver returns a ResultSet object of a type that most closely matches the requested type. An application can find out the type of a ResultSet object by calling the method ResultSet.getType.

  • Check if TYPE FORWARD_ONLY is supported by your JDBC driver.

  • Check if you get an SQLWarning.

  • Check what ResultSet type you got.

Upvotes: 2

Related Questions