pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Kellerer <spam_eater@gmx.net>
To: pgsql-jdbc@postgresql.org
Subject: getUdateCount() vs. RETURNING clause
Date: Tue, 24 Nov 2009 12:45:26 +0100
Message-ID: <hegh0m$j0f$1@ger.gmane.org> (raw)

Hi, 

I'm having a problem using an UPDATE statement with a RETURNING clause.

It seems I cannot retrieve the number of rows affected using getUpdateCount()

This is my statement

UPDATE my_table
  SET the_column = the_column + 1
  WHERE id = =
  RETURNING the_column;

My Java code is:

PreparedStatement pstmt = con.prepareStatement(UPDATE_SQL); // the statement from above
pstmt.setInt(1, 42);
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?

My Postgres version is: 8.4.1 
My driver version is: PostgreSQL 8.4 JDBC4 (build 701)
I'm using Java6 (1.6.0_16)

Regards
Thomas




view thread (10+ messages)  latest in thread

Message-ID: <hegh0m$j0f$1@ger.gmane.org>
Permalink:  ../hegh0m$j0f$1@ger.gmane.org/
Also on:    postgresql.org/message-id/hegh0m$j0f$1@ger.gmane.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: spam_eater@gmx.net
  Subject: Re: getUdateCount() vs. RETURNING clause
  In-Reply-To: <hegh0m$j0f$1@ger.gmane.org>
  References: <hegh0m$j0f$1@ger.gmane.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox