Re: Retrieve primary key after inserting - Mailing list pgsql-jdbc

From Hale Pringle
Subject Re: Retrieve primary key after inserting
Date
Msg-id HIEOJHJBFLIEMFCAOMOEOENPCGAA.halepringle@yahoo.com
Whole thread Raw
In response to Retrieve primary key after inserting  (Alex Dovlecel <dovle@kbs.twi.tudelft.nl>)
List pgsql-jdbc
One way to do this is as follows for table "recipients" and id "entryid"
The serial keyword in CREATE TABLE will create a sequence called
"recipients_entryid_seq"
(e.g. "{tableName}_{serialFieldName}_seq").  The nextval() function will
bump the value by one and return the updated value.


        private int locID;

        public int getID(){
            return locID;
         }

        public void InsertTable(String fields, String values) {
            try {
                statement =
                dbc.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
                rs = statement.executeQuery("SELECT
nextval('recipients_entryid_seq')");
                while ( rs.next() ) {
                    locId = rs.getInt("nextval" );
                    //    System.out.println("locId = "+locId);
                }
                query = "Insert into recipients (entryid,"+fields+ ") values
("+locId+","+values+")";
                statement.executeUpdate( query );
                 //System.out.println( "Recipients Insert Successful new id
= "+locId+" \n" );
            }//End try

            catch(   etc. )
           }
>Hello all,

>Is there a special mechanism to retrieve the primary key from a record that
I
>just inserted?
<snip>
>Tx
>dovle


pgsql-jdbc by date:

Previous
From: Dave Cramer
Date:
Subject: Re: by the way...
Next
From: Jeff Hodd
Date:
Subject: Missing javarc file