Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id B730B635FA2 for ; Fri, 25 Sep 2009 12:37:11 -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 88510-09 for ; Fri, 25 Sep 2009 15:37:04 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-ew0-f216.google.com (mail-ew0-f216.google.com [209.85.219.216]) by mail.postgresql.org (Postfix) with ESMTP id C8A806351B2 for ; Fri, 25 Sep 2009 12:37:03 -0300 (ADT) Received: by ewy12 with SMTP id 12so2342113ewy.24 for ; Fri, 25 Sep 2009 08:37:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:date:message-id:subject :from:to:content-type; bh=th6EFIAPN+6uKzTTXpfQLf2vOLC6TdVwEOgShVhaAmg=; b=n59reBM/7ZzcK6BW1cMWYsiSr6PKllri+acA9ncCLHZ85/PUMBvcv08E+xmPhBFt8x aSLZcT1t+smQtVGtsNxoeuSYN8Wo6wXgWjMJjWkhfsil3VnPaUKvkOMnrQp3W+Alzp84 D1R//LUu0y1fzKPyFVxsqjvRo4JiOuNVk2LCg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=p7tLLAjYbDMnNUcEWpS95d5zI8gN/kMg4rSxbAVbfYRGTrfI3pEumouQ+isIQU5JRM iar9x18ZxGqUOIhVZ4B0oL1Z8aTX2YiCRxi2iPTmiAZqAuhr4m+G13YtwJl1pVDQh2sa OoqpCgPDcOwQuD/6klgzh0moPjff9G5iVZWp8= MIME-Version: 1.0 Received: by 10.211.159.19 with SMTP id l19mr1207044ebo.43.1253893022135; Fri, 25 Sep 2009 08:37:02 -0700 (PDT) Date: Fri, 25 Sep 2009 11:37:02 -0400 Message-ID: Subject: Support for getting generated keys. From: Junyan Luo To: pgsql-jdbc@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 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/41 X-Sequence-Number: 19988 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?) 2. For a PreparedStatement, you can EITHER use the approach above, OR use Statement.getGeneratedKeys(), and in the latter case you have to make sure you use Connection.prepareStatement(sql, new String[]{"id column"}) to indicate the NAME of your id column. Setting id column by indexes are not supported; 3. Unfortunately, batch update is still not supported in EITHER case. I tried to use a preparedStatement created with a id column in a batch update situation, and it always give me this exception: java.sql.BatchUpdateException: Batch entry 0 INSERT INTO test.keytest (name) VALUES ('my name') RETURNING "id" was aborted. Am I missing anything? Thanks! REGARDS, Junyan