Advertisement
Guest User

Untitled

a guest
Jun 18th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. package Profession;
  2.  
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.util.ArrayList;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9.  
  10. public class operation {
  11. private ArrayList <product> products = new ArrayList<product>();
  12.  
  13. public ArrayList<product> getProducts() {
  14. return products;
  15. }
  16.  
  17. public void setProducts(ArrayList<product> products) {
  18. this.products = products;
  19. }
  20.  
  21. public void add (product p){
  22. try {
  23. //1
  24. Class.forName("com.mysql.jdbc.Driver");
  25. //2
  26. java.sql.Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/productmanagement","root","");
  27. //3
  28. PreparedStatement pr = cn.prepareStatement("INSERT INTO product VALUES (NULL,?,?,?,?)");
  29. pr.setString(1, p.getName());
  30. pr.setString(2, p.getDescr());
  31. pr.setInt(3, p.getPrice());
  32. pr.setInt(4, p.getState());
  33.  
  34. //4
  35. pr.execute();
  36.  
  37. } catch (Exception ex) {
  38. //Logger.getLogger(operation.class.getName()).log(Level.SEVERE, null, ex);
  39. ex.printStackTrace();
  40. }
  41.  
  42. //products.add(p);
  43. }
  44. public void remove (Long id){
  45. try {
  46. //1
  47. Class.forName("com.mysql.jdbc.Driver");
  48. //2
  49. java.sql.Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/productmanagement","root","");
  50. //3
  51. PreparedStatement pr = cn.prepareStatement("DELETE FROM product WHERE id =?");
  52. pr.setLong(1, id);
  53.  
  54. //4
  55. pr.execute();
  56.  
  57. } catch (Exception ex) {
  58. //Logger.getLogger(operation.class.getName()).log(Level.SEVERE, null, ex);
  59. ex.printStackTrace();
  60. }
  61. /*for (product p : products ){
  62. if (p.getId()==id){
  63. products.remove(p);
  64. break;
  65. }
  66. }*/
  67.  
  68. }
  69. public ArrayList getAll(){
  70. ArrayList listproduct = new ArrayList<product>();
  71. try {
  72. //1
  73. Class.forName("com.mysql.jdbc.Driver");
  74. //2
  75. java.sql.Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/productmanagement","root","");
  76. //3
  77. PreparedStatement pr = cn.prepareStatement("SELECT * FROM product ORDER BY id");
  78. //4
  79. ResultSet rs = pr.executeQuery();
  80. //5
  81. while (rs.next()){
  82. product p = new product();
  83. p.setId (rs.getLong("id"));
  84. p.setName (rs.getString("name"));
  85. p.setDescr (rs.getString("descr"));
  86. p.setPrice (rs.getInt("price"));
  87. p.setState (rs.getInt("state"));
  88. listproduct.add(p);
  89. }
  90.  
  91. } catch (Exception ex) {
  92. //Logger.getLogger(operation.class.getName()).log(Level.SEVERE, null, ex);
  93. ex.printStackTrace();
  94. }
  95. return listproduct;
  96.  
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement