如何处理java sql异常:耗尽结果集[重复](how to handle java sql exception:exhausted result set [duplicate])

编程入门 行业动态 更新时间:2024-10-27 14:30:17
如何处理java sql异常:耗尽结果集[重复](how to handle java sql exception:exhausted result set [duplicate])

这个问题在这里已经有了答案:

SQLException:Exhausted Resultset 3的答案

我有以下代码的问题。 帮我!!

Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe","system","**********"); Statement st=conn.createStatement(); String sql="select password from db where username='user'"; ResultSet rs=st.executeQuery(sql); rs.next(); String password=rs.getString("password"); if(password.equals(pass)) { RequestDispatcher rd=req.getRequestDispatcher("/home.jsp"); rd.forward(req,res); } else { out.println("invalid username and password"); }

当我执行此代码时,我得到一个java sql异常:耗尽的结果集。 提前致谢...

This question already has an answer here:

SQLException: Exhausted Resultset 3 answers

i have problem with the below code. help me!!

Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe","system","**********"); Statement st=conn.createStatement(); String sql="select password from db where username='user'"; ResultSet rs=st.executeQuery(sql); rs.next(); String password=rs.getString("password"); if(password.equals(pass)) { RequestDispatcher rd=req.getRequestDispatcher("/home.jsp"); rd.forward(req,res); } else { out.println("invalid username and password"); }

when i execute this code i am getting an java sql exception : exhausted result set. thanks in advance...

最满意答案

而不是使用rs.next(); ,使用while while while(rs.next()) 。 获得结果集后, 指针将指向第一条记录。 每次执行rs.next() , 指针都会前进到下一条记录。 如果与while一起使用,一旦到达结果集的末尾,一旦迭代了所有记录, rs.next()将返回false。 在您的情况下,由于您没有检查结果集是否已用尽并尝试提升指针 ,因此您将获得异常。

Instead of using rs.next();, use it with while e.g while(rs.next()). Once you got the resultset, the pointer will point to the first record. Each time you do a rs.next(), the pointer will advances to the next record. If you use with while, once you reach to the end of your resultset, rs.next() will return false once all records are iterated. In your case, since you are not checking whether resultset has exhausted and trying to advanced the pointer, you are getting the exception.

更多推荐

本文发布于:2023-08-02 19:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379387.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何处理   异常   java   sql   handle

发布评论

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

>www.elefans.com

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