-
Notifications
You must be signed in to change notification settings - Fork 0
/
DAOImpl.java
100 lines (89 loc) · 4.29 KB
/
DAOImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.task1;
import javax.swing.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class DAOImpl implements ProductDAO {
public void insertProduct(Product product) {
try {
Class.forName ("com.mysql.jdbc.Driver");
Connection myConn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/product", "root", "");
PreparedStatement psst;
psst = myConn.prepareStatement ("insert into product values (?,?,?,?,?)");
psst.setString (1, product.getID());
psst.setString (2, product.getType());
psst.setString (3, product.getManufacturer());
psst.setString (4, product.getProductionDate());
psst.setString (5, product.getExpiryDate());
int i = psst.executeUpdate();
if (i > 0) {
JOptionPane.showMessageDialog (null, "Product was successfully added");
} else {
JOptionPane.showMessageDialog (null, "Product was not added");
}
} catch (Exception var5) {
JOptionPane.showMessageDialog (null, var5);
}
}
public void deleteProduct(String productID) {
try {
Class.forName ("com.mysql.jdbc.Driver");
Connection myConn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/product", "root", "");
PreparedStatement psst;
psst = myConn.prepareStatement ("delete from Product where Product_ID = ?");
psst.setString (1, productID);
int i = psst.executeUpdate();
if (i > 0) {
JOptionPane.showMessageDialog (null, "Product was successfully deleted");
} else {
JOptionPane.showMessageDialog (null, "Product was not deleted");
}
} catch (Exception var5) {
JOptionPane.showMessageDialog (null, var5);
}
}
public void updateProduct(Product product) {
try {
Class.forName ("com.mysql.jdbc.Driver");
Connection myConn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/product", "root", "");
PreparedStatement psst;
psst = myConn.prepareStatement ("update product set Type = ?, Manufacturer = ?, Production_Date = ?, Expiry_Date = ? where Product_ID = ?");
psst.setString (1, product.getType());
psst.setString (2, product.getManufacturer());
psst.setString (3, product.getProductionDate());
psst.setString (4, product.getExpiryDate());
psst.setString (5, product.getID());
int i = psst.executeUpdate();
if (i > 0) {
JOptionPane.showMessageDialog (null, "Product was successfully updated");
} else {
JOptionPane.showMessageDialog (null, "Product was not updated");
}
} catch (Exception var5) {
JOptionPane.showMessageDialog (null, var5);
}
}
public List<Product> retrieveProduct(String manufacturer) {
List<Product> productList = new ArrayList<Product>();
try {
Class.forName ("com.mysql.jdbc.Driver");
Connection myConn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/product", "root", "");
PreparedStatement psst;
psst = myConn.prepareStatement ("select * from product where Manufacturer = ?");
psst.setString (1, manufacturer);
ResultSet myRs = psst.executeQuery();
while (myRs.next()) {
Product retrievedProduct = new Product(myRs.getString("Product_ID"));
retrievedProduct.setType(myRs.getString("Type"));
retrievedProduct.setManufacturer(myRs.getString("Manufacturer"));
retrievedProduct.setProductionDate(myRs.getString("Production_Date"));
retrievedProduct.setExpiryDate(myRs.getString("Expiry_Date"));
productList.add(retrievedProduct);
//System.out.println(myRs.getString("Type") + "," + myRs.getString("Product_ID"));
}
} catch (Exception var5) {
JOptionPane.showMessageDialog (null, var5);
}
return productList;
}
}