Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.apress.springrecipes.vehicle;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import javax.sql.DataSource;
- public class JdbcVehicleDao implements VehicleDAO{
- private DataSource dataSource;
- public void setDataSource(DataSource datasource)
- {
- this.dataSource=dataSource;
- }
- public void insert(Vehicle vehicle)
- {
- String sql="INSERT INTO VEHICLE(VEHICLE NO.,COLOR,WHEEL,SEAT)"+
- "VALUES(?,?,?,?)";
- Connection conn=null;
- try{
- conn= dataSource.getConnection();
- PreparedStatement ps=conn.prepareStatement(sql);
- ps.setString(0, vehicle.getVehicleNo());
- ps.setString(1, vehicle.getColor());
- ps.setInt(2, vehicle.getWheel());
- ps.setInt(3, vehicle.getSeat());
- ps.executeUpdate();
- ps.close();
- }catch(SQLException e)
- {
- throw new RuntimeException(e);
- }finally
- {
- if(conn!=null)
- try{
- conn.close();
- }
- catch(SQLException e)
- {}
- }
- }
- public Vehicle findByVehicleNo(String vehicleNo){
- String sql ="SELECT * FROM VEHICLE WHERE VEHICLE_NO=?";
- Connection conn=null;
- try{
- conn = dataSource.getConnection();
- PreparedStatement ps= conn.prepareStatement(sql);
- ps.setString(0, vehicleNo);
- Vehicle vehicle = null;
- ResultSet rs = ps.executeQuery();
- if (rs.next()) {
- vehicle = new Vehicle(rs.getString("VEHICLE_NO"),
- rs.getString("COLOR"), rs.getInt("WHEEL"),
- rs.getInt("SEAT"));
- }
- rs.close();
- ps.close();
- return vehicle;
- }catch(SQLException e)
- {
- throw new RuntimeException(e);
- }
- finally
- {
- if(conn!=null)
- try{
- conn.close();
- }
- catch(SQLException e)
- {}
- }
- }
- public void update(Vehicle vehicle) {}
- public void delete(Vehicle vehicle) {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement