Skip to content

Insert Update

Zsolt Herpai edited this page Apr 2, 2015 · 4 revisions

###Insert and Update queries####

query.update(sqlQuery)...

Creates an insert or update query that can be parameterized executed. ####Parameters#### 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();

####Update result#### UpdateResult contains stats about a successfully executed query. Note: errors trigger exceptions

UpdateResult result = query ...
Long affectedRows = result.affectedRows();

#####Fetching generated keys##### 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(sql)
	.runFetchGenKeys(Mappers.singleLong());
List<Long> keys = result.generatedKeys();

Clone this wiki locally