【问题标题】:How Do I Pass an Array of Strings Query String with the JdbcTemplate Query For Object Function?如何使用 JdbcTemplate 查询对象函数传递字符串数组查询字符串?
【发布时间】:2014-04-11 06:53:51
【问题描述】:

我想使用 jdbcTemplate 将一个字符串数组传递给一个存储过程,但在这样做时遇到了麻烦。这是查询:

"SELECT * from stored_procedure(?::text[])"

这就是我使用 jdbcTemplate 调用存储过程的方式(其中 notes 是一个列表):

jdbcTemplate.queryForObject(sql, Long.class, notes == null ? null : notes.toArray());

这是我得到的错误:

PreparedStatementCallback; bad SQL grammar [SELECT * from stored_procedure(?::text[])]; nested exception is org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of [Ljava.lang.Object;. Use setObject() with an explicit Types value to specify the type to use.

我没有在网上找到任何解决此问题的方法。

【问题讨论】:

标签: java arrays spring parameter-passing jdbctemplate


【解决方案1】:

您可以使用支持命名参数绑定(使用:foo 而不是?)和集合参数扩展(将:foo 扩展为?, ?, ?)的NamedParameterJdbcTemplate。然后你可以使用:

jdbcTemplate.queryForObject("SELECT * FROM stored_procedure({:notes})", 
    Collections.singletonMap("notes", notes));

我认为 PostgreSQL JDBC 驱动程序不支持数组参数类型(即您不能将数组绑定到单个 ?)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-23
    • 2022-11-04
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    相关资源
    最近更新 更多