ResultSet: Exception: set type is TYPE_FORWARD_ONLY — why?

后端 未结 9 1346
春和景丽
春和景丽 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

    This question is pretty old. I believe the solution would already have been found. However, i would like to suggest here something different from what Aditya did.

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

    Instead of ResultSet.TYPE_SCROLL_SENSITIVE, i would use INSENSITIVE

    Check this link for refernce

    0 讨论(0)
  • 2020-12-15 07:43

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

    Statement statement = 
     connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
        ResultSet.CONCUR_READ_ONLY);
    
    0 讨论(0)
  • 2020-12-15 07:45

    Like the exception says: You can't scroll your result set in any other direction than forwards. So when you loop through your result set to get the row count (I don't even understand why you do it), then this row will throw that exception:

    rs.beforeFirst();
    

    because that would scroll backwards.

    Either create your statement so that you can scroll it (Google for examples) or remove that row counting. I'd suggest the latter, since the count seems unnecessary.

    0 讨论(0)
提交回复
热议问题