How to get automatically generated "id" field - Mailing list pgsql-jdbc

From Nikola Milutinovic
Subject How to get automatically generated "id" field
Date
Msg-id 42336C35.7080003@ev.co.yu
Whole thread Raw
Responses Re: How to get automatically generated "id" field
List pgsql-jdbc
Hi all.

This may be a bit more for general ML, but I'm in a fix and need an
advice. I have several tables with auto-generated "id" field, like this.

CREATE TABLE photo (
    id   SERIAL PRIMARY KEY,
    size   INT8,
    file   VARCHAR(256)
)

I would like to be able to insert values and get back the "id" of a
newly inserted record. What is a good way to do it?

I imagine I could open a transaction and read the value of the sequence,
but that is ugly, since the sequence name is autogenerated and PG specific.

Would it be better to change "SERIAL" to just INT8 and do in a transaction:

...start transaction
executeQuery( "SELECT max( id )+1 AS new_id FROM photo" );
..read "new_id"
executeUpdate( "INSERT INTO photo (id, size, file) VALUES (...)" );
..commit

Nix.

pgsql-jdbc by date:

Previous
From: Ragnar Hafstað
Date:
Subject: Re: [GENERAL] MS Access to PostgreSQL
Next
From: Dave Cramer
Date:
Subject: Re: How to get automatically generated "id" field