Advertisement
Guest User

Untitled

a guest
Nov 25th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. package com.apress.springrecipes.vehicle;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. import javax.sql.DataSource;
  9.  
  10. public class JdbcVehicleDao implements VehicleDAO{
  11.  
  12. private DataSource dataSource;
  13.  
  14. public void setDataSource(DataSource datasource)
  15. {
  16. this.dataSource=dataSource;
  17. }
  18.  
  19. public void insert(Vehicle vehicle)
  20. {
  21.  
  22.  
  23. String sql="INSERT INTO VEHICLE(VEHICLE NO.,COLOR,WHEEL,SEAT)"+
  24. "VALUES(?,?,?,?)";
  25. Connection conn=null;
  26. try{
  27. conn= dataSource.getConnection();
  28.  
  29. PreparedStatement ps=conn.prepareStatement(sql);
  30. ps.setString(0, vehicle.getVehicleNo());
  31. ps.setString(1, vehicle.getColor());
  32. ps.setInt(2, vehicle.getWheel());
  33. ps.setInt(3, vehicle.getSeat());
  34. ps.executeUpdate();
  35. ps.close();
  36.  
  37.  
  38. }catch(SQLException e)
  39. {
  40. throw new RuntimeException(e);
  41. }finally
  42. {
  43. if(conn!=null)
  44. try{
  45. conn.close();
  46. }
  47. catch(SQLException e)
  48. {}
  49.  
  50.  
  51. }
  52.  
  53. }
  54.  
  55. public Vehicle findByVehicleNo(String vehicleNo){
  56. String sql ="SELECT * FROM VEHICLE WHERE VEHICLE_NO=?";
  57. Connection conn=null;
  58. try{
  59.  
  60. conn = dataSource.getConnection();
  61. PreparedStatement ps= conn.prepareStatement(sql);
  62. ps.setString(0, vehicleNo);
  63.  
  64. Vehicle vehicle = null;
  65. ResultSet rs = ps.executeQuery();
  66. if (rs.next()) {
  67. vehicle = new Vehicle(rs.getString("VEHICLE_NO"),
  68. rs.getString("COLOR"), rs.getInt("WHEEL"),
  69. rs.getInt("SEAT"));
  70. }
  71. rs.close();
  72. ps.close();
  73. return vehicle;
  74.  
  75. }catch(SQLException e)
  76. {
  77. throw new RuntimeException(e);
  78. }
  79. finally
  80. {
  81. if(conn!=null)
  82. try{
  83. conn.close();
  84. }
  85. catch(SQLException e)
  86. {}
  87.  
  88. }
  89.  
  90. }
  91. public void update(Vehicle vehicle) {}
  92. public void delete(Vehicle vehicle) {}
  93.  
  94.  
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement