我正在制作一个应用程序,尝试使用存储在 MySQL 服务器中的数据登录用户。 我遇到的问题是,当我尝试使用正确的用户名和密码登录时,我的代码实际上是检查“adminData”表中的每条记录,并向我显示每条记录的相应结果! 我也附上了我的代码,请看一下! 所以我想问我如何只能搜索一个特定的记录,并且它只会将我恢复为该特定的记录! 请帮帮我..:)
btnSubmit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String query = "SELECT username, password FROM adminData";
String username = null;
String password = null;
;
try
{
rs = st.executeQuery(query);
String fieldWarning = "*all field are mandatory!";
String enteredUsername = usernameField.getText();
@SuppressWarnings("deprecation") String enteredPassword = passwordField.getText();
usernameField.setCursor(null);
fieldLabel.setText("");
int userLen = enteredUsername.length();
int passLen = enteredPassword.length();
if (userLen != 0 && passLen != 0)
{
if (userLen <= 32 && passLen <= 32)
{
while (rs.next())
{
username = rs.getString("username");
password = rs.getString("password");
if (enteredUsername.equals(username))
{
if (enteredPassword.contentEquals(password))
{
AdminDashboard frame = new AdminDashboard();
frame.setVisible(true);
frame.dispose();
}
else
{
JOptionPane.showMessageDialog(null, "Password Missmatch");
}
}
else
{
JOptionPane.showMessageDialog(null, "Invalid User");
passwordField.setText("");
usernameField.setText("");
usernameField.setCursor(null);
}
}
}
else
{
JOptionPane.showMessageDialog(null,
"Please enter a valid username or password! \n(Note: Only 32 char MAX)");
passwordField.setText("");
usernameField.setText("");
}
}
else
{
fieldLabel.setText(fieldWarning);
usernameField.setCursor(null);
}
}
catch (SQLException e1)
{
JOptionPane.showMessageDialog(null, "Database Error: " + e1);
}
}
});
最佳答案
这是因为您的查询返回整个表。如果您有用户名,您可以简单地调整查询以仅返回包含该特定用户详细信息的行,而不是所有用户。 因此,只需在查询之前分配enteredUsername,并将用户名条件添加到您的sql查询中即可。
btnSubmit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String enteredUsername = usernameField.getText();
String query = "SELECT username, password FROM adminData WHERE username='"+enteredUsername+"'";
String username = null;
String password = null;
;
try
{
rs = st.executeQuery(query);
String fieldWarning = "*all field are mandatory!";
@SuppressWarnings("deprecation") String enteredPassword = passwordField.getText();
usernameField.setCursor(null);
fieldLabel.setText("");
int userLen = enteredUsername.length();
int passLen = enteredPassword.length();
if (userLen != 0 && passLen != 0)
{
if (userLen <= 32 && passLen <= 32)
{
while (rs.next())
{
username = rs.getString("username");
password = rs.getString("password");
if (enteredUsername.equals(username))
{
if (enteredPassword.contentEquals(password))
{
AdminDashboard frame = new AdminDashboard();
frame.setVisible(true);
frame.dispose();
}
else
{
JOptionPane.showMessageDialog(null, "Password Missmatch");
}
}
else
{
JOptionPane.showMessageDialog(null, "Invalid User");
passwordField.setText("");
usernameField.setText("");
usernameField.setCursor(null);
}
}
}
else
{
JOptionPane.showMessageDialog(null,
"Please enter a valid username or password! \n(Note: Only 32 char MAX)");
passwordField.setText("");
usernameField.setText("");
}
}
else
{
fieldLabel.setText(fieldWarning);
usernameField.setCursor(null);
}
}
catch (SQLException e1)
{
JOptionPane.showMessageDialog(null, "Database Error: " + e1);
}
}
});
现在,只要您的用户名是唯一的,它就会始终返回一条记录。
关于java - 如何使用Java仅搜索MySQL表中的一条特定记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55194019/