I have an insert method in my vert.x project written in rxjava style. In my method I run an insert query to insert the record in ms sql server. I want to get the auto incremented key of newly inserted record. How I can get it?
Here is my code.
@Override
public Single<Record> save(Record record) {
return new AsyncResultSingle<Record>(resultHandler -> {
jdbcClient.rxGetConnection()
.subscribe(connection -> {
String sql = "INSERT into record (ani, template_id) values (?, ?)";
JsonArray params = new JsonArray().add(record.ani).add(record.templateId);
connection.rxQueryWithParams(sql, params)
.doAfterTerminate(connection::close)
.subscribe(resultSet -> {
List<JsonObject> rows = resultSet.getRows();
//how I can get a key or Record object which has been inserted?
resultHandler.handle(Future.succeededFuture());
}, onError -> {
resultHandler.handle(Future.failedFuture(onError));
});
}, onError -> {
resultHandler.handle(Future.failedFuture(onError));
});
});
}
I think, instead of
you are looking for
EDIT: you already figured it out, but to correct my answer here: you also have to use
rxUpdateWithParams
instead ofrxQueryWithParams
.After I have changed
to
then, I am able to get the key as;
Thank you @taygetos for the hint.