开发者

java.sql.SQLException: Operation not allowed after ResultSet closed

I have this piece of code:

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    String Username2 = request.getParameter("Username2");
    String Password2 = request.getParameter("Password2");
    String ResetPassword = request.getParameter("ResetPassword");

    try {
        Class.forName("com.mysql.jdbc.Driver");
        String st = "jdbc:mysql://localhost:3306/LoginAccount";
        Connection conn = (Connection) DriverManager.getConnection(st, "root", "baljinder");
        Statement sta = (Statement) conn.createStatement();
        ResultSet rs = sta.executeQuery("SELECT * FROM Account where Username='" + Username2 + "' && Password ='" + Password2 + "' ;");

        while (rs.next()) {

            if (Username2.equals(rs.getString("Username")) && Password2.equals(rs.getString("Password"))) {
                sta.executeUpdate("update Account set Password ='" + ResetPassword + "' where Username='" + Username2 + "' ;");
                out.print("your successful to Reset the password");
                conn.close();
            } else {
                out.println("<h1>the Username and Password didn't match did not found </h1>");
            }
        }
    } catch (SQLException ex) {
        Logger.getLogger(AccountServlet.class.getName()).log(Level.SEVERE, null, ex);
        //out开发者_StackOverflow社区.print(ex);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(AccountServlet.class.getName()).log(Level.SEVERE, null, ex);
        out.print(ex);
    } finally {
        out.close();
    }
}

I'm getting this again & again,

java.sql.SQLException: Operation not allowed after ResultSet closed

any mistakes?


You're closing the database connection while iterating through the ResultSet. A ResultSet needs to have its connection open to work.

You should also make sure to close the JDBC connection in a finally block.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜