PSQLException:ResultSet放置不正确,也许您需要调用下一个

编程入门 行业动态 更新时间:2024-10-20 06:35:34
本文介绍了PSQLException:ResultSet放置不正确,也许您需要调用下一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 public UserBean authenticate(String username,String password){ PostGresDAO pg=new PostGresDAO(); //creates new connection Connection conn=pg.getConnecion(); //return connection object PreparedStatement ps; ResultSet rs; String query="select password,name from scg_users where username=?"; UserBean ub=null; boolean authenticated=false; try{ ps=conn.prepareStatement(query); ps.setString(1, username); rs=ps.executeQuery(); if(rs!=null){ authenticated=password.equals(rs.getString(1)); //exception raised here if(authenticated){ ub=new UserBean(); ub.setUser(rs.getString(2)); ub.setUsername(username); } } } catch(SQLException e){ e.printStackTrace(); } return ub; }

我正在使用此代码对用户进行身份验证。用户名和密码是从request参数中提取的,并传递到此方法进行身份验证。但是它抛出:

I am using this code for authenticating a user. The username and password are extracted from the request parameter and passed onto this method for authentication. But it throws a:

org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.

请咨询。

推荐答案

错误告诉您恰好出了什么问题-您没有在ResultSet上调用 next()来获取结果的第一行。

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.

此行:

if(rs!=null)

是毫无意义的;我认为 executeQuery 不会返回null。如果您的查询有问题,将引发异常。如果没有结果,它将返回一个空结果集。要查看是否有一行,应调用 next()并检查返回值:

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

另外:

  • 捕获异常并仅打印堆栈跟踪而不进行重新抛出几乎总是错误的方法
  • 您的代码建议您以纯文本格式存储密码。请不要真的,真的不是。
  • 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.

更多推荐

PSQLException:ResultSet放置不正确,也许您需要调用下一个

本文发布于:2023-10-18 11:00:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504044.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:您需要   不正确   PSQLException   ResultSet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!