Advertisement
Guest User

Untitled

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