jdbc 空结果集检查不起作用
在下面的java类中,我有一个方法authenticate,其中我使用resultSet.next()
方法来检查给定的用户名和密码是否存在于数据库中,但它即使给定的用户名和密码存在于数据库中,也会返回 false。
public boolean authenticate(String userName,String password){
//db connection code
try {
String query = "select user_name from registeredUser where user_name= ? AND password = ?";
pstmt = conn.prepareStatement(query);
pstmt.setString(1, userName);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if(rs.next()) {
System.out.println("True");
return true;
}
else return false;
} catch (Exception e) {
e.printStackTrace();
return False;
} finally {
try {
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
In the following java class i'm having a method authenticate, in which i'm using resultSet.next()
method to check that whether the given userName and password exist in the database or not, but it is returning false even when the given userName and password exist in the database.
public boolean authenticate(String userName,String password){
//db connection code
try {
String query = "select user_name from registeredUser where user_name= ? AND password = ?";
pstmt = conn.prepareStatement(query);
pstmt.setString(1, userName);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if(rs.next()) {
System.out.println("True");
return true;
}
else return false;
} catch (Exception e) {
e.printStackTrace();
return False;
} finally {
try {
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您的底层 DBMS,您可能会遇到用户名和密码大小写问题。
对于大多数 DBMS,
MySecretPassword
的值与mysecretpassword
不同。因此,如果您的 DBMS 区分大小写,并且用户输入的用户名和密码与数据库中存储的不完全相同,则 SELECT 很可能会返回没有什么。
Depending on your underlying DBMS you might have a problem with the case of username and password.
For most DBMS
MySecretPassword
is a different value thanmysecretpassword
.So, in case your DBMS is case-sensitive and the user did not enter the username and password exactly the same it's stored in the database it is very likely that the SELECT returns nothing.
尝试下一个代码:
Try the next code:
在OP的评论中,你说“我在db.userName:usman,password:2中有以下值”,但你的代码说用户名字段被称为“user_name”。
用户名 != 用户名
In the comments to the OP you say "i'm having following values in the db. userName:usman,password:2" but your code says the user name field is called "user_name".
userName != user_name