pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Oliver Jowett <oliver@opencloud.com>
To: Peter.Zoche@materna.de
Cc: pgsql-jdbc@postgresql.org
Subject: Re: INSERT and get ID
Date: Thu, 28 Jul 2005 01:14:56 +1200
Message-ID: <42E788D0.5050902@opencloud.com> (raw)
In-Reply-To: <3617A3C21370D045B75C0A40A7A6530D0B37E199@ntexc2buc.do-office.buc.materna.com>
References: <3617A3C21370D045B75C0A40A7A6530D0B37E199@ntexc2buc.do-office.buc.materna.com>

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



view thread (9+ messages)  latest in thread

Message-ID: <42E788D0.5050902@opencloud.com>
Permalink:  ../42E788D0.5050902@opencloud.com/
Also on:    postgresql.org/message-id/42E788D0.5050902@opencloud.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: oliver@opencloud.com, Peter.Zoche@materna.de
  Subject: Re: INSERT and get ID
  In-Reply-To: <42E788D0.5050902@opencloud.com>
  References: <3617A3C21370D045B75C0A40A7A6530D0B37E199@ntexc2buc.do-office.buc.materna.com> <42E788D0.5050902@opencloud.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox