Java Reference
In-Depth Information
public Vehicle findByVehicleNo(String vehicleNo) {
Vehicle vehicle = getSimpleJdbcTemplate().queryForObject(
sql, Vehicle.class, vehicleNo);
return vehicle;
Querying for Multiple Rows
Now let's see how to query for a result set with multiple rows. For example, suppose that you need a
findAll() method in the DAO interface to get all vehicles.
package com.apress.springenterpriserecipes.vehicle;
public interface VehicleDao {
public List<Vehicle> findAll();
Without the help of RowMapper , you can still call the queryForList() method and pass in a SQL
statement. The returned result will be a list of maps. Each map stores a row of the result set with the
column names as the keys.
package com.apress.springenterpriserecipes.vehicle;
import org.springframework.jdbc.core.JdbcTemplate;
public class JdbcVehicleDao implements VehicleDao {
public List<Vehicle> findAll() {
String sql = "SELECT * FROM VEHICLE";
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
List<Vehicle> vehicles = new ArrayList<Vehicle>();
List<Map<String,Object>> rows = jdbcTemplate. queryForList(sql);
for (Map<String, Object> row : rows) {
Vehicle vehicle = new Vehicle();
vehicle.setVehicleNo((String) row.get("VEHICLE_NO"));
vehicle.setColor((String) row.get("COLOR"));
vehicle.setWheel((Integer) row.get("WHEEL"));
vehicle.setSeat((Integer) row.get("SEAT"));
return vehicles;
Search WWH ::

Custom Search