我在java中创建我的第一个应用程序,所以基本上我想添加一个忘记密码功能。
现在我正在做的是我正在创建一个表单,并且在 POST 方法我从下拉发送三个角色,即Admin,Student,Teacher,
现在基于这些角色,我将在电子邮件中发送密码(我将来会更安全)所以首先我要检查我的角色,其次是在前端输入的电子邮件是否真的存在于数据库中,这是我的代码
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import com.suzieloveschildren.emailContactUs.EmailUtility;
import com.suzieloveschildren.entitiesClasses.Intervenants;
import com.suzieloveschildren.entitiesClasses.Mediateurs;
import com.suzieloveschildren.implementationController.EnfantsHibernateDAO;
import com.suzieloveschildren.implementationController.IntervenantsHibernateDAO;
import com.suzieloveschildren.implementationController.MediateursHibernateDAO;
import com.suzieloveschildren.initGeneric.GenericSpringServlet;
/**
* Servlet implementation class ForgotPassword
*/
@WebServlet("/ForgotPassword")
public class ForgotPassword extends GenericSpringServlet {
private static final long serialVersionUID = 1L;
@Autowired
EnfantsHibernateDAO enfantsdao;
@Autowired
MediateursHibernateDAO mediateursdao;
@Autowired
IntervenantsHibernateDAO intervenantsdao;
private String host;
private String port;
private String user;
private String pass;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/views/main/forgot.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/* Mediateurs mediateurs = new Mediateurs(); */
String url = "jdbc:mysql://localhost:3307/suzieloveschildren";
String username = "root";
String password = "";
String driverName = "com.mysql.jdbc.Driver";
String email = request.getParameter("checkmail");
String role = request.getParameter("checkrole");
/* if (role.equals("ROLE_ADMIN")) {
}*/
if (role.equals("ROLE_MED")) {
List<Mediateurs> listofemails = mediateursdao.readAllMediateursEmails();
String emails = listofemails.toString();
emails = emails.replace("[", "");
emails = emails.replace("]", "");
boolean check = false;
List<String> emailsforsend = Arrays.asList(emails.split(","));
for (String em : emailsforsend) {
// System.out.println(em);
em = em.trim();
if (Objects.equals(em, email)) {
check = true;
}
}
if (!check) {
response.sendRedirect("ForgotPassword");
} else {
try {
System.out.println(email);
Class.forName(driverName);
Connection connection = DriverManager.getConnection(url, username, password);
String sql ="SELECT PASSWORD FROM mediateurs WHERE EMAIL= "+ email;
System.out.println(sql);
PreparedStatement stmt = connection.prepareStatement(sql);
System.out.println(stmt);
ResultSet rs = stmt.executeQuery();
System.out.println("uneeb");
if (rs.next()) {
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString("PASSWORD"));
}
/*String password2 = "TestPassword";
String resultMessage = "";
try {
String subject = "Email Password Sent";
String content = "Your password is:sefssfasasdaas " ;
EmailUtility.sendEmail(host, port, user, pass, email, subject, content);
resultMessage = "The e-mail was sent successfully";
} catch (Exception ex) {
ex.printStackTrace();
resultMessage = "There were an error: " + ex.getMessage();
request.setAttribute("Message", resultMessage);
}*/
connection.close();
} catch (Exception ex) {
}
}
}
}
}
现在if (role.equals("ROLE_MED"))
部分是我被卡住的地方,在的其他条件下如果是我必须从数据库中获取密码的地方电子邮件(我的电子邮件也很独特)现在这是查询
SELECT PASSWORD FROM mediateurs WHERE EMAIL="example@yahoo.com"
现在这个查询在 phpmyadmin 上运行得非常好,但是当我尝试使用预处理语句执行它时,就像没有任何事情一样,在行之后
ResultSet rs = stmt.executeQuery();
在它下面没有任何执行,甚至没有System.out.println("uneeb");
我不知道我做错了什么,我是java的绝对初学者我已经测试了许多其他问题的解决方案,但没有一个对我有用!有什么帮助吗?