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

From Dave Cramer
Subject Re: INSERT and get ID
Date
Msg-id F2314408-011B-41C3-A57F-33815A9669A9@fastcrypt.com
Whole thread Raw
In response to INSERT and get ID  (Peter.Zoche@materna.de)
List pgsql-jdbc
On 27-Jul-05, at 8:56 AM, Peter.Zoche@materna.de wrote:

> Hi!
>
> Say I have done the following:
>
> CREATE SEQUENCE myseq
>
> CREATE TABLE foo (
> id INTEGER,
> val INTEGER,
> PRIMARY KEY( id ) );
>
> Is there an easy way to get the id of the newly inserted data set?
> So if I do the following:
>
> 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? 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. (using postgresql 8.0 JDBC3) I googled but I did
> only find
> PostgreSQL-
> specific hints, no one for JDBC.
you can use select currval('myseq') after using nextval('myseq') this
will retrieve the value recently returned by nextval
in your session ( so it will not be over written by another connection )


>
> Thanks, Peter
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
>        subscribe-nomail command to majordomo@postgresql.org so that
> your
>        message can get through to the mailing list cleanly
>
>


pgsql-jdbc by date:

Previous
From: Oliver Jowett
Date:
Subject: Re: INSERT and get ID
Next
From: Roland Walter
Date:
Subject: Re: INSERT and get ID