Java Reference
In-Depth Information
Named parameters can also be used in batch update. You can provide either a Map array or a
SqlParameterSource array for the parameter values.
package com.apress.springenterpriserecipes.vehicle;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
public class JdbcVehicleDao extends SimpleJdbcDaoSupport implements VehicleDao {
public void insertBatch(List<Vehicle> vehicles) {
+ "VALUES (:vehicleNo, :color, :wheel, :seat)";
List<SqlParameterSource> parameters = new ArrayList<SqlParameterSource>();
for (Vehicle vehicle : vehicles) {
parameters.add(new BeanPropertySqlParameterSource(vehicle));
parameters.toArray(new SqlParameterSource[0]));
3-6. Handling Exceptions in the Spring JDBC Framework
Many of the JDBC APIs declare throwing java.sql.SQLException , a checked exception that must be
caught. It's very troublesome to handle this kind of exception every time you perform a database
operation. You often have to define your own policy to handle this kind of exception. Failure to do so
may lead to inconsistent exception handling.
The Spring framework offers a consistent data access exception-handling mechanism for its data access
module, including the JDBC framework. In general, all exceptions thrown by the Spring JDBC framework
are subclasses of DataAccessException , a type of RuntimeException that you are not forced to catch. It's
the root exception class for all exceptions in Spring's data access module.
Figure 3-1 shows only part of the DataAccessException hierarchy in Spring's data access module. In
total, there are more than 30 exception classes defined for different categories of data access exceptions.
Search WWH ::

Custom Search