Skip to content Skip to sidebar Skip to footer

Resultset: Exception: Set Type Is Type_forward_only -- Why?

I have very simple code: pstat=con.prepareStatement('select typeid from users where username=? and password=?'); pstat.setString(1, username); pstat.setString(2, passw

Solution 1:

Change your first statement to this

pstat=con.prepareStatement("select typeid from users where username=? and password=?",
                            ResultSet.TYPE_SCROLL_SENSITIVE, 
                        ResultSet.CONCUR_UPDATABLE);

This way you can move forward and backward, so less things to worry about

Solution 2:

The type TYPE_FORWARD_ONLY means you can only move forward on the result set, not backward, so you get an exception when you try to go back with beforeFirst(). Instead you can either use the following prepareStatement(), which receives the resultset type as a parameter, or to do:

        pstat=con.prepareStatement("select typeid from users where username=? and password=?");             
        pstat.setString(1, username);
        pstat.setString(2, password);
        rs=pstat.executeQuery();
        int rowCount=0;

        while(rs.next())
        {
            rowCount++;
            typeID=rs.getInt(1);
        }

Solution 3:

Though this question is old, answers do not age, encountered a similar problem today, this is how I approached it, as here This is the functionality provided by Java JDBC driver, and PostgreSQL database. This case create a Statement object using the default parameters, the system-generated data sets can only be a one-way move the pointer forward, and not two-way mobile data record pointer, the former

Statement stmt = dbConn.createStatement (); Result rs = stmt.executeQuery (sql); Changed to Statement stmt = dbConn.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); Result rs = stmt.executeQuery (sql); Generated at this time rs can use rs.first () reverse move the pointer operation

Solution 4:

You can only do this with a resultset that is of type TYPE_SCROLL_SENSITIVE, which is defined as "The constant indicating the type for a ResultSet object that is scrollable and generally sensitive to changes made by others."

You need to do something like the following ...

Statementstatement= 
 connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
    ResultSet.CONCUR_READ_ONLY);

Solution 5:

java.sql.SQLException: Result set type is TYPE_FORWARD_ONLY

with JDBC 2.0 API, the user has the flexibility to move the cursor either forward or backward.

Your error can be removed by creating the statemnt as follows

Statementst= conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);

Also, a better way to count the number of rows would be

rs=pstat.executeQuery();                //execute the query
rs.last();                              //move the cursorto the lastrowint numberOfRows = rs.getRow();         //get the number ofrows
rs.beforeFirst();                       //back toinitial state

Post a Comment for "Resultset: Exception: Set Type Is Type_forward_only -- Why?"