Spring Dao - a Spring JDBC update example

Spring JDBC FAQ: Can you provide an example of a SQL UPDATE query used with Spring JDBC, i.e., Spring Dao class and methods?

Here's some example Java code from a Spring Dao class where I use Spring JDBC and the JDBCTemplate update method to run a SQL UPDATE statement. In short, if you can get access to the Spring JDBCTemplate, this Spring JDBC code shows a simple example of how to execute a SQL UPDATE statement.

Spring JDBC update method

Here's the source code for my example Spring JDBC update method:

private void grabFileEvent(FTPEvent ftpEvent)
{
  String updateStatement = " UPDATE files"
                         + " SET locked_by_computer_name=?, last_file_event=10"
                         + " WHERE id=?";
  this.getJdbcTemplate().update(updateStatement, new Object[] {"workstation77", ftpEvent.getId()});
}

There are other ways in the Spring Framework to execute SQL UPDATE statements, but in this case I use the version of the getJdbcTemplate().update that takes (1) a String (your SQL statement) and (2) an array of objects that represent the parameters that match the ? characters in your SQL statement.

Myself, I'm really happy with the Spring JDBC framework. Once you get past all the wiring and configuration parts, Spring really makes your JDBC statements and Spring Dao classes much easier to work with.

 

Post new comment

The content of this field is kept private and will not be shown publicly.