This question already has an answer here:
- How to get the insert ID in JDBC? 11 answers
My current method is this:
SELECT TOP 1 ID FROM DATAENTRY ORDER BY ID DESC
This assumes the latest inserted item always has the highest unique ID (primary key, autoincrementing). Something smells wrong here.
Alternatives?
If using MySQL you can do
If using MS SQL
For H2, I believe it's
but I don't have any experience with that DB
If the JDBC driver supports it, you can also just use
Statement#getGeneratedKeys()
for that.