gpt4 book ai didi

java - 如何使用 JdbcTemplate 查询对象函数传递字符串数组查询字符串?

转载 作者:行者123 更新时间:2023-11-30 09:11:06 25 4
gpt4 key购买 nike

我想使用 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.

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

最佳答案

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

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

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

关于java - 如何使用 JdbcTemplate 查询对象函数传递字符串数组查询字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22261730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com