Upon inserting order into a HSQL database through my OrderDAO object, I want to be able to retrieve the sequence number that was assigned to order on insert.
I have this for my PreparedStatement
:
public long saveOrder(Order order) {
long orderId = 0;
try (Connection conn = MyDataSource.getDataSource().getConnection();
PreparedStatement ps = conn.prepareStatement("INSERT INTO orders(id, order_number) VALUES (NEXT VALUE FOR seq1, ?)",
PreparedStatement.RETURN_GENERATED_KEYS)) {
ps.setString(1, order.getOrderNumber());
ps.execute();
ResultSet rs = ps.getResultSet();
if (rs.next()) {
orderId = rs.getLong(1);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return orderId;
}
I would assume, that after executing the query and asking for its resultset, the first column in the resultset would be the sequence number. But it seems that i doesn't work that way.
Where am I going wrong with this?