Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, password);
rs=pstat.executeQuery();
int rowCount=0;
while(rs.next())
{       
    rowCount++;         
}
rs.beforeFirst();
if(rowCount>=1)
{
while(rs.next())
{
    typeID=rs.getInt(1);
}

But when execute this code I am getting...

java.sql.SQLException: Result set type is TYPE_FORWARD_ONLY
at sun.jdbc.odbc.JdbcOdbcResultSet.beforeFirst(Unknown Source)
at server.ClientImpl.login(ClientImpl.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

What is causing this and how can I fix it?

like image 477
sAaNu Avatar asked Jun 16 '11 06:06

sAaNu


People also ask

What is TYPE_FORWARD_ONLY?

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() .

What is the default type of ResultSet?

A default ResultSet object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row. It is possible to produce ResultSet objects that are scrollable and/or updatable.

What is ResultSet ResultSet types?

A ResultSet object maintains a cursor that points to the current row in the result set. The term "result set" refers to the row and column data contained in a ResultSet object. The methods of the ResultSet interface can be broken down into three categories − Navigational methods − Used to move the cursor around.

What is a forward only ResultSet?

A forward only updatable result set maintains a cursor which can only move in one direction (forward), and also update rows. To create a forward only updatable result set, the statement has to be created with concurrency mode ResultSet. CONCUR_UPDATABLE and type ResultSet.


2 Answers

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

like image 65
Adithya Surampudi Avatar answered Sep 29 '22 06:09

Adithya Surampudi


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);
        }
like image 45
MByD Avatar answered Sep 29 '22 06:09

MByD