ReadyStatement - 不绑定所有参数

发布于 2024-10-04 07:36:22 字数 1508 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

婴鹅 2024-10-11 07:36:22

据我所知,对 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文