Re: Support for getting generated keys.

From: Kris Jurka <books(at)ejurka(dot)com>
To: Junyan Luo <jzl106(at)gmail(dot)com>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: Support for getting generated keys.
Date: 2009-09-26 14:38:00
Message-ID: alpine.BSO.2.00.0909261033190.2098@leary.csoft.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

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

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message Kris Jurka 2009-09-26 14:46:30 Re: Wrong column default values
Previous Message Kris Jurka 2009-09-26 14:32:19 Re: [JDBC] BUG #5058: [jdbc] Silent failure with executeUpdate()