【发布时间】:2020-10-10 02:17:06
【问题描述】:
我的 url 中有一个查询参数列表,我想在我的 java web 服务中使用它们来对表运行查询
public Set<Result> getResult(String query, List<String> sortedQueryParamsValue) {
Connection connection = getConnection();//jdbc connection
//query is some thing like: select * from table A where status = ? and Id = ?
try (PreparedStatement getStatement = connection.prepareStatement(query)) {
for (int i = 0; i <sortedQueryParamKeys.size(); i++) {// sortedQueryParamsValue length is matching the number of values I need for the query and the order matches the order I am expecting
String value = sortedQueryParamsValue.get(i);
getStatement.setString(1, value);
}
try (ResultSet rs = getStatement.executeQuery()) {
while (rs.next()) {
//add to the list of results
}
}
//return the resultset
}
我总是在 getStatement.setString(1, value); 中使用 1 的原因是,我认为在每次迭代中,一个 ? 被替换为该值,但最后我得到一些异常说 java.sql.SQLException: IN or OUT位置 2 处缺少参数。
有谁知道我在这里做错了什么?
【问题讨论】:
-
将
getStatement.setString(1, value);更改为getStatement.setString(i+1, value); -
通过您的编辑,您使您的问题无效,并使现有答案错误。我已回滚您的编辑。