我在将Integer数组插入Postgresql表时遇到问题,我该怎么做?
String sql = "INSERT INTO draw_result (id, ball_numbers, balls_with_mega_ball, draw_dates, mega_plier) VALUES(?, ?, ?, ?, ?)";
Object[] params = {randomNumbers, ballNumbers, ballNumbersMegaBall, drawDates, megaPlier};
jdbcTemplate.update(sql, params);
其中ballNumbers和ballNumbersMegaBall是ArrayList。填写2位数字。
这是PostgreSQL表:
CREATE TABLE public.draw_result
(
id bigint NOT NULL,
draw_dates date,
ball_numbers bigint[],
balls_with_mega_ball bigint[],
mega_plier bigint,
CONSTRAINT draw_result_pkey PRIMARY KEY (id)
)
这是Springboot的错误:
出现意外错误(type = Internal Server Error,status = 500)。 PreparedStatementCallback;错误的SQL语法[INSERT INTO draw_result(id,> ball_numbers,balls_with_mega_ball,draw_dates,mega_plier)VALUES(?,?,?,?,>?)];嵌套异常是org.postgresql.util.PSQLException:无法推断用于java.util.ArrayList实例的SQL>类型。使用带有>显式类型值的setObject()来指定要使用的类型。
答案 0 :(得分:3)
最近我遇到了类似的问题。我的解决方案:
public void setDrawResult(BigInteger id, List<BigInteger> ballNumbers, List<BigInteger> ballsWithMegaBall, Date drawDates,BigInteger megaPlier){
String sql = "INSERT INTO draw_result (id, ball_numbers, balls_with_mega_ball, draw_dates, mega_plier) VALUES(?, ?, ?, ?, ?)";
jdbcTemplate.update(sql
, id
, createSqlArray(ballNumbers)
, createSqlArray(ballsWithMegaBall)
, drawDates
, megaPlier
);
}
private java.sql.Array createSqlArray(List<BigInteger> list){
java.sql.Array intArray = null;
try {
intArray = jdbcTemplate.getDataSource().getConnection().createArrayOf("bigint", list.toArray());
} catch (SQLException ignore) {
}
return intArray;
}