Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id E0E48633DAF for ; Mon, 13 Jul 2009 19:35:47 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 37608-06 for ; Mon, 13 Jul 2009 19:35:31 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from tremor.opencloud.co.nz (tremor.opencloud.co.nz [131.203.101.230]) by mail.postgresql.org (Postfix) with ESMTP id 2A22963386F for ; Mon, 13 Jul 2009 19:35:36 -0300 (ADT) Received: from 125-237-255-118.jetstream.xtra.co.nz ([125.237.255.118] helo=[192.168.1.3]) by tremor.opencloud.co.nz with esmtpsa (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.63) (envelope-from ) id 1MQU7Q-0003z2-3g; Tue, 14 Jul 2009 10:35:32 +1200 Message-ID: <4A5BB6B3.3020002@opencloud.com> Date: Tue, 14 Jul 2009 10:35:31 +1200 From: Oliver Jowett User-Agent: Thunderbird 2.0.0.22 (X11/20090608) MIME-Version: 1.0 To: Thomas Kellerer CC: pgsql-jdbc@postgresql.org Subject: Re: DELETE ... RETURNING References: <226a19190907131251m6c3786f5id8e8025f5c53cf66@mail.gmail.com> <4A5BAA29.9000004@opencloud.com> In-Reply-To: X-Enigmail-Version: 0.95.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200907/31 X-Sequence-Number: 19799 Thomas Kellerer wrote: > The following code will print "deleted: -1" and nothing more. > stmt.execute("delete from test_delete returning id"); > System.out.println("deleted: " + stmt.getUpdateCount()); > if (stmt.getMoreResults()) > { > System.out.println("has result"); > rs = stmt.getResultSet(); > while (rs.next()) > { > System.out.println(rs.getObject(1)); > } > } I took another look at this, and your code assumes that the update count is the first result. It's not. In the case where both a resultset and an update count are present in a single query, the driver puts the resultset result first (so that executeQuery() works nicely). So your first call to getUpdateCount() returns -1 because the current result is a resultset, not an update count (see the javadoc). Then you call getMoreResults() which moves to the 2nd result (the update count) and returns false because there's no resultset (again, see the javadoc). If you want a general-purpose result processing loop, you want something like this: boolean hasResultSet = stmt.execute("..."); while (hasResultSet || stmt.getUpdateCount() != -1) { if (hasResultSet) { ResultSet rs = stmt.getResultSet(); // .. process it .. } else { int updateCount = stmt.getUpdateCount(); // .. process it .. } hasResultSet = stmt.getMoreResults(); } -O