Re: JDBC behaviour - Mailing list pgsql-jdbc

From Thomas Kellerer
Subject Re: JDBC behaviour
Date
Msg-id nabpk4$4sc$1@ger.gmane.org
Whole thread Raw
In response to Re: JDBC behaviour  (Sridhar N Bamandlapally <sridhar.bn1@gmail.com>)
List pgsql-jdbc
Sridhar N Bamandlapally schrieb am 21.02.2016 um 03:20:
> Hi
>
> My expectation is simple, please refer below
>
> create table employee(empid numeric(4) primary key, ename varchar(20));
>
> from Java/jdbc code, conn.setAutoCommit(false)
>
> insert into employee values(1, 'K1');
> insert into employee values(1, 'K1');
> insert into employee values(2, 'K2');
>
> by looking at exceptions i may rollback or commit, i.e. conn.rollback() or conn.commit()
> if I rollback table should be empty,
> if I commit table should have 2 rows
>
> is there any way is possible ?

Use on conflict do nothing:

    insert into employee values(1, 'K1') on conflict (empid) do nothing;
    insert into employee values(1, 'K1') on conflict (empid) do nothing;
    insert into employee values(2, 'K2') on conflict (empid) do nothing;

This can be combined with a PreparedStatement and batching if needed.




pgsql-jdbc by date:

Previous
From: John R Pierce
Date:
Subject: Re: JDBC behaviour
Next
From: Vitalii Tymchyshyn
Date:
Subject: Re: JDBC behaviour