我知道如果满足SQL String语句,next()将继续检查每一行。否则,如果它不能满足SQL Select语句,则为false。 (我希望我没有错过任何东西)
如果我的陈述是
ResultSet myRs1 = null;
String SQL1 = "SELECT * FROM USERS WHERE USERNAME = ? AND PASSWORD = ? AND USERTYPE = ?";
如果textfield上的用户名值与行中的用户名匹配但是密码在同一列中不匹配,我应该使用哪种方法? 此外,如果rowCounter返回false并将false计为0,我该如何比较密码?我想到了吸气剂
我想做点什么
if((usernameInTextField found in current row) BUT (passwordInTextField doesn't match password in current row))
Print message "Password Incorrect"
请耐心等待我,因为我是JDBC的新手。
这是我的代码:
ResultSet myRs1 = null;
String SQL1 = "SELECT * FROM USERS WHERE USERNAME = ? AND PASSWORD = ? AND USERTYPE = ?";
try {
myConnection = DriverManager.getConnection(dbURL,username,password);
JOptionPane.showMessageDialog(null, "Successfully Connected To Database");
PreparedStatement myPrepStmt = myConnection.prepareStatement(SQL1);
myPrepStmt.setString(1,userNameEntered); //assigns a string value to the first ?
myPrepStmt.setString(2,passwordEntered); //assigns a string value to the second ?
myPrepStmt.setString(3,userTypeEntered);
myRs1 = myPrepStmt.executeQuery(); // executes the select query and stores it to myRs
int rowCounter = 0;
while(myRs1.next()) //as long as query is satisfied, next() will return true
{
rowCounter++;
}
if(rowCounter == 0)
{
JOptionPane.showMessageDialog(this, "Username, Password or Usertype maybe incorrect");
}
else if(rowCounter > 1) //if there are duplications
{
JOptionPane.showMessageDialog(null, "User details found but has more 1 one entry" +
"\nFound: " + rowCounter + " rows" );
}
else if(rowCounter == 1){
JOptionPane.showMessageDialog(null, "Found " + rowCounter + " record");
}
} //end of try
catch (SQLException e) {
}
我的代码使用rowCounter,但无法指定哪个不正确(用户名/密码/ usertype)。
感谢。