问题
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));
});
});
}
回答1:
After I have changed
connection.rxQueryWithParams(sql, params)
to
connection.rxUpdateWithParams(sql, params)
then, I am able to get the key as;
JsonArray rows = resultSet.getKeys();
Thank you @taygetos for the hint.
回答2:
I think, instead of
List<JsonObject> rows = resultSet.getRows();
you are looking for
List<JsonArray> results = resultSet.getResults();
EDIT: you already figured it out, but to correct my answer here: you also have to use rxUpdateWithParams
instead of rxQueryWithParams
.
来源:https://stackoverflow.com/questions/54117013/how-to-get-a-key-from-rxjava-insert-query-in-vert-x