准备好的语句错误:java.sql.SQLException:参数索引超出范围(1>参数数,为0)
我目前对此代码有一些问题:准备好的语句错误:java.sql.SQLException:参数索引超出范围(1>参数数,为0),java,sql-server,prepared-statement,Java,Sql Server,Prepared Statement,我目前对此代码有一些问题: 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 != n
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:参数索引超出范围1>数量
参数,即0
通过阅读其他答案,我尝试改变“?”标签,但失败。改变
Select * from user where username= '?' and password= '?'
到
无需添加'
更新:
并更改inserter.executeQueryquery;插入执行器 变化
Select * from user where username= '?' and password= '?'
到
无需添加'
更新:
并更改inserter.executeQueryquery;插入执行器 错误消息表示查询中没有参数。这是因为您使用撇号转义了问号字符。 重写查询,不要在问号周围使用撇号
String query = "Select * from user where username= ? and password= ?";
错误消息表示查询中没有参数。这是因为您使用撇号转义了问号字符。 重写查询,不要在问号周围使用撇号
String query = "Select * from user where username= ? and password= ?";
java.sql.SQLException:参数索引超出范围1>数量
参数,即0
之所以出现此错误,是因为您调用了executeQuery而不是executeQuery,因此它认为您的查询不需要任何参数,但您提供了多个导致此异常的参数,所以只需使用inserter.executeQuery即可
前面提到的第二个错误是,您不需要在查询中使用引号,它只应该是Select*from user,其中username=?密码=
java.sql.SQLException:参数索引超出范围1>数量
参数,即0
之所以出现此错误,是因为您调用了executeQuery而不是executeQuery,因此它认为您的查询不需要任何参数,但您提供了多个导致此异常的参数,所以只需使用inserter.executeQuery即可
前面提到的第二个错误是,您不需要在查询中使用引号,它只应该是Select*from user,其中username=?密码=?谢谢大家的帮助这是固定代码:
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();
while (resultSet.next()) {
this.userId = resultSet.getInt("userId");
this.username = resultSet.getString("username");
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;
}
我所需要改变的只是四处移动?并将executeQuery更改为executeQuery
万分感谢 感谢大家的帮助这是固定代码:
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();
while (resultSet.next()) {
this.userId = resultSet.getInt("userId");
this.username = resultSet.getString("username");
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;
}
我所需要改变的只是四处移动?并将executeQuery更改为executeQuery
万分感谢 @Pavol更新了我的答案,如果你还有任何问题..在这里查看代码@Pavol更新了我的答案,如果你还有任何问题..在这里查看代码