gpt4 book ai didi

java - 准备语句错误 : java. sql.SQLException : Parameter index out of range (1 > number of parameters, 这是 0)

转载 作者:行者123 更新时间:2023-11-29 06:53:36 25 4
gpt4 key购买 nike

我目前在使用这段代码时遇到了一些问题:

 public User validateUser(String username, String password) {

boolean found = false;
Connection c = DBHelperClass.getConnection();
String query = "Select * from user where username= '?' and password= '?' ";


if (c != null) {
try {
PreparedStatement inserter = c.prepareStatement(query);
inserter.setString(1, username);
inserter.setString(2, password);
System.out.println("help: "+query);
ResultSet resultSet = inserter.executeQuery(query);
while (resultSet.next()) {
this.userId = resultSet.getInt("userId");
this.username = resultSet.getString("usrname");
this.password = resultSet.getString("password");
this.address = resultSet.getString("address");
this.email = resultSet.getString("email");
this.phone = resultSet.getInt("phone");

found = true;

}
} catch (SQLException ex) {
Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
}

}
return this;
}

我得到的错误是:

java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0)

通过阅读其他答案,我尝试改变“?”标记但未能这样做。

最佳答案

改变

 Select * from user where username= '?' and password= '?' 

Select * from user where username= ? and password= ?

无需添加'

更新:

并将 inserter.executeQuery(query); 更改为 inserter.executeQuery();

关于java - 准备语句错误 : java. sql.SQLException : Parameter index out of range (1 > number of parameters, 这是 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39894654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com