By Alvin Alexander. Last updated: June 4, 2016
Spring JDBC Example/Question: How do I perform a SQL DELETE query (statement) using JDBC and the Spring Framework?
Here's a simple Java method that I created to perform a SQL DELETE using Spring JDBC techniques:
private void deleteById(String sql, int id) { getSimpleJdbcTemplate().update(sql, id); }
To use this method you just need to call it with the SQL DELETE statement you want to run, also passing in the id
of the record you want to delete, like this:
deleteById("DELETE FROM hosts WHERE id=?", id);
Don't forget that Spring JDBC methods can throw a RuntimeException, and you'll want to handle those appropriately.