Thread: Retrieve auto-generated key.

Retrieve auto-generated key.

From
"Sangeeta"
Date:

Hi

 

Ability to retrieve auto-generated key is one of the key features of JDBC3.
I tried using prepareStatement(String sql, int autoGeneratedKeys) , prepareStatement(String sql, int[] columnIndexes) and prepareStatement(String sql, String[] columnNames) but with no success . It returned with exception message as "Returning autogenerated keys is not supported."

 

I know there is already one thread for retrieve auto-generated key but look like that thread does not have one distinct   solution.

Is there any patch or workaround to retrieve auto-generated key ?

 

 

Thanks

Sangeeta

Re: Retrieve auto-generated key.

From
"gcj"
Date:

Have you tried using the “RETURNING” keyword?  For example…

 

String sql = “ INSERT INTO \“YourSchemaName\”.\”YourTableName\” (\”YourColName1\”, \”YourColName2\”) VALUES (?, ?) RETURNING \“YourSerialColName\”;

PreparedStatement ps = con.prepareStatement(sql);

ps.setString(1, “someValue”);

ps.setString(2, “anotherValue”);

rs = ps.executeQuery();

if (rs.next()) {

  System.out.println(“Auto-generated key is:” +rs.getInt(1));

}

 

Hope this helps


From: pgsql-jdbc-owner@postgresql.org [mailto:pgsql-jdbc-owner@postgresql.org] On Behalf Of Sangeeta
Sent: Tuesday, November 04, 2008 8:46 AM
To: pgsql-jdbc@postgresql.org
Subject: [JDBC] Retrieve auto-generated key.

 

Hi

 

Ability to retrieve auto-generated key is one of the key features of JDBC3.
I tried using prepareStatement(String sql, int autoGeneratedKeys) , prepareStatement(String sql, int[] columnIndexes) and prepareStatement(String sql, String[] columnNames) but with no success . It returned with exception message as "Returning autogenerated keys is not supported."

 

I know there is already one thread for retrieve auto-generated key but look like that thread does not have one distinct   solution.

Is there any patch or workaround to retrieve auto-generated key ?

 

 

Thanks

Sangeeta

Re: Retrieve auto-generated key.

From
Alexander Panzhin
Date:
If you want an easy solution(and a bit more portable), you probably are
better off with doing a "SELECT lastval()"
Though an insert trigger inserting into another table with a serial
column might screw up the result.

See http://www.postgresql.org/docs/8.3/interactive/functions-sequence.html


> Hi
>
>
>
> Ability to retrieve auto-generated key is one of the key features of
> JDBC3.
> I tried using prepareStatement(String sql, int autoGeneratedKeys) ,
> prepareStatement(String sql, int[] columnIndexes) and
> prepareStatement(String sql, String[] columnNames) but with no success
> . It returned with exception message as "Returning autogenerated keys
> is not supported."
>
>
>
> I know there is already one thread for retrieve auto-generated key but
> look like that thread does not have one distinct   solution.
>
> Is there any patch or workaround to retrieve auto-generated key ?
>
>
>
>
>
> Thanks
>
> Sangeeta
>


Attachment