UPDATE query

The SQL UPDATE query is used to modify the existing record(s) in a database table.

Let's assume we are changing the name of the specific person in our actors database table.

The SQL syntax of UPDATE query is:

executeStatement("UPDATE actors SET name = 'Kaley Cuoco-Sweeting' WHERE name = 'Kaley Cuoco'");

Here we select specific records with the help of WHERE condition, and set new value 'Kaley Cuoco-Sweeting' in the name column for all fetched records. In WHERE clause, you can combine several conditions using AND or OR operators.

It is equivalent to the following function in QueryDSL:

update(actors)
    .where(actors.name.eq(
"Kaley Cuoco"))
    .set(actors.name, "Kaley Cuoco-Sweeting")
    .execute();

Updating several values

It was a simple example. Let's show how to update several values at once. 

Say, we want also to update the marital status. It is evident how to do this when it is defined as a String text, so now we will show how to update the value when it is defined as option of an option list. Assume the status in the model is defined as MaritalStatus option list with alternative options (SINGLE, MARRIED).

The queries will be:

update(actors)
    .where(actors.name.eq(
"Kaley Cuoco"))
    .set(actors.name, "Kaley Cuoco-Sweeting")
    .set(actors.status, MARRIED)
    .execute();

executeStatement("UPDATE actors SET name = ?, status = ? WHERE name = ?",
    "Kaley Cuoco-Sweeting", MARRIED, "Kaley Cuoco");