Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id E87D1632FAB for ; Wed, 25 Nov 2009 03:32:12 -0400 (AST) 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 27789-09 for ; Wed, 25 Nov 2009 07:32:06 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 675BB632750 for ; Wed, 25 Nov 2009 03:32:05 -0400 (AST) Received: from list by lo.gmane.org with local (Exim 4.50) id 1NDCM4-000788-P3 for pgsql-jdbc@postgresql.org; Wed, 25 Nov 2009 08:32:00 +0100 Received: from 217.110.94.67 ([217.110.94.67]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 25 Nov 2009 08:32:00 +0100 Received: from spam_eater by 217.110.94.67 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 25 Nov 2009 08:32:00 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-jdbc@postgresql.org From: Thomas Kellerer Subject: Re: getUdateCount() vs. RETURNING clause Date: Wed, 25 Nov 2009 08:31:37 +0100 Lines: 43 Message-ID: References: <4B0C7CFE.50106@opencloud.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 217.110.94.67 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 In-Reply-To: <4B0C7CFE.50106@opencloud.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.532 tagged_above=-10 required=5 tests=BAYES_00=-2.599, RCVD_NUMERIC_HELO=2.067 X-Spam-Level: X-Archive-Number: 200911/24 X-Sequence-Number: 20071 Oliver Jowett, 25.11.2009 01:40: > Thomas Kellerer wrote: > >> boolean hasResult = pstmt.execute(); >> >> if (hasResult) { >> ResultSet rs = pstmt.getResultSet(); >> if (rs != null && rs.next()) { >> int newId = rs.getInt(1); >> System.out.println("newid: " + newId); >> } >> } >> >> int affected = pstmt.getUpdateCount(); >> System.out.println("affected: " + affected); >> >> I do see the returned ID from the ResultSet but getUpdateCount() always >> returns -1 even though I know that a row was updated. >> What am I missing here? > > Update counts and result sets (for a particular result) are mutually > exclusive. If getResultSet() returns non-null then getUpdateCount() must > return -1; see the javadoc for those two methods. Hmm, my understand was a bit different. The Javadocs simply say There are no more results when the following is true: ((stmt.getMoreResults() == false) && (stmt.getUpdateCount() == -1)) It doesn't state that they are mutually exclusive. And for me it seemed to imply that I can call getMoreResults() and getUpdateCount() in a loop in order to process everything that is returned. Additionally the Javadocs for getUpdateCount() says: "Gets the *current* result as an update count" and ".. if there are no more results it returns -1" The word "current" here also let me to believe I can call those methods multiple times. Regards Thomas