ResultSet.next()行计数器无法计算

时间:2016-02-09 16:38:53

标签: java swing jdbc derby resultset

我正在试图找出为什么这不会计算并显示Rows:2当我输入“ashton”用户名和“ashton”输入密码时。在我的数据库中,我插入了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,则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");
        }

2 个答案:

答案 0 :(得分:3)

您的错误是致电rs.next两次:每次拨打next时,您都隐含丢弃光标的最后状态。每次调用next后,读取结果集的列是一种很好的(也是更清晰的)练习。

在您的情况下,只需在if循环之后移动while,即可更改条件:

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

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

答案 1 :(得分:2)

主要问题是在获取数据之前两次调用git diff HEAD 'HEAD@{7 days ago}'。 你可以使用

myRs.next()

here

所述

或按照here

所述使用此模板
myRs.isBeforeFirst()

您根本不需要循环 - 只需使用带有计数的SQL请求

if (!myRs.next() ) {
    System.out.println("no data");
} else {

    do {
     //statement(s)
    } while (myRs.next());
}