Skip site navigation (1) Skip section navigation (2)

getUdateCount() vs. RETURNING clause

From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: getUdateCount() vs. RETURNING clause
Date: 2009-11-24 11:45:26
Message-ID: hegh0m$j0f$ (view raw, whole thread or download thread mbox)
Lists: pgsql-jdbc

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 && {
    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)



pgsql-jdbc by date

Next:From: Oliver JowettDate: 2009-11-25 00:40:30
Subject: Re: getUdateCount() vs. RETURNING clause
Previous:From: Kris JurkaDate: 2009-11-20 23:45:57
Subject: Re: COPY and turning off autocommit

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group