AccountCheck 方法始终返回 false 并提示“帐户存在!”总是。想法是检查数据库中是否存在帐户,如果不存在则创建一个帐户。请帮忙。我没有在代码中进行任何验证。
private boolean AccountCheck(String username, String password) {
try {
String sql = "select count(*) from user where username=? and password=?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, username);
pst.setString(2, password);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
rs.close();
System.out.println("Account exists!");
return false;
} else {
rs.close();
System.out.println("Accout does not exist!");
return true;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
最佳答案
即使记录不存在,计数也会返回“0”作为输出。因此 rs.next() 将始终返回 true。
解决方案:
替换 select count(*) from user where username=?和密码=?
与
从用户中选择*,其中用户名=?和密码=?
如果您使用*
而不是count(*)
,它将不会返回任何记录
关于java - 无论现有帐户如何,select count(*) 都会返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36534034/