Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.db2019.dbshowcase.api;
- import com.db2019.dbshowcase.exception.FetchDataException;
- import com.db2019.dbshowcase.exception.SaveDataException;
- import com.db2019.dbshowcase.model.*;
- import com.db2019.dbshowcase.util.DBPropertyLoader;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.List;
- public class DataHandlerImpl implements DataHandler {
- private static final DataHandler _instance = new DataHandlerImpl();
- private final String url;
- private final String username;
- private final String password;
- private DataHandlerImpl() {
- url = DBPropertyLoader.getProperty("url");
- username = DBPropertyLoader.getProperty("username");
- password = DBPropertyLoader.getProperty("password");
- }
- public static DataHandler getInstance() {
- return _instance;
- }
- @Override
- public List<Customer> getAllCustomers() throws FetchDataException {
- List<Customer> list = new ArrayList<>();
- try (Connection connection = DriverManager.getConnection(url,username,password);
- Statement stmt = connection.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM customer")){
- int i;
- while(rs.next()) {
- i = 1;
- String t1 = rs.getString(i++);
- String t2 = rs.getString(i++);
- Customer customer = new Customer();
- customer.setFirstName(t1);
- customer.setLastName(t2);
- list.add(customer);
- }
- }
- catch (SQLException e){
- throw new FetchDataException(e);
- }
- return list;
- }
- public List<Product> getAllProducts() throws FetchDataException {
- List<Product> productList = new ArrayList<>();
- try (Connection connection = DriverManager.getConnection(url,username,password);
- Statement stmt = connection.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM product")){
- int i;
- while(rs.next()) {
- i = 1;
- int product_id = rs.getInt(i++);
- String product_name = rs.getString(i++);
- double product_price = rs.getDouble(i++);
- Product product = new Product();
- product.setProductName(product_name);
- product.setProductId(product_id);
- product.setPrice(product_price);
- productList.add(product);
- }
- }
- catch (SQLException e){
- throw new FetchDataException(e);
- }
- return productList;
- }
- @Override
- public List<Order> getAllOrders() throws FetchDataException {
- return null;
- }
- @Override
- public boolean addOrder(Customer c, Product p, Date date) throws SaveDataException {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement