【发布时间】:2019-03-28 20:53:40
【问题描述】:
我正在使用 Spring JdbcTemplate,但我遇到了一个查询,该查询更新了一个实际上是 int 数组的列。数据库是 postgres 8.3.7。 这是我正在使用的代码:
public int setUsersArray(int idUser, int idDevice, Collection<Integer> ids) {
int update = -666;
int[] tipi = new int[3];
tipi[0] = java.sql.Types.INTEGER;
tipi[1] = java.sql.Types.INTEGER;
tipi[2] = java.sql.Types.ARRAY;
try {
update = this.jdbcTemplate.update(setUsersArrayQuery, new Object[] {
ids, idUser, idDevice }, tipi);
} catch (Exception e) {
e.printStackTrace();
}
return update;
}
查询是“update table_name set array_column = ? where id_user = ? and id_device = ?”。 我得到了这个例外:
org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [更新 acotel_msp.users_mau 设置 denied_sub_client = ?哪里 id_users = ?和 id_mau = ?];列索引超出范围:4,列数:3。嵌套异常是 org.postgresql.util.PSQLException:列索引超出范围:4,列数:3。
原因:org.postgresql.util.PSQLException:列索引超出范围:4,列数:3。
我已经查看了 spring jdbc 模板文档,但找不到任何帮助,我会继续寻找,无论如何有人能指出我正确的方向吗?谢谢!
编辑:
显然顺序错了,是我的错……
我尝试了你的两种解决方案,在第一种情况下我有这个:
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback;错误的 SQL 语法 [更新用户集 denied_sub_client = ?哪里 id_users = ?和 id_device = ?];嵌套异常是 org.postgresql.util.PSQLException: Cannot cast an instance of java.util.ArrayList to type Types.ARRAY
尝试第二种解决方案我有这个:
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback;错误的 SQL 语法 [更新用户集 denied_sub_client = ?哪里 id_users = ?和 id_device = ?];嵌套异常是 org.postgresql.util.PSQLException: Cannot cast an instance of [Ljava.lang.Object;键入 Types.ARRAY
我想我需要一个 java.sql.Array 的实例,但是如何使用 JdbcTemplate 创建它?
【问题讨论】:
标签: arrays spring postgresql jdbctemplate