这个问题在这里已经有了答案:
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 answersi 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.
更多推荐
发布评论