-
Notifications
You must be signed in to change notification settings - Fork 22
Insert Update
Zsolt Herpai edited this page Aug 7, 2017
·
4 revisions
query.update(sqlQuery)...Creates an insert or update query that can be parameterized executed.
Both positional and named parameters are supported
UpdateResult result = query
.update("UPDATE CUSTOMER SET NAME = ?, ADDRESS = ?")
.params("John Doe", "Dallas")
.run();Map<String, Object> namedParams = new HashMap<>();
namedParams.put("name", "John Doe");
namedParams.put("address", "Dallas");
UpdateResult result = query
.update("UPDATE CUSTOMER SET NAME = :name, ADDRESS = :address")
.namedParams(namedParams)
.run();UpdateResult contains stats about a successfully executed query. Note: errors trigger exceptions
UpdateResult result = query ...
Long affectedRows = result.affectedRows();Inserted / updated rows may have keys generated by the database, that can be useful for the application to know. The API supports any key types, statements affecting multiple rows, rows containing multiple generated keys. Note that this feature needs proper JDBC driver support.
An example:
UpdateResultGenKeys<Long> result = query
.update("INSERT INTO CUSTOMER(NAME) VALUES(:name)")
.namedParams(namedParams)
.runFetchGenKeys(Mappers.singleLong());
Long id = result.generatedKeys().get(0);