java - ResultSet.next()行计数器计数失败

标签 java swing jdbc derby resultset

我试图弄清楚为什么当我输入“ashton”作为用户名和“ashton”作为密码时,这不会计数并显示 Rows: 2。在我的数据库中,我插入了 2 个用户名和密码条目。

这是表格的屏幕截图:

enter image description here

这是 GRAB 文件:

enter image description here

这是我的代码:

private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    String userNameEntered = userNameTxtField.getText().trim();
    String passwordEntered = passwordTxtField.getText().trim();

    if(userNameEntered.isEmpty() || passwordEntered.isEmpty()){
        JOptionPane.showMessageDialog(this, "Please fill out all fields");
    }

    else{
    String username = "jordan";
    String password = "jordan";
    String dbURL = "jdbc:derby://localhost:1527/JDBCSTUDY";
    Connection myConnection = null;
    ResultSet myRs = null;
    String SQL = "SELECT * FROM USERS WHERE USERNAME = ? AND PASSWORD = ?";


    try {
      myConnection = DriverManager.getConnection(dbURL,username,password);
        JOptionPane.showMessageDialog(null, "Successfully Connected To Database");

        PreparedStatement myPrepStmt = myConnection.prepareStatement(SQL,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            myPrepStmt.setString(1,userNameEntered); //assigns a string value to the first ?
            myPrepStmt.setString(2,passwordEntered); //assigns a string value to the second ?
        myRs = myPrepStmt.executeQuery(); // executes the select query and stores it to myRs


        if(myRs.next() == false){//next() method returns true if the select statement is satisfied or if query is valid
            JOptionPane.showMessageDialog(this, "Not found");
        }

        int countRows = 0;
        while(myRs.next()){
            countRows++;
            if((myRs.getString(2).equals(userNameEntered))
                && (myRs.getString(3).equals(passwordEntered))){
                JOptionPane.showMessageDialog(this,"found" +"\nRows: " + countRows );
            }
        }


    } //end of try
    catch (SQLException e) {
        //if an exception or an error even occured while executing the try{} block, the 3 lines will be printed
        System.err.println("Error message: " + e.getMessage());
        System.err.println("Error Code: " + e.getErrorCode());
        System.err.println("SQL State: " + e.getSQLState());
    }

    finally{
        if(myConnection!=null){
            try {

                myConnection.close();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null,"Error encountered: " + ex.toString());
            }
        }//end of if   
    }//end of finally
}
} 

根据我的理解,如果 SELECT 查询成功或者 next() 移动光标时有行,则 next() 返回 true。我需要能够计算行数以显示有超过 1 行持有相同的用户名和密码。我无法继续制作另一个 ifelse 来计算用户名和密码的重复次数,因为在我的代码中,它似乎没有计算 2 行。

如果有任何帮助,我将不胜感激。

谢谢。

这是我得到的输出, enter image description here

这就是我所做的,并且有效。谢谢你们的建议!它帮助我了解更多。

int countRows = 0;
        while(myRs.next()){
            countRows++;
        }

        if(countRows == 0)
        {
            JOptionPane.showMessageDialog(this, "User details doesn't exist. \n Please register first");
        }
        else if(countRows > 1) //if there are duplications 
        {
            JOptionPane.showMessageDialog(null, "User details found but has more 1 one entry" +
                    "\nFound: " + countRows + " users" );
        }
        else if(countRows == 1){
            JOptionPane.showMessageDialog(null, "User Found");
        }

最佳答案

您的错误是调用 rs.next 两次:每次调用 next 时,您都会隐式丢弃光标的最后状态。每次调用 next读取结果集的列是一个很好(而且更清晰)的做法。

就您而言,只需在 while 循环之后移动 if 即可更改条件:

    int countRows = 0;
    while(myRs.next()){
        countRows++;
        ...
    }

    if (countRows==0)
    {
        JOptionPane.showMessageDialog(this, "Not found");
    }

关于java - ResultSet.next()行计数器计数失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35297374/

相关文章:

java - GridBagLayout 为什么第三列更大?

Java While 循环检查时间但不执行方法

Java JDBC 调用 Oracle 10 函数 "invalid identifier"

java - 两个类来扩展一项 Activity ?

java - 我的无效字符在哪里 (ORA-00911)

java - 每次做出错误的猜测时,我都需要让我的程序向 GUI 添加一个 .jpg。

java - 如何测试 ArrayList<String> 中的某个项目是否位于某个位置

tomcat - 数据库连接数大于 DBCP maxTotal 设置的数

java - MySQL 连接不适用于 HikariCP

java - 如何使用 Java Optional 优雅地替换三元运算符