Advertisement
Guest User

Untitled

a guest
Apr 8th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  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> list = new ArrayList<>();
  31. try (Connection connection = DriverManager.getConnection(url,username,password);
  32. Statement stmt = connection.createStatement();
  33. ResultSet rs = stmt.executeQuery("SELECT * FROM customer")){
  34. int i;
  35. while(rs.next()) {
  36. i = 1;
  37. String t1 = rs.getString(i++);
  38. String t2 = rs.getString(i++);
  39. Customer customer = new Customer();
  40. customer.setFirstName(t1);
  41. customer.setLastName(t2);
  42. list.add(customer);
  43.  
  44. }
  45.  
  46. }
  47.  
  48. catch (SQLException e){
  49. throw new FetchDataException(e);
  50. }
  51.  
  52. return list;
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59. public List<Product> getAllProducts() throws FetchDataException {
  60. List<Product> productList = new ArrayList<>();
  61. try (Connection connection = DriverManager.getConnection(url,username,password);
  62. Statement stmt = connection.createStatement();
  63. ResultSet rs = stmt.executeQuery("SELECT * FROM product")){
  64. int i;
  65. while(rs.next()) {
  66. i = 1;
  67. int product_id = rs.getInt(i++);
  68. String product_name = rs.getString(i++);
  69. double product_price = rs.getDouble(i++);
  70. Product product = new Product();
  71. product.setProductName(product_name);
  72. product.setProductId(product_id);
  73. product.setPrice(product_price);
  74. productList.add(product);
  75.  
  76. }
  77.  
  78. }
  79.  
  80. catch (SQLException e){
  81. throw new FetchDataException(e);
  82. }
  83.  
  84. return productList;
  85. }
  86.  
  87. @Override
  88. public List<Order> getAllOrders() throws FetchDataException {
  89. return null;
  90. }
  91.  
  92. @Override
  93. public boolean addOrder(Customer c, Product p, Date date) throws SaveDataException {
  94. return false;
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement