我正在尝试在 html 中创建重置密码。当我提交重置按钮时,它将转到 ResetPasswordServlet。然后控制器将从 servlet 转到 CustomerDAO。在那里,它需要更新数据库。我也创建了数据库连接。我对其进行了测试,但它不起作用(未在数据库中更新。)我知道我的代码有问题,但我无法弄清楚。
这是我的代码。任何人都可以帮助我找出我做错的地方。太感谢了。
ResetPasswordServlet
package com.dao;
/**
* @see HttpServlet#HttpServlet()
*/
public ResetPasswordServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<center><h2>Reset your password</h2></center>");
out.println("<form action=ResetPassword method=post>");
out.println("Enter your username <input type=text name=LoginId><br> ");
out.println("Enter your new Password<input type=password name=password><br>");
out.println("Confirm your new Password <input type=password name=confirm><br>");
out.println("<input type=submit value=RESET>");
out.println("</form>");
out.println("</body></html>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
CustomerDAO customerDAO = new CustomerDAO();
String loginId = request.getParameter("LoginId");
String loginPassword = request.getParameter("password");
String confirmPassword = request.getParameter("confirm");
Login login = customerDAO.resetPassword(loginId, loginPassword, confirmPassword);
}
}
客户DAO
public Login resetPassword(String loginId, String loginPassword, String confirmPassword) {
Login login = null;
try {
BaseDAO baseDAO = new BaseDAO();
Connection c =baseDAO.getConnection();
String query = "update test.Customer set LoginPassword=?, ConfirmPassword=? where LoginId=?" ;
PreparedStatement ps = c.prepareStatement(query);
ps.setString(1, loginId);
ps.setString(2, loginPassword);
ps.setString(3, confirmPassword);
System.out.println(loginId);
System.out.println(loginPassword);
System.out.println(confirmPassword);
int i = ps.executeUpdate();
if(i==1) {
System.out.println("record updated successfully");
}else {
System.out.println("record not updated.");
}
c.close();
}catch(Exception e) {
e.printStackTrace();
}
return login;
}
我对代码做了一些更改。
当我提交按钮时,它没有显示任何错误,也没有在数据库中更新。在 CustomerDAO 中,执行 else 块并打印未更新的记录。