Skip to content

Insert Update

Zsolt Herpai edited this page Aug 7, 2017 · 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("INSERT INTO CUSTOMER(NAME) VALUES(:name)")
	.namedParams(namedParams)
	.runFetchGenKeys(Mappers.singleLong());
Long id = result.generatedKeys().get(0);

Clone this wiki locally