Java Reference
In-Depth Information
public void setValues(PreparedStatement ps, int i)
throws SQLException {
Vehicle vehicle = vehicles.get(i);
ps.setString(1, vehicle.getVehicleNo());
ps.setString(2, vehicle.getColor());
ps.setInt(3, vehicle.getWheel());
ps.setInt(4, vehicle.getSeat());
}
});
}
}
You can test your batch insert operation with the following code snippet in the
Main
class:
package com.apress.springenterpriserecipes.vehicle;
...
public class Main {
public static void main(String[] args) {
...
VehicleDao vehicleDao = (VehicleDao) context.getBean("vehicleDao");
Vehicle vehicle1 = new Vehicle("TEM0002", "Blue", 4, 4);
Vehicle vehicle2 = new Vehicle("TEM0003", "Black", 4, 6);
vehicleDao.insertBatch(
Arrays.asList(new Vehicle[] { vehicle1, vehicle2 }));
}
}
3-2. Using a JDBC Template to Query a Database
Problem
To implement a JDBC query operation, you have to perform the following tasks, two of which (task 5 and
task 6) are additional as compared to an update operation:
1.
Obtain a database connection from the data source.
Create a
PreparedStatement
object from the connection.
2.
Bind the parameters to the
PreparedStatement
object.
3.
Execute the
PreparedStatement
object.
4.
5.
Iterate the returned result set.
6.
Extract data from the result set.
Handle
SQLException
.
7.
8.
Clean up the statement object and connection.
Search WWH ::
Custom Search