jdbc 空结果集检查不起作用

发布于 2024-11-08 00:53:28 字数 876 浏览 0 评论 0原文

在下面的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅浅 2024-11-15 00:53:28

根据您的底层 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 than mysecretpassword.

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.

陪你搞怪i 2024-11-15 00:53:28

尝试下一个代码:

public boolean authenticate(String userName,String password){
//db connection code
    try {
      String query = "select count(*) from registeredUser where user_name= ? AND password = ?";
      pstmt = conn.prepareStatement(query); 
          pstmt.setString(1, userName);
      pstmt.setString(2, password);
      rs = pstmt.executeQuery();
      rs.next();
      int count = rs.getInt(1);
      if(count != 0) {  
            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();
      }
    }
}

Try the next code:

public boolean authenticate(String userName,String password){
//db connection code
    try {
      String query = "select count(*) from registeredUser where user_name= ? AND password = ?";
      pstmt = conn.prepareStatement(query); 
          pstmt.setString(1, userName);
      pstmt.setString(2, password);
      rs = pstmt.executeQuery();
      rs.next();
      int count = rs.getInt(1);
      if(count != 0) {  
            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();
      }
    }
}
小镇女孩 2024-11-15 00:53:28

在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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文