how to get the primary key after an insert h2

Solutions on MaxInterview for how to get the primary key after an insert h2 by the best coders in the world

showing results for - "how to get the primary key after an insert h2"
Leonardo
25 Oct 2016
1// here you can specify the list of returned attributes, in your case just the data
2String[] returnedAttributes = {"data"};
3String insertQuery = "insert into test(id) values(1);";
4try 
5(
6    PreparedStatement insertStatement = conn.prepareStatement(insertQuery, returnedAttributes);
7) 
8{
9    int rows = insertStatement.executeUpdate();
10    if (rows == 0) 
11    {
12        throw new SQLException("Failed of insertion");
13    }
14    try (ResultSet rs = insertStatement.getGeneratedKeys()) {
15        if (rs.next()) 
16        {
17             java.util.UUID uuid = (java.util.UUID) rs.getObject("data");
18             System.out.println(uuid);
19        }
20    }
21}