How to get the id of an inserted record in vertx using postgres sql client

spirit :

I have a function that insert a single record into a PostgreSQL database. My question is how do i get the ID of the inserted record since the ID is auto generated This is the code

postgreSQLClient.getConnection(ar -> {
        if(ar.failed())
              LOGGER.error("saving message error:", ar.cause());
        else{
           SQLConnection conn = ar.result();
           conn.queryWithParams(insertStr.toString(), sqlParam, rs -> {
               conn.close();
               Constant.LOGGER.info("Save ID: " +rs.result());
           });
        }
    });
vasquez :

Add a RETURNING clause to your insert statement. See https://www.postgresql.org/docs/current/static/sql-insert.html

This is from the vert.x documentation, see https://vertx.io/docs/vertx-mysql-postgresql-client/java/

Note about last inserted ids

When inserting new rows into a table, you might want to retrieve auto-incremented ids from the database. The JDBC API usually lets you retrieve the last inserted id from a connection. If you use MySQL, it will work the way it does like the JDBC API. In PostgreSQL you can add the "RETURNING" clause to get the latest inserted ids. Use one of the query methods to get access to the returned columns.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=104341&siteId=1