Java Reference
In-Depth Information
Now you can delete the preceding InsertVehicleStatementCreator class because it will not be used
anymore.
Updating a Database with a Statement Setter
The second callback interface, PreparedStatementSetter , as its name indicates, performs only the
parameter binding task (task 3) of the overall update process.
package com.apress.springenterpriserecipes.vehicle;
...
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
public class JdbcVehicleDao implements VehicleDao {
...
public void insert(final Vehicle vehicle) {
String sql = "INSERT INTO VEHICLE (VEHICLE_NO, COLOR, WHEEL, SEAT) "
+ "VALUES (?, ?, ?, ?)";
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.update( sql, new PreparedStatementSetter() {
public void setValues(PreparedStatement ps)
throws SQLException {
ps.setString(1, vehicle.getVehicleNo());
ps.setString(2, vehicle.getColor());
ps.setInt(3, vehicle.getWheel());
ps.setInt(4, vehicle.getSeat());
}
});
}
}
Another version of the update() template method accepts a SQL statement and a
PreparedStatementSetter object as arguments. This method will create a PreparedStatement
object for you from your SQL statement. All you have to do with this interface is to bind your
parameters to the PreparedStatement object.
Updating a Database with a SQL Statement and Parameter Values
Finally, the simplest version of the update() method accepts a SQL statement and an object array as
statement parameters. It will create a PreparedStatement object from your SQL statement and bind the
parameters for you. Therefore, you don't have to override any of the tasks in the update process.
package com.apress.springenterpriserecipes.vehicle;
...
import org.springframework.jdbc.core.JdbcTemplate;
public class JdbcVehicleDao implements VehicleDao {
...
public void insert(final Vehicle vehicle) {
String sql = "INSERT INTO VEHICLE (VEHICLE_NO, COLOR, WHEEL, SEAT) "
+ "VALUES (?, ?, ?, ?)";
Search WWH ::




Custom Search