INSERT and get ID

From: Peter(dot)Zoche(at)materna(dot)de
To: pgsql-jdbc(at)postgresql(dot)org
Subject: INSERT and get ID
Date: 2005-07-27 12:56:59
Message-ID: 3617A3C21370D045B75C0A40A7A6530D0B37E199@ntexc2buc.do-office.buc.materna.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

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.

Thanks, Peter

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Oliver Jowett 2005-07-27 13:14:56 Re: INSERT and get ID
Previous Message Rohit Ankushe 2005-07-27 08:30:06 JDBC JAR Problem