PSQLException: ResultSet not positioned properly, perhaps you need to call next

前端 未结 3 1421
无人共我
无人共我 2020-12-10 08:14
public UserBean authenticate(String username,String password){
    PostGresDAO pg=new PostGresDAO();   //creates new connection
    Connection conn=pg.getConnecion()         


        
相关标签:
3条回答
  • 2020-12-10 08:54

    Because I got the message even when I called next() on resultSet I'll tell my solution.

    The solution is NOT to call resultSet.get* on your resultSet if it's empty. So do a check if(resultSet.next()){ ...

    0 讨论(0)
  • 2020-12-10 09:05

    Instead of

    if(rs!=null)
    

    You need to check for

    if(rs.next())
    

    This will return the first row, if there are any matching rows.

    0 讨论(0)
  • 2020-12-10 09:10

    The error is telling you exactly what's wrong - you're not calling next() on your ResultSet to get to the first row of the results.

    This line:

    if(rs!=null)
    

    is pointless as far as I know; I don't believe executeQuery will ever return null. If there's a problem in your query, an exception will be thrown. If there are no results, it will return an empty result set. To see if there's a row, you should call next() and check the return value:

    if (rs.next())
    

    Additionally:

    • Catching an exception and just printing the stack trace without rethrowing is almost always the wrong approach
    • Your code suggests that you're storing passwords in plain text. Please don't. Really, really don't.
    0 讨论(0)
提交回复
热议问题