Advertisement
Guest User

Untitled

a guest
Apr 11th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.db2019.dbshowcase.api;
  2.  
  3. import com.db2019.dbshowcase.exception.FetchDataException;
  4. import com.db2019.dbshowcase.exception.SaveDataException;
  5. import com.db2019.dbshowcase.model.*;
  6. import com.db2019.dbshowcase.util.DBPropertyLoader;
  7.  
  8. import java.sql.*;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. public class DataHandlerImpl implements DataHandler {
  13.     private static final DataHandler _instance = new DataHandlerImpl();
  14.     private final String url;
  15.     private final String username;
  16.     private final String password;
  17.  
  18.     private DataHandlerImpl() {
  19.         url = DBPropertyLoader.getProperty("url");
  20.         username = DBPropertyLoader.getProperty("username");
  21.         password = DBPropertyLoader.getProperty("password");
  22.     }
  23.  
  24.     public static DataHandler getInstance() {
  25.         return _instance;
  26.     }
  27.  
  28.     @Override
  29.     public List<Customer> getAllCustomers() throws FetchDataException {
  30.         List<Customer> customers = new ArrayList<>();
  31.  
  32.         try {
  33.             Connection con = DriverManager.getConnection(url, username, password);
  34.  
  35.             Statement st = con.createStatement();
  36.             String sql = ("SELECT * FROM customer");
  37.             ResultSet rs = st.executeQuery(sql);
  38.  
  39.             while (rs.next()) {
  40.                 Customer tmp = new Customer();
  41.                 int id = rs.getInt("customer_id");
  42.                 String firstName = rs.getString("first_name");
  43.                 String lastName = rs.getString("last_name");
  44.  
  45.                 tmp.setCustomerId(id);
  46.                 tmp.setFirstName(firstName);
  47.                 tmp.setLastName(lastName);
  48.  
  49.                 customers.add(tmp);
  50.             }
  51.  
  52.             con.close();
  53.         } catch (SQLException e) {
  54.             e.printStackTrace();
  55.         }
  56.  
  57.         return customers;
  58.     }
  59.  
  60.     @Override
  61.     public List<Product> getAllProducts() throws FetchDataException {
  62.         List<Product> products = new ArrayList<>();
  63.  
  64.         try {
  65.             Connection con = DriverManager.getConnection(url, username, password);
  66.  
  67.             Statement st = con.createStatement();
  68.             String sql = ("SELECT * FROM customer");
  69.             ResultSet rs = st.executeQuery(sql);
  70.  
  71.             while (rs.next()) {
  72.                 Product tmp = new Product();
  73.                 int id = rs.getInt("product_id");
  74.                 String productName = rs.getString("product_name");
  75.                 Double price = rs.getDouble("price");
  76.  
  77.                 tmp.setProductId(id);
  78.                 tmp.setProductName(productName);
  79.                 tmp.setPrice(price);
  80.  
  81.                 products.add(tmp);
  82.             }
  83.  
  84.             con.close();
  85.         } catch (SQLException e) {
  86.             e.printStackTrace();
  87.         }
  88.  
  89.         return products;
  90.     }
  91.  
  92.     @Override
  93.     public List<Order> getAllOrders() throws FetchDataException {
  94.         return null;
  95.     }
  96.  
  97.     @Override
  98.     public boolean addOrder(Customer c, Product p, Date date) throws SaveDataException {
  99.         return false;
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement