Java Reference
In-Depth Information
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.update(sql, new Object[] { vehicle.getVehicleNo(),
vehicle.getColor(),vehicle.getWheel(), vehicle.getSeat() });
Of the three different versions of the update() method introduced, the last is the simplest because
you don't have to implement any callback interfaces. Additionally, we've managed to remove all setX
(setInt, setString, etc.) -style methods for parameterizing the query. In contrast, the first is the most
flexible because you can do any preprocessing of the PreparedStatement object before its execution. In
practice, you should always choose the simplest version that meets all your needs.
There are also other overloaded update() methods provided by the JdbcTemplate class. Please refer
javadoc for details.
Batch Updating a Database
Suppose that you want to insert a batch of vehicles into the database. If you call the insert() method
multiple times, it will be very slow as the SQL statement will be compiled repeatedly. So, it would be
better to add a new method to the DAO interface for inserting a batch of vehicles.
package com.apress.springenterpriserecipes.vehicle;
public interface VehicleDao {
public void insertBatch(List<Vehicle> vehicles);
The JdbcTemplate class also offers the batchUpdate() template method for batch update operations.
It requires a SQL statement and a BatchPreparedStatementSetter object as arguments. In this method,
the statement is compiled (prepared) only once and executed multiple times. If your database driver
supports JDBC 2.0, this method automatically makes use of the batch update features to increase
package com.apress.springenterpriserecipes.vehicle;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
public class JdbcVehicleDao implements VehicleDao {
public void insertBatch(final List<Vehicle> vehicles) {
+ "VALUES (?, ?, ?, ?)";
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
public int getBatchSize() {
return vehicles.size();
Search WWH ::

Custom Search