Re: INSERT and get ID - Mailing list pgsql-jdbc

From Oliver Jowett
Subject Re: INSERT and get ID
Date
Msg-id 42E788D0.5050902@opencloud.com
Whole thread Raw
In response to INSERT and get ID  (Peter.Zoche@materna.de)
List pgsql-jdbc
Peter.Zoche@materna.de wrote:

> String insert = "INSERT INTO foo (id, val)
> VALUES( nextval('myseq'), 5 )";
> int count = statement.executeUpdate( insert );
>
> I only get the row count of inserted rows. Do I have to query the
> database again to get the id?

Currently, yes. You can usually do some trickery with currval() to run
both an INSERT and a SELECT in the same statement and avoid an extra
round-trip.

> Or is there another way? I have tried
> statement.executeUpdate( insert, Statement.RETURN_GENERATED_KEYS);
> but I get an PSQLException saying that the feature of auto-generated keys
> is not supported.

Right, this really needs server-side support before we can implement it
(INSERT ... RETURNING ... or similar)

-O

pgsql-jdbc by date:

Previous
From: Peter.Zoche@materna.de
Date:
Subject: INSERT and get ID
Next
From: Dave Cramer
Date:
Subject: Re: INSERT and get ID