2015-06-26 82 views
1

下面的表格檢索MySQL數據庫的用戶名和密碼,並用用戶輸入檢查它們。用戶名和密碼不匹配Java中的錯誤消息

未顯示密碼或用戶名錯誤消息。我嘗試了很多方法,但沒有按預期工作。

如何編碼該功能?

enter image description here

private void mysettingChangebtnActionPerformed(java.awt.event.ActionEvent evt) {             

    String val1 = usernametxt.getText(); 
    String val2 = passwordtxt.getText(); 

    if(val1.equals("") || val2.equals("")){ 
      JOptionPane.showMessageDialog(null, "Fill all fields and try again .... "); 
      usernametxt.setText(""); 
      passwordtxt.setText(""); 


    } 

    try{ 

     String sql1 = "SELECT username FROM logininfo WHERE username LIKE '"+val1+"'" ; 
     String sql2 = "SELECT password FROM logininfo WHERE password LIKE '"+val2+"'" ; 

     pst1 = conn.prepareStatement(sql1); 
     pst2 = conn.prepareStatement(sql2); 


     rs1 = pst1.executeQuery(); 
     rs2 = pst2.executeQuery(); 

     while(rs1.next()){ 
      String uname=rs1.getString("username"); 
      //System.out.println(uname); 

     while(rs2.next()){ 
      String pwd=rs2.getString("password"); 
      //System.out.println(pwd); 



     if(val1.equals(uname) && val2.equals(pwd)){ 

      chg2.setVisible(true); 
      this.setVisible(false); 
     } else{ 
      JOptionPane.showMessageDialog(null, "Login Information is Incorrect. Try Again.... "); 
     } 

     if(!(val1.equals(uname)) || !(val2.equals(pwd))){ 
      JOptionPane.showMessageDialog(null, "Login Information is Incorrect. Try Again.... "); 


       } 
     }} 




    }catch(SQLException | HeadlessException e){ 
     JOptionPane.showMessageDialog(null, "errrrrr"+e); 

    } 

} 

回答

1

您可能需要通過用戶名和密碼來查找用戶:

String username = usernametxt.getText(); 
String password = passwordtxt.getText(); 
// avoid SQL injection by setting query parameters with '?' 
String sql = "SELECT * FROM logininfo WHERE username = ? AND password = ?"; 
PreparedStatement st = conn.prepareStatement(sql); 
st.setString(1, username); 
st.setString(2, password); 
ResultSet rs = st.executeQuery();  
if (!rs.next()) { 
    // no records found, login failed 
    JOptionPane.showMessageDialog(null, "Login Information is Incorrect."); 
} 
else { 
    // record found, login succeeded 
    // assuming here that there is a unique constraint in the database 
    // on (username, password), otherwise multiple records could be found 
    chg2.setVisible(true); 
    this.setVisible(false); 
} 
+0

它的工作..我添加了錯誤信息顯示空條目..感謝您的幫助:) @Adriaan科斯特 – tenten

1

我有問題這樣的,我解決了磨片我從安全文件中刪除MD5加密,但也許你的數據庫使用MD5加密和您的應用程序不嘗試一下這

1

製作用戶String val2 = passwordtxt.getText();未返回加密值。 Java密碼字段可能不會返回純文本值。

2

你可以通過2個PARAMS

"SELECT username FROM logininfo WHERE username = '"+val1+"' 
    and password = '"+val2+"' " ; 

你需要使用PreparedStatement的照顧SQL注入作爲well..Pass的參數,並將其綁定到查詢使用一個查詢它做。

在你的情況

while(rs1.next()){ 
     String uname=rs1.getString("username"); 
     //System.out.println(uname); 
    } // <-- close it... 

     while(rs2.next()){ 
     String pwd=rs2.getString("password"); 
     //System.out.println(pwd); 
    } <--- close it.. 

卸下兩個}}末,然後再試一次......

+0

最好使用「?」在PreparedStatements上設置參數和setXXX()方法來避免SQL注入漏洞。 –

+0

我在答案中提到過,請@AdriaanKoster –

+0

哦,是的,你說得對。但爲什麼不在你的代碼中顯示正確的方式呢? –

相關問題