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

Re: need help of getting PK after insertRow in JDBC

From: Martin Gainty <mgainty(at)hotmail(dot)com>
To: "Chen, Dongdong (GE Healthcare, consultant)" <dongdong(dot)chen(at)ge(dot)com>,<pgsql-general(at)postgresql(dot)org>, <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: need help of getting PK after insertRow in JDBC
Date: 2008-09-29 20:16:21
Message-ID: BLU142-W279EE7B9CAF50A659B5A06AE400@phx.gbl (view raw, whole thread or download thread mbox)
Lists: pgsql-generalpgsql-jdbc
Good Afternoon

add the CLOSE_CURSORS_AT_COMMIT Option to the Statements capabilities e.g.
    Statement st = db.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE.ResultSet.CLOSE_CURSORS_AT_COMMIT);

//then tell the connection handle to commit the DML to the DB


Control your own destiny or someone else will -- Jack Welch
Disclaimer and confidentiality note 
Everything in this e-mail and any attachments relates to the official business of Sender. This transmission is of a confidential nature and Sender does not endorse distribution to any party other than intended recipient. Sender does not necessarily endorse content contained within this transmission. 

Subject: [GENERAL] need help of getting PK after insertRow in JDBC 
Date: Fri, 26 Sep 2008 10:14:01 +0800
From: Dongdong(dot)Chen(at)ge(dot)com
To: pgsql-general(at)postgresql(dot)org; pgsql-jdbc(at)postgresql(dot)org

    I am a 
software engineer from GE. I am using JDBC to operate PostgreSQL8.3 in 
Ubuntu8.04. The develop environment is Eclipse3.2 My problem is: 

    There is a 
PostgreSQL table XX containing 5 fields: AA, BB, CC, DD, EE, AA is primary key 
and auto-generated type, BB, CC, DD and EE is string type.
    I want to 
get the value of AA immediately after insert a row into the table. the 
code is like this:
    Statement st 
= db.creatStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
rs=st.executeQuery("SELECT * FROM XX");
rs.updateString(BB, "b");
rs.updateString(CC, "c");
rs.updateString(DD, "d");
rs.updateString(EE, "e");
    int index = 
   in the last 
sentence I always get 0 no matter how many records I insert. I tried other ways 
of moving the cursor including next(), last() and so on, and also 
cannot get the correct value. I tried the drivers both 
postgresql-8.3-603.jdbc3.jar and 
    But when I 
use pdadminIII to check the table XX, the AA field is already auto-generated 
with the correct value.
    I found a 
way to solve this: close resultset and statement after moveToCurrentRow() and 
re-open them, and rs.last(), then run int index=rs.getInt("AA"), I can get the 
correct value. I think this method is sort of awkward, anyone knows a 
better way and the correct operations? 
I am not sure it is proper to 
send this mail to this mail list. Sorry if bring you any 
Thanks a 
Best Regards


See how Windows connects the people, information, and fun that are part of your life.

In response to


pgsql-jdbc by date

Next:From: GaussDate: 2008-09-30 00:21:43
Subject: Re: [JDBC] need help of getting PK after insertRow in JDBC
Previous:From: Tom LaneDate: 2008-09-29 16:34:21
Subject: Re: Postgres Giving Errors Incomplete Startup Packet

pgsql-general by date

Next:From: Scott MarloweDate: 2008-09-29 20:18:04
Subject: Re: database question
Previous:From: Scott MarloweDate: 2008-09-29 20:15:32
Subject: Re: Re: Is there a parameter to tell postgresql to not attempt to open an IPv6 socket?

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