I already know that PostgreSQL 9.5 and newer support INSERT ... ON CONFLICT UPDATE
, i.e. upsert. Although, I can't afford to upgrade version I have.
So i try this in pgAdmin:
do $$
begin
insert into test(id,description,name) values(10,'','road');
exception when unique_violation then
update function set name = 'newRoad',description='yes' where id =10;
end $$;
It works but when I try to integrate this with jdbc template, I get :
java.lang.ArrayIndexOutOfBoundsException: null
by doing this:
sql.append("DO $$ BEGIN INSERT INTO test(id,description,NAME) VALUES(?,?,?)");
sql.append(" exception when unique_violation then UPDATE SET description=?,");
sql.append(" name=? where id=?");
try {
jdbcTemplate.update(sql.toString(), new Object[]{id,description,name,description,name,id});
}
I don't know how to handle parameters with JDBC template request with 'exception when'.