Java Reference
In-Depth Information
package com.apress.springenterpriserecipes.vehicle;
...
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class JdbcVehicleDao
extends JdbcDaoSupport implements VehicleDao {
public void insert(final Vehicle vehicle) {
String sql = "INSERT INTO VEHICLE (VEHICLE_NO, COLOR, WHEEL, SEAT) "
+ "VALUES (?, ?, ?, ?)";
getJdbcTemplate().update(sql, new Object[] { vehicle.getVehicleNo(),
vehicle.getColor(), vehicle.getWheel(), vehicle.getSeat() });
}
...
}
By extending
JdbcDaoSupport
, your DAO class inherits the
setDataSource()
method. You can inject a
data source into your DAO instance for it to create a JDBC template.
<beans ...>
...
<bean id="vehicleDao"
class="com.apress.springenterpriserecipes.vehicle.JdbcVehicleDao">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
3-4. Using the Simple JDBC Template with Java 1.5
Problem
The
JdbcTemplate
class works fine in most circumstances, but it can be further improved to take
advantage of the Java 1.5 features.
Solution
org.springframework.jdbc.core.simple.SimpleJdbcTemplate
is an evolution of
JdbcTemplate
that takes
advantage of Java 1.5 features such as autoboxing, generics, and variable-length arguments to simplify
its usage.
How It Works
Using a Simple JDBC Template to Update a Database
Many of the methods in the classic
JdbcTemplate
require statement parameters to be passed as an object
array. In
SimpleJdbcTemplate
, they can be passed as variable-length arguments; this saves you the
trouble of wrapping them in an array. To use
SimpleJdbcTemplate
, you can either instantiate it directly or
retrieve its instance by extending the
SimpleJdbcDaoSupport
class.
Search WWH ::
Custom Search