sproketboy
sproketboy

Reputation: 9452

Java JDBC Retrieve values from DEFAULTs after an insert

Does anyone know of a standard way to retrieve value defined with Defaults in a database when you insert?

These are not primary keys but other columns, the getGeneratedKeys method only returns for auto-increment, but I have other defaults like LastUpdate (date) or CreatedOn (date).

I realize that some databases like MSSQL have an output option or Oracle Return option, but I'm looking for a common way to do it.

Upvotes: 0

Views: 342

Answers (1)

rzwitserloot
rzwitserloot

Reputation: 103254

Use the generated key so you can then follow up with a SELECT allTheFieldsYouCareAbout FROM tableYouJustAddedSomethingTo WHERE unid = generatedKeyYouJustGot.

Yeah, that's annoying and somewhat dubious from a performance perspective (the primary key is doubtlessly indexed, so not too pricey, but it's still another back-and-forth over TCP or whatever pipe you're using to talk to your database).

It's also the only way that reliably works on all major JDBC drivers.

Upvotes: 1

Related Questions