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

we are a community of more than 2 million smartest coders
registration for
employee referral programs
are now open
get referred to google, amazon, flipkart and more
register now
  
pinned-register now
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}