我正在尝试在html中创建重置密码。当我提交重置按钮时,它将转到ResetPasswordServlet。然后控制器将从servlet转到CustomerDAO。在那里,它需要更新数据库。我也创建了数据库连接。我测试了它但它没有工作(没有在数据库中更新。)我知道我的代码有问题,但我无法弄明白。
这是我的代码。任何人都可以帮我弄清楚我做错了什么。 非常感谢你。
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);
}
}
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块并打印未更新的记录。
答案 0 :(得分:0)
您的问题在以下代码中:
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);
SQL查询字符串中占位符的顺序为:
但参数的顺序是
因此,您尝试更新LoginId
值ConfirmPassword
的记录,这种记录不太可能存在。
N.B。为什么要存储确认密码?对于servlet来说,检查LoginPassword
是否等于ConfirmPassword
并且如果不是更新数据库则显示错误会不会更有意义?