|
FAQ: How do I run a SQL DELETE command using JDBC and the Spring Framework?
Here's a simple Java method that I created to perform a SQL DELETE using plain old JDBC and the Spring Framework:
private void deleteById(String sql, int id)
{
getSimpleJdbcTemplate().update(sql, id);
}
To use this Java 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.
|