ResultSet.next()行计数器计数失败
ResultSet.next() row counter fails to count
我想弄清楚为什么当我输入 "ashton" 作为用户名和 "ashton" 作为密码时,这不会计数并显示行数:2。在我的数据库中,我插入了 2 个用户名和密码条目。
下面是table的截图:
这是 GRAB 文件:
这是我的代码:
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
}
}
在我的理解中,next() returns 如果 SELECT 查询成功或者当游标被 next() 移动时有行则为真。我需要能够计算行数以显示有超过 1 行拥有相同的用户名和密码。我无法继续制作另一个 ifelse 来计算用户名和密码的重复,因为在我的代码中,它似乎不计算 2 行。
如有任何帮助,我将不胜感激。
谢谢。
这是我得到的输出,
这就是我所做的,并且奏效了。谢谢大家的建议!它帮助我了解更多。
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");
}
主要问题是您在获取数据之前调用了两次 myRs.next()
。
您可以使用
myRs.isBeforeFirst()
如所述here
或按照说明使用此模板here
if (!myRs.next() ) {
System.out.println("no data");
} else {
do {
//statement(s)
} while (myRs.next());
}
而且您根本不需要循环 — 只需使用带有计数
的 SQL 请求
SELECT COUNT(*) FROM USERS WHERE USERNAME = ? AND PASSWORD = ?
我想弄清楚为什么当我输入 "ashton" 作为用户名和 "ashton" 作为密码时,这不会计数并显示行数:2。在我的数据库中,我插入了 2 个用户名和密码条目。
下面是table的截图:
这是 GRAB 文件:
这是我的代码:
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
}
}
在我的理解中,next() returns 如果 SELECT 查询成功或者当游标被 next() 移动时有行则为真。我需要能够计算行数以显示有超过 1 行拥有相同的用户名和密码。我无法继续制作另一个 ifelse 来计算用户名和密码的重复,因为在我的代码中,它似乎不计算 2 行。
如有任何帮助,我将不胜感激。
谢谢。
这是我得到的输出,
这就是我所做的,并且奏效了。谢谢大家的建议!它帮助我了解更多。
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");
}
主要问题是您在获取数据之前调用了两次 myRs.next()
。
您可以使用
myRs.isBeforeFirst()
如所述here
或按照说明使用此模板here
if (!myRs.next() ) {
System.out.println("no data");
} else {
do {
//statement(s)
} while (myRs.next());
}
而且您根本不需要循环 — 只需使用带有计数
的 SQL 请求SELECT COUNT(*) FROM USERS WHERE USERNAME = ? AND PASSWORD = ?