ResultSet: Exception: set type is TYPE_FORWARD_ONLY — why?

后端 未结 9 1359
春和景丽
春和景丽 2020-12-15 07:08

I have very simple code:

pstat=con.prepareStatement(\"select typeid from users where username=? and password=?\");             
pstat.setString(1, username);         


        
9条回答
  •  死守一世寂寞
    2020-12-15 07:39

    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

    Statement st = 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 cursor to the last row
    int numberOfRows = rs.getRow();         //get the number of rows
    rs.beforeFirst();                       //back to initial state
    

提交回复
热议问题