SQL java gets value assigned to auto increment primary key

I have an auto increment attribute of a primary key in my table. I want to know the value assigned to it for a string that is inserted using statement.executeUpdate (). How to achieve this in the best way?

+4
source share
1 answer

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

Here is an example that returns a ResultSet with values ​​for automatically generated columns in table 1:

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

Source: https://habr.com/ru/post/1310049/


All Articles