liveBook · Manning

Database update

As explained in Spring in Action, Sixth Edition

The JdbcTemplate update method executes a SQL statement that writes or modifies database data and accepts values for its parameters.

JdbcTemplate’s update() method executes SQL that inserts or modifies database data. Pass the SQL string followed by values for its ? parameters; unlike query(), it does not require ResultSet mapping. section 3.1.2

Insert an ingredient

A repository can save an Ingredient with jdbcTemplate.update("insert into Ingredient (id, name, type) values (?, ?, ?)", ingredient.getId(), ingredient.getName(), ingredient.getType().toString());. The three values replace the SQL placeholders, after which the repository returns the ingredient. section 3.1.2

Listing 3.6 Inserting data with JdbcTemplate
@Override
public Ingredient save(Ingredient ingredient) {
  jdbcTemplate.update(
      "insert into Ingredient (id, name, type) values (?, ?, ?)",
      ingredient.getId(),
      ingredient.getName(),
      ingredient.getType().toString());
  return ingredient;
}

View the complete save method using update() to insert an Ingredient row. section 3.1.2

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains SQL parameters and contrasts update() with query(), but stops short of transaction handling or generated keys.
Examples
Demonstrates inserting an Ingredient row with three values supplied to an SQL statement.
What it assumes
Assumes basic SQL and repository structure while showing how JDBC template parameters map to method arguments.
What you can do after
Build it from scratch by implementing a repository save method that inserts domain-object values into a database.
How it connects
Connects JdbcTemplate with SQL INSERT statements, repositories, query parameters, and database reads.

Also explained in

Compare how 3 books explain database update →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.