Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. package carrental;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7.  
  8.  
  9.  
  10. public class CarManagerImpl implements CarManager {
  11.  
  12.  
  13.    
  14.     public void addCar(Car car) {
  15.  
  16.  
  17.         PreparedStatement insert = null;
  18.         Connection conn = null;
  19.         String url="jdbc:derby://localhost:1527/rental [marek on MAREK]";
  20.  
  21.  
  22.  
  23.         try {
  24.  
  25.             conn = DriverManager.getConnection(url,"marek","matus");
  26.             insert = conn.prepareStatement("INSERT INTO APP.CAR " +
  27.                     "(VIN, COLOR, TYPE, PRICEPERDAY, SPZ)" +
  28.                     "values (?,?,?,?,?)");
  29.  
  30.  
  31.  
  32.             insert.setInt(1, car.getVin());
  33.  
  34.             insert.setInt(2, car.getColor());
  35.             insert.setString(3, car.getType());
  36.  
  37.             insert.setInt(4, car.getPricePerDay());
  38.             insert.setString(5, car.getSpz());
  39.  
  40.             insert.execute();
  41.  
  42.         } catch (SQLException ex) {
  43.             ex.printStackTrace();
  44.  
  45.         } finally {
  46.             if (conn != null) {
  47.             try {
  48.                 conn.close();
  49.             } catch (SQLException ex) {
  50.                  ex.printStackTrace();
  51.             }
  52.         }
  53.              if (insert != null) {
  54.             try {
  55.                 insert.close();
  56.             } catch (SQLException ex) {
  57.                  ex.printStackTrace();
  58.             }
  59.         }
  60.         }
  61.     }
  62.  
  63.     public boolean removeCar(Car car) {
  64.        boolean ret = false;
  65.        PreparedStatement delete = null;
  66.        Connection conn = null;
  67.        String url="jdbc:derby://localhost:1527/rental [marek on MAREK]";
  68.  
  69.         try {
  70.            conn = DriverManager.getConnection(url,"marek","matus");
  71.             delete = conn.prepareStatement("DELETE FROM APP.CAR WHERE VIN = (?)");
  72.             delete.setInt(1, car.getVin() );
  73.             if (delete.executeUpdate() != 0) {
  74.                 ret = true;
  75.             }
  76.         } catch (SQLException ex) {
  77.             ex.printStackTrace();
  78.         } finally {
  79.             if (conn != null) {
  80.             try {
  81.                 conn.close();
  82.             } catch (SQLException ex) {
  83.                  ex.printStackTrace();
  84.             }
  85.         }
  86.              if (delete != null) {
  87.             try {
  88.                 delete.close();
  89.             } catch (SQLException ex) {
  90.                  ex.printStackTrace();
  91.             }
  92.         }
  93.         }
  94.          return ret;
  95.     }
  96.  
  97.  
  98.  
  99.    
  100.     public Car findCarByVin(int vin) {
  101.         return null;  //To change body of implemented methods use File | Settings | File Templates.
  102.     }
  103.  
  104.     public Car editCar(Car car) {
  105.         return null;  //To change body of implemented methods use File | Settings | File Templates.
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement