Advertisement
Guest User

Untitled

a guest
Nov 5th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. package domain.db;
  2.  
  3. import domain.model.Product;
  4.  
  5. import java.sql.*;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Properties;
  9.  
  10. public class ProductDbSQL implements ProductDb {
  11. private Properties properties = new Properties();
  12. private String url = "jdbc:postgresql://databanken.ucll.be:51819/2TX33?currentSchema=r0702435";
  13.  
  14. public ProductDbSQL() {
  15. properties.setProperty("user", "local_r0702435");
  16. properties.setProperty("password", "jDssCQ9Bpfdz;§t");
  17. properties.setProperty("ssl", "true");
  18. properties.setProperty("sslfactory", "org.postgresql.ssl.NonValidatingFactory");
  19. properties.setProperty("sslmode","prefer");
  20. try {
  21. Class.forName("org.postgresql.Driver");
  22. } catch (ClassNotFoundException e) {
  23. throw new DbException(e.getMessage(), e);
  24. }
  25.  
  26. }
  27.  
  28. @Override
  29. public void add(Product product) {
  30. if (product == null) {
  31. throw new DbException("Nothing to add.");
  32. }
  33. String sql = "INSERT INTO product(productid,name,description,price) VALUES (?,?,?,?)";
  34. try (Connection connection = DriverManager.getConnection(url, properties);
  35. PreparedStatement statement = connection.prepareStatement(sql)) {
  36. statement.setInt(1, product.getProductId());
  37. statement.setString(2, product.getName());
  38. statement.setString(3, product.getDescription());
  39. statement.setDouble(4, product.getPrice());
  40. statement.execute();
  41. } catch (SQLException e) {
  42. throw new DbException(e);
  43. }
  44. }
  45.  
  46. @Override
  47. public List<Product> getAll() {
  48. List<Product> producten = new ArrayList<Product>();
  49. try (Connection connection = DriverManager.getConnection(url, properties);
  50. Statement statement = connection.createStatement()){
  51. ResultSet result = statement.executeQuery("SELECT * FROM product");
  52. while (result.next()) {
  53. String id = result.getString("productid");
  54. String name = result.getString("name");
  55. String description = result.getString("description");
  56. String price = result.getString("price");
  57. Product product = new Product(Integer.parseInt(id),name,description,Double.parseDouble(price));
  58. producten.add(product);
  59. }
  60. } catch (SQLException e) {
  61. throw new DbException(e.getMessage(), e);
  62. }
  63. return producten;
  64. }
  65.  
  66. @Override
  67. public Product get(int productId)
  68. {
  69. String sql = "SELECT * FROM product WHERE productid = ?";
  70. try (Connection connection = DriverManager.getConnection(url, properties);
  71. PreparedStatement statement = connection.prepareStatement(sql)){
  72. statement.setInt(1, productId);
  73. ResultSet result = statement.executeQuery();
  74. result.next();
  75. String id = result.getString("productid");
  76. String name = result.getString("name");
  77. String description = result.getString("description");
  78. String price = result.getString("price");
  79. Product product = new Product(Integer.parseInt(id),name,description,Double.parseDouble(price));
  80. return product;
  81. } catch (SQLException e) {
  82. throw new DbException(e.getMessage(), e);
  83. }
  84. }
  85.  
  86. @Override
  87. public void update(Product product)
  88. {
  89. String sql = "UPDATE product SET" +
  90. "naam = ?," +
  91. "description = ?," +
  92. "price = ?" +
  93. "WHERE productid = ?";
  94. try(Connection connection = DriverManager.getConnection(url,properties);
  95. PreparedStatement statement = connection.prepareStatement(sql)) {
  96. statement.setString(1,product.getName());
  97. statement.setString(2,product.getDescription());
  98. statement.setDouble(3,product.getPrice());
  99. statement.setInt(4,product.getProductId());
  100. statement.execute();
  101. }catch (SQLException e){
  102. throw new DbException(e.getMessage(),e);
  103. }
  104. }
  105.  
  106. @Override
  107. public void delete(int productId)
  108. {
  109. String sql = "DELETE FROM product WHERE productid = ?";
  110. try (Connection connection = DriverManager.getConnection(url, properties);
  111. PreparedStatement statement = connection.prepareStatement(sql)){
  112. statement.setInt(1,productId);
  113. statement.execute();
  114. } catch (SQLException e) {
  115. throw new DbException(e.getMessage(), e);
  116. }
  117. }
  118.  
  119. @Override
  120. public int getNumberOfProducts()
  121. {
  122. try (Connection connection = DriverManager.getConnection(url, properties);
  123. Statement statement = connection.createStatement()){
  124. ResultSet result = statement.executeQuery("SELECT productid FROM product");
  125. int teller = 0;
  126. while (result.next()) {
  127. teller++;
  128. }
  129. return teller;
  130. } catch (SQLException e) {
  131. throw new DbException(e.getMessage(), e);
  132. }
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement