The easy way: via the new
jdbc 3.0 statement.executeUpdate( String sql, int autoGeneratedKeys) method. Unfortunately I haven't seen any any drivers supporting the jdbc 3.0 functionality yet.
Other options:
Database specific:
The Oracle way of doing this is to use a sequence. Generate a new autonumber value and read it using the sequence_name.NEXTVAL function (Select sequence_name.NEXTVAL from dual). Read the new sequence number into your
java program using resultset.getString(). Then do a sql update, inserting the new sequence value as the id.
For other databases you might want to look into how they deal with auto-number and such mechanisms. As you can see, this approach is anything but portable.
Another suggestion is to use System.currentTimeMillis() function to return to you a unique number based on the date/time. Just have some error handling code to deal with simultaneous inserts that may result in duplicate values ( primary key violation exceptions ). This is probably the easiest way to implement such a feature.
Jamie
[ September 16, 2002: Message edited by: Jamie Robertson ]