下面的表格檢索MySQL數據庫的用戶名和密碼,並用用戶輸入檢查它們。用戶名和密碼不匹配Java中的錯誤消息
未顯示密碼或用戶名錯誤消息。我嘗試了很多方法,但沒有按預期工作。
如何編碼該功能?
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);
}
}
它的工作..我添加了錯誤信息顯示空條目..感謝您的幫助:) @Adriaan科斯特 – tenten