我正在使用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 [update acotel_msp.users_mau set denied_sub_client = ? where id_users = ? and id_mau = ?]; The column index is out of range: 4, number of columns: 3.; nested exception is org.postgresql.util.PSQLException: The column index is out of range: 4, number of columns: 3.
Caused by: org.postgresql.util.PSQLException: The column index is out of range: 4, number of columns: 3.
我查看了Spring JDBC模板文档,但没有找到任何帮助,我会继续寻找,无论如何,能否有人指导我正确的方向?谢谢!
编辑:
显然顺序错了,是我的错...
我尝试了你们两个的解决方案,第一种情况下我得到了这个:
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [update users set denied_sub_client = ? where id_users = ? and id_device = ?]; nested exception is org.postgresql.util.PSQLException: Cannot cast an instance of java.util.ArrayList to type Types.ARRAY
尝试第二种解决方案时,我得到了这个:
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [update users set denied_sub_client = ? where id_users = ? and id_device = ?]; nested exception is org.postgresql.util.PSQLException: Cannot cast an instance of [Ljava.lang.Object; to type Types.ARRAY
我想我需要一个java.sql.Array的实例,但是我该如何使用JdbcTemplate创建它呢?
varchar(10)[]
,所以我必须将varchar
作为第二个参数传递。 - Frederic Leitenberger