我想检查一个表username
中是否已经存在。database
Java 代码:
if (e.getSource() == jButton2)
{
U = jTextField1.getText();
if (jTextField1.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(this, "Please Write Username !");
}
else if (jPasswordField1.getText().equals(""))
{
JOptionPane.showMessageDialog(this, "Please Write Password !");
}
else
{
try {
String Driver = "com.mysql.jdbc.Driver";
String URL = "jdbc:mysql://localhost:3306/LoginForm";
Class.forName(Driver);
Connection Conn = DriverManager.getConnection(URL, "root", "12345");
Statement S = Conn.createStatement();
ResultSet RS = S.executeQuery(
"SELECT * FROM login where username ='" + U +
"' and Password ='" + jPasswordField1.getText() + "'");
while (RS.next()) {
String Username = RS.getString("Username");
String Password = RS.getString("Password");
String Admin = RS.getString("Admin");
if (Username.equals(U) & Password.equals(jPasswordField1.getText()) &
Admin.equals("0"))
{
JOptionPane.showMessageDialog(this, "Logged In Successfully !");
this.dispose();
NormalUsers NU = new NormalUsers();
break;
}
else if (Username.equals(U) &
Password.equals(jPasswordField1.getText()) & Admin.equals("1"))
{
JOptionPane.showMessageDialog(this,
"Logged In Successfully , Opening Administration Panel !");
this.dispose();
AdminPanel AP = new AdminPanel();
break;
}
else
{
JOptionPane.showMessageDialog(this, "Invalid Username Or Password!");
}
}
}
catch (SQLException | ClassNotFoundException ex)
{
}
}
}
问题是:
- 如果 中没有行
database
,则没有错误消息:Invalid Username or Password
。 SQL
当我手动执行查询时:SELECT * From Login
我看到那里存在该行,但由于我过滤password
,它没有进入while loop
.
我应该如何使用 java 检查用户名是否存在于 MySQL 表中?