[英]a login form error + JDBC CONNECTIVITY(JAVA-MYSQL)
我有一個具有JDBC連接(JAVA-MYSQL)的項目,該項目具有登錄表單。 但是編碼不起作用。(這意味着它始終顯示“ WRONG PASSWORD”(錯誤密碼),盡管我確定它是正確的)。
請找到錯誤。 (因為它沒有顯示)。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String S; String email = jTextField3.getText(); try { Class.forName("java.sql.Driver"); Connection con = DriverManager.getConnection("jdbc:Mysql://localhost/nami", "root", "123456"); Statement st = con.createStatement(); S = "SELECT password FROM signup WHERE email =" + "'" + email + "'" + ";"; st.executeQuery(S); String pass = new String(jPasswordField2.getPassword()); if (pass.equals("S")) { jOptionPane1.showMessageDialog(null, "YOU HAVE SUCCESSFULLY LOGGED IN"); MAINPAGE at = new MAINPAGE(); jDesktopPane1.add(at); at.show(); } else { jOptionPane1.showMessageDialog(null, "WRONG PASSWORD!!"); } } catch (Exception e) { jOptionPane2.showMessageDialog(null, "Error" + e.getMessage()); } }
pass.equals("S")
表示您的密碼應始終為S
才能成功登錄。
您需要從數據庫中查詢,然后進行比較。
Result rs = st.executeQuery(S);
String queryPass = null;
if(rs.next()){
queryPass = rs.getString("password");
}
if (pass.equals(queryPass)) {
jOptionPane1.showMessageDialog(null, "YOU HAVE SUCCESSFULLY LOGGED IN");
MAINPAGE at = new MAINPAGE();
jDesktopPane1.add(at);
at.show();
} else {
jOptionPane1.showMessageDialog(null, "WRONG PASSWORD!!");
}
順便說一句,將參數直接傳遞到sql中是一個壞主意,您需要使用PreparedStatement而不是Statement來避免SQL注入
## 嘗試這個 ##
String email=request.getParameter("email");
String pass=request.getParameter("pass");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/users_record","root","root");
Statement st1=con.createStatement();
ResultSet obj1=st1.executeQuery("select * from registration where email='" +email+ "'");
System.out.println(email);
while(obj1.next())
{
String p2=obj1.getString(5);
if(p2.equals(pass))
{
response.sendRedirect("home1.jsp?msg=YOU HAVE SUCCESSFULLY LOGGED IN ");
}
else
{
response.sendRedirect("login1.jsp?msg=Invalid password");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.