[英]Username and Password not Matching Error Messages in Java
The below form , retrieves MySQL database username and password and check them with user's input. 下面的表格检索MySQL数据库的用户名和密码,并通过用户输入进行检查。
Error messages on password or username incorrect is not showing. 密码或用户名不正确的错误消息未显示。 I tried many methods but not working as expected.
我尝试了许多方法,但未按预期工作。
How can I code that functionality? 我该如何编码该功能?
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);
}
}
you can do it using one query by passing 2 params 您可以通过传递2个参数使用一个查询来完成
"SELECT username FROM logininfo WHERE username = '"+val1+"'
and password = '"+val2+"' " ;
You need to take care of sql injection as well..Pass the parameters using preparedstatement and bind them to the query. 您还需要注意sql注入。.使用preparestatement传递参数并将其绑定到查询。
In your case 就你而言
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..
remove the two }}
at the end and try again... 最后删除两个
}}
,然后重试...
我有这样的问题,我解决了从安全文件中删除md5加密的问题,但是也许您的数据库使用了md5加密,而您的应用程序却不尝试这样做
Make user that String val2 = passwordtxt.getText();
使用户使用
String val2 = passwordtxt.getText();
is not returning an encrypted value. 没有返回加密值。 Java password field might not be returning the plain text value.
Java密码字段可能未返回纯文本值。
You might need to look up the user by username AND password: 您可能需要通过用户名和密码来查找用户:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.