如果数据库中存在该行,我正在尝试验证用户名和密码。但是我将参数索引超出范围(1>参数数量,即0)。请帮助
Scanner in = new Scanner(System.in);
String password = in .next();
String username = in .next();
PreparedStatement ps =
connect.prepareStatement
( "select LastName,BirthDate from employees where LastName= '" + username + "' and BirthDate = '" + password + "'" );
ps.setString (1, username);
ps.setString (2, password);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
System.out.println("login completed");
} else {
// Quest not completed yet
System.out.println("login failed");
}
答案 0 :(得分:1)
您正在将字符串串联与设置准备好的语句参数混合在一起。您应该仅使用两者之一,最好是后者。您的代码应如下所示:
PreparedStatement ps = connect.prepareStatement (
"select LastName,BirthDate from employees where LastName=? and BirthDate=?"
);
ps.setString (1, username);
ps.setString (2, password);
ResultSet rs = ps.executeQuery();