pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kris Jurka <books@ejurka.com>
To: Junyan Luo <jzl106@gmail.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Support for getting generated keys.
Date: Sat, 26 Sep 2009 10:38:00 -0400 (EDT)
Message-ID: <alpine.BSO.2.00.0909261033190.2098@leary.csoft.net> (raw)
In-Reply-To: <bc5f338c0909250837i25e01b1aqba352c23da1e9f3d@mail.gmail.com>
References: <bc5f338c0909250837i25e01b1aqba352c23da1e9f3d@mail.gmail.com>



On Fri, 25 Sep 2009, Junyan Luo wrote:

> Although driver 8.4 is supposed to support getting generated keys,
> this issue still remains quite unclear for most people (including me)
> due to the lack of documentation. I have explored older threads for a
> while and figured out the current workaround and here is my findings.
> Please point out any mistake I made:
>
> 1. For a Statement, you MUST use the "RETURNING" clause in a "INSERT"
> statement. The code to get generated keys will be like:
>       ResultSet keys = Statement.executeQuery("INSERT...RETURNING [id
> column]"); // Make sure it's executeQuery() not executeUpdate()
>       keys.next();  //The result set contains generated keys;
>       int key = keys.getInt(1);
> However, Statement.getGeneratedKeys() does NOT work for Statement (Am
> I correct?)

You don't need to use RETURNING and executeQuery.  You can use a plain 
insert statement and executeUpdate(sql, new String[]{"id"}) and the 
results will be available via getGeneratedKeys.

Kris Jurka



view thread (2+ messages)

Message-ID: <alpine.BSO.2.00.0909261033190.2098@leary.csoft.net>
Permalink:  ../alpine.BSO.2.00.0909261033190.2098@leary.csoft.net/
Also on:    postgresql.org/message-id/alpine.BSO.2.00.0909261033190.2098@leary.csoft.net

 · 

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: books@ejurka.com, jzl106@gmail.com
  Subject: Re: Support for getting generated keys.
  In-Reply-To: <alpine.BSO.2.00.0909261033190.2098@leary.csoft.net>
  References: <bc5f338c0909250837i25e01b1aqba352c23da1e9f3d@mail.gmail.com> <alpine.BSO.2.00.0909261033190.2098@leary.csoft.net>

* 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