Advertisement
Guest User

Untitled

a guest
Nov 16th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. package data;
  2.  
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import model.Category;
  9. import model.Product;
  10.  
  11. /*
  12. * To change this license header, choose License Headers in Project Properties.
  13. * To change this template file, choose Tools | Templates
  14. * and open the template in the editor.
  15. */
  16. /**
  17. *
  18. * @author Jasper
  19. */
  20. public abstract class DBShop {
  21.  
  22. private static final String URL = "jdbc:mysql://localhost:3333/shop";
  23. private static final String USER = "user";
  24. private static final String PWD = "";
  25.  
  26. private static DBShop instance;
  27. private Connection connection;
  28.  
  29. public static DBShop getInstance() {
  30. if (instance == null) {
  31. instance = new FileDB();
  32. }
  33. return instance;
  34. }
  35.  
  36. private void initConnection() {
  37. try {
  38. Class.forName("com.mysql.jdbc.Driver");
  39. connection = DriverManager.getConnection(URL, USER, PWD);
  40.  
  41. } catch (ClassNotFoundException ex) {
  42. ex.printStackTrace();
  43. } catch (SQLException ex) {
  44. ex.printStackTrace();
  45. }
  46. }
  47.  
  48. public void addProduct(String name, double price) {
  49. try {
  50.  
  51. String sql = "insert into shop.product(name, price) values(?,?)";
  52. PreparedStatement prep = connection.prepareStatement(sql);
  53. prep.setString(1, name);
  54. prep.setDouble(2, price);
  55. prep.executeUpdate();
  56. prep.close();
  57. } catch (SQLException ex) {
  58. ex.printStackTrace();
  59. }
  60. }
  61.  
  62. public void addCategory(int id, String name) {
  63. try {
  64.  
  65. String sql = "insert into shop.category(id, name) values(?,?)";
  66. PreparedStatement prep = connection.prepareStatement(sql);
  67. prep.setInt(1, id);
  68. prep.setString(2, name);
  69. prep.executeUpdate();
  70. prep.close();
  71. } catch (SQLException ex) {
  72. ex.printStackTrace();
  73. }
  74. }
  75.  
  76. public void removeCategory(int id) {
  77. try {
  78.  
  79. String sql = "DELETE FROM shop.category WHERE id="+id;
  80. Statement stmt = connection.createStatement();
  81. stmt.executeUpdate(sql);
  82. } catch (SQLException ex) {
  83. ex.printStackTrace();
  84. }
  85. }
  86.  
  87. public List<Product> getAllProducts() {
  88. List<Product> products = new ArrayList<>();
  89.  
  90. try {
  91. String sql = "select * from shop.product";
  92. Statement stmt = connection.createStatement();
  93. ResultSet rs = stmt.executeQuery(sql);
  94.  
  95. while (rs.next()) {
  96. products.add(new Product(rs.getString("name"), rs.getDouble("price"), rs.getString("id")));
  97. }
  98. } catch (SQLException ex) {
  99. ex.printStackTrace();
  100. }
  101.  
  102. return products;
  103. }
  104.  
  105. public List<Category> getCategories() {
  106. List<Category> categories = new ArrayList<>();
  107.  
  108. try {
  109. String sql = "select * from shop.category";
  110. Statement stmt = connection.createStatement();
  111. ResultSet rs = stmt.executeQuery(sql);
  112.  
  113. while (rs.next()) {
  114. categories.add(new Category(rs.getInt("id"), rs.getString("name")));
  115. }
  116. } catch (SQLException ex) {
  117. ex.printStackTrace();
  118. }
  119.  
  120. return categories;
  121. }
  122.  
  123. public DBShop() {
  124. initConnection();
  125. }
  126.  
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement