Re: getting primary key values for inserted records?

From: Jeremy Buchmann <jeremy(at)wellsgaming(dot)com>
To: Guido Fiala <guido(dot)fiala(at)dka-gmbh(dot)de>
Cc: pgsql-jdbc <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: getting primary key values for inserted records?
Date: 2004-01-27 17:16:54
Message-ID: 9AB80F78-50EC-11D8-BFB3-000A95D7EFFE@wellsgaming.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

> I'am currently at the point, that after i insert a new Record in a
> ResultSet
> using insertRow() and using a serial for the primary key of the target
> table.
>
> Of course i do not call rs.updateInt(primaryKey), as the sequence does
> get me
> the new value automatically.
>
> However - even using rs.refreshRow() doesn't get me the values, just a
> requery
> of the ResultSet.
>
> What is the way to go here?

If you don't mind running two statements, you can select the next value
from the sequence and then put it in your insert statement. Like this:

$pkey = "SELECT nextval('sequence_name')";
"INSERT INTO table (id, ...) VALUES ($pkey, ...)";

It's psuedo-code, but you get the idea. This ensures that you know the
primary key of the row you inserted, and the number came from the
sequence so it's safe.

Hope that helps,
Jeremy

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message Peter Eisentraut 2004-01-27 17:17:52 Re: jdbc connection string
Previous Message Barry Lind 2004-01-27 17:14:09 Re: Patch to readd documentation