【发布时间】:2019-04-04 12:58:26
【问题描述】:
如果数据库中存在该行,我正在尝试验证用户名和密码。但是我让参数索引超出范围(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");
}
【问题讨论】:
-
PreparedStatement 查询语法不是这样工作的。请参阅 API。
-
单独传递参数的要点是您不要将它们直接粘贴到 SQL 中。
-
试试这个:
select LastName,BirthDate from employees where LastName=? and BirthDate=?.