简体   繁体   English

Servlet 不更新数据库中的值

[英]Servlet not updating value in database

I'm trying to create a basic password reset system.我正在尝试创建一个基本的密码重置系统。 I'm creating an unique token for each user, storing in database and retrieving that token value in url like: http://localhost:8080/login/reset-password.jsp?token=0d1eaa1a-869c-4f40-8437-a3cdaeddf497 I'm creating an unique token for each user, storing in database and retrieving that token value in url like: http://localhost:8080/login/reset-password.jsp?token=0d1eaa1a-869c-4f40-8437-a3cdaeddf497

The problem here is that I'm trying to update the password in the database but the value isn't updated.这里的问题是我正在尝试更新数据库中的密码,但值没有更新。 It displays that the password is changed, but nothing happens in database.它显示密码已更改,但数据库中没有任何反应。 Please help.请帮忙。

Here's my servlet:这是我的servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String password = request.getParameter("password");
        String token = request.getParameter("token");



                ForgotPasswordHandler.UpdatePassword(password, token);

                String message = "Password changed!";
                request.setAttribute("message", message);
                request.getRequestDispatcher("reset-password.jsp?token=" + token).forward(request, response);   



    } 

Here's my method in class ForgotPasswordHandler.java:这是我在 class ForgotPasswordHandler.java 中的方法:

public static void UpdatePassword(String password, String token) {

            try
              {

              if (con == null){
                 System.out.println("Failed connection");

              }else{

                PreparedStatement ps = con.prepareStatement(
                  "UPDATE user SET password = ? WHERE reset_token = ?");

                ps.setString(1,password);
                ps.setString(2,token);

                ps.executeUpdate();

                ps.close();


              }}
            catch (Exception e) {
                 e.printStackTrace(System.out);    
              }

            }

Your connection may have autoCommit turned off.您的连接可能已关闭 autoCommit。 If that is the problem you can fix it with code like this:如果这是问题,您可以使用以下代码修复它:

   if (!con.getAutoCommit()) {
      con.commit();
   }

One other thing to note is that if the call to ps.executeUpdate() fails, the prepared statement will not be cleaned up.需要注意的另一件事是,如果对 ps.executeUpdate() 的调用失败,则准备好的语句将不会被清理。 To avoid that problem move the close to the finally block.为避免该问题,将靠近 finally 块。 Here is what the full code would look like:以下是完整代码的样子:

PreparedStatement ps = null;
try
{
    if (con == null){
        System.out.println("Failed connection");
    } else {
        ps = con.prepareStatement(
            "UPDATE user SET password = ? WHERE reset_token = ?");

        ps.setString(1,password);
        ps.setString(2,token);

        ps.executeUpdate();   

       if (!con.getAutoCommit()) {
          con.commit();
       }
    }
}
catch (Exception e) {
        e.printStackTrace(System.out);    
} 
finally {
    if (ps != null) {
        ps.close();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM