From: | "Hale Pringle" <halepringle(at)yahoo(dot)com> |
---|---|
To: | <pgsql-jdbc(at)postgresql(dot)org> |
Subject: | Re: Retrieve primary key after inserting |
Date: | 2002-11-19 15:00:27 |
Message-ID: | HIEOJHJBFLIEMFCAOMOEOENPCGAA.halepringle@yahoo.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | 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
From | Date | Subject | |
---|---|---|---|
Next Message | Felipe Schnack | 2002-11-19 15:14:06 | more about setDefault() |
Previous Message | Bruce Momjian | 2002-11-19 14:05:08 | Re: documentation |