0

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'.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
John
  • 185
  • 1
  • 6
  • 21
  • http://stackoverflow.com/q/17267417/330315 –  Jan 20 '17 at 13:47
  • thank you but I had already read this post. My request works good but my problem is to handle parameters with jdbc template because I can not upgrade my postgres version. – John Jan 20 '17 at 13:52
  • You don't need to upgrade your Postgres version. See e.g. here: http://stackoverflow.com/a/8702291/330315 –  Jan 20 '17 at 13:54

0 Answers0