我现在正在创建一个登录页面,但是每次我尝试“登录”时,都会收到“您的用户名和密码不匹配”消息,即使用户名和密码有效并存储在数据库中也是如此。我什至无法从其他在线资源中弄清楚这一点,因为据我所知它应该工作吗?
private void jButtonLoginActionPerformed(java.awt.event.ActionEvent evt)
{
Connection con = myConnection.getConnection();
PreparedStatement pscon;
ResultSet rscon;
try
{
pscon = con.prepareStatement("SELECT * dbname.user WHERE 'username' = ? AND 'password' = ?");
pscon.setString(1, jTextFieldUsername.getText());
pscon.setString(2, String.valueOf(jPasswordFieldPass.getPassword()));
rscon = pscon.executeQuery();
if(rscon.next())
{
JOptionPane.showMessageDialog(null, "Logged in successfully!");
}
else
{
JOptionPane.showMessageDialog(null, "Your username and password don't match!");
}
}
catch (SQLException ex)
{
Logger.getLogger(LoginPage.class.getName()).log(Level.SEVERE, null, ex);
}
}
在此先感谢您的帮助!
答案 0 :(得分:2)
您的查询是错误的。
SELECT * dbname.user WHERE 'username' = ? AND 'password' = ?
应该是
SELECT * FROM dbname.user WHERE 'username' = ? AND 'password' = ?
您错过了“ FROM”。此外,您不应存储未加密和未隐藏的密码。
答案 1 :(得分:1)
您的sql写不正确,您需要删除'
和username
的冗余password
并添加FROM
关键字
所以改变
pscon = con.prepareStatement("SELECT * dbname.user WHERE 'username' = ? AND 'password' = ?");
到
pscon = con.prepareStatement("SELECT * FROM dbname.user WHERE username = ? AND password = ?");
或添加`以包装列名
pscon = con.prepareStatement("SELECT * FROM dbname.user WHERE `username` = ? AND `password` = ?");