开发者

SQL java get value assigned to auto increment primary key [duplicate]

This question already has answers here: How to get the insert ID in JDBC? (14 answers) Closed 6 years ago.

I have a primary key auto increment attribute in my table. I want to 开发者_运维问答know the value assigned to it for a row that is inserted using statement.executeUpdate(). How to achieve this in the best possible manner?


Use Statement#getGeneratedKeys() and Statement#executeUpdate(String, int) (this is a JDBC 3.0 feature, your database has to support JDBC 3.0).

Here's an example that returns a ResultSet with values for auto-generated columns in TABLE1:

Statement stmt = conn.createStatement();
int rows = stmt.executeUpdate("INSERT INTO TABLE1 (C11, C12) VALUES (1,1)", Statement.RETURN_GENERATED_KEYS);
ResultSet rs = stmt.getGeneratedKeys();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜