java.sql.SQLException:结果集关闭后不允许执行操作

发布于 2024-12-08 12:31:49 字数 1856 浏览 0 评论 0原文

我有这段代码:

 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.print(ex);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(AccountServlet.class.getName()).log(Level.SEVERE, null, ex);
        out.print(ex);
    } finally {
        out.close();
    }
}

我再次得到这个&再次,

java.sql.SQLException:结果集关闭后不允许操作

有任何错误吗?

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.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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原野 2024-12-15 12:31:49

您将在迭代 ResultSet 时关闭数据库连接。 ResultSet 需要打开其连接才能工作。

您还应该确保在finally 块中关闭JDBC 连接。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文