Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id BED086329C7 for ; Sat, 26 Sep 2009 11:38:09 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 56697-02 for ; Sat, 26 Sep 2009 14:38:01 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from leary.csoft.net (leary.csoft.net [205.205.214.5]) by mail.postgresql.org (Postfix) with ESMTP id 3336A631870 for ; Sat, 26 Sep 2009 11:38:02 -0300 (ADT) Received: by leary.csoft.net (Postfix, from userid 1112) id 8E565A313; Sat, 26 Sep 2009 10:38:00 -0400 (EDT) Date: Sat, 26 Sep 2009 10:38:00 -0400 (EDT) From: Kris Jurka X-X-Sender: books@leary.csoft.net To: Junyan Luo cc: pgsql-jdbc@postgresql.org Subject: Re: Support for getting generated keys. In-Reply-To: Message-ID: References: User-Agent: Alpine 2.00 (BSO 1167 2008-08-23) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200909/50 X-Sequence-Number: 19997 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