ReadyStatement - 不绑定所有参数
我正在使用 Spring-JDBC 执行 SQL 并获取结果。
SELECT
SUM(spend) total_sum
FROM TABLE_NAME
WHERE
sup_id = ? AND
( ( YEAR = ? AND period IN ( ? ) ) OR
( YEAR = ? AND period IN(?)))
绑定变量传递为:
final Object[] paramMap = { "1234", "2010",
"'01_JAN','02_FEB','03_MAR'", "2009", "'11_NOV','12_DEC'" };
final List<LeadTimeDto> query = getJdbcTemplate().query(sql, paramMap,
new RowMapper<LeadTimeDto>() {
@Override
public LeadTimeDto mapRow(final ResultSet resultSet,
final int arg1) throws SQLException {
final LeadTimeDto leadTimeDto = new LeadTimeDto();
final String string = resultSet.getString("total_sum");
System.out.println("ltime = " + string);
leadTimeDto.setLeadTime(string);
return leadTimeDto;
}
});
我不确定这里发生了什么。我遇到了第三个参数的绑定问题。 如果我在查询本身中写入第三个参数的值,如下所示,它就可以工作。
SELECT
SUM(spend) total_sum
FROM TABLE_NAME
WHERE
sup_id = ? AND
( ( YEAR = ? AND period IN ( '01_JAN','02_FEB','03_MAR' ) ) OR
( YEAR = ? AND period IN(?)))
如果是引号(')的问题,那么第五个参数也应该是问题。但它绑定得很好。
我尝试过将 getNamedParameterJdbcTemplate() 与 Map 和 Bean 一起使用,但没有成功。
I'm using Spring-JDBC to execute the SQL and fetch the results.
SELECT
SUM(spend) total_sum
FROM TABLE_NAME
WHERE
sup_id = ? AND
( ( YEAR = ? AND period IN ( ? ) ) OR
( YEAR = ? AND period IN(?)))
And the bind variables are passed as:
final Object[] paramMap = { "1234", "2010",
"'01_JAN','02_FEB','03_MAR'", "2009", "'11_NOV','12_DEC'" };
final List<LeadTimeDto> query = getJdbcTemplate().query(sql, paramMap,
new RowMapper<LeadTimeDto>() {
@Override
public LeadTimeDto mapRow(final ResultSet resultSet,
final int arg1) throws SQLException {
final LeadTimeDto leadTimeDto = new LeadTimeDto();
final String string = resultSet.getString("total_sum");
System.out.println("ltime = " + string);
leadTimeDto.setLeadTime(string);
return leadTimeDto;
}
});
I'm not sure what is happening here. I've the problem with the binding of the THIRD parameter.
If I write the value of the third parameter in the query itself as below it is working.
SELECT
SUM(spend) total_sum
FROM TABLE_NAME
WHERE
sup_id = ? AND
( ( YEAR = ? AND period IN ( '01_JAN','02_FEB','03_MAR' ) ) OR
( YEAR = ? AND period IN(?)))
If it is the problem with quotes(') then FIFTH param should have also been the issue. But it is binding fine.
I've tried using getNamedParameterJdbcTemplate() as well both with Map and Bean, but no luck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,对 IN 子句使用准备好的语句参数是不合法的。
请参阅以下文章,其中描述了一些替代方案:
http://www.javaranch.com /journal/200510/Journal200510.jsp#a2
Using prepared statement parameters for IN clause is not legal as far as I know.
See the following article, which describes some alternatives:
http://www.javaranch.com/journal/200510/Journal200510.jsp#a2