-
Notifications
You must be signed in to change notification settings - Fork 9
/
ProductQueries.java
116 lines (97 loc) · 4.04 KB
/
ProductQueries.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// The PreparedStatements that are used in the GameSpot application.
package gamestore.model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ProductQueries {
private static final String URL = "jdbc:mysql://localhost:3306/gamespot_db?zeroDateTimeBehavior=convertToNull";
private static final String USERNAME = "root";
private static final String PASSWORD = "nbuser";
private Connection connection; // manages the connection
private PreparedStatement selectAllProducts;
private PreparedStatement selectAlreadyPurchasedProducts;
// Constructor
public ProductQueries() {
try {
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
// creates the query for all the records in Product table
selectAllProducts = connection.prepareStatement("SELECT * FROM products");
// creates the query for all purchased products
selectAlreadyPurchasedProducts = connection.prepareStatement("SELECT ProductID FROM orders WHERE CustomerID = ?");
} catch (SQLException sqlException) {
sqlException.printStackTrace();
System.exit(1);
}
}
public List<Product> getAllProducts() {
List<Product> results = null;
ResultSet resultSet = null;
try {
// executeQuery returns a ResultSet that contains the desired records
resultSet = selectAllProducts.executeQuery();
results = new ArrayList<Product>();
while (resultSet.next()) {
results.add(new Product(
resultSet.getInt("productID"),
resultSet.getString("title"),
resultSet.getDouble("price"),
resultSet.getString("description"),
resultSet.getString("video"),
resultSet.getString("platform"),
resultSet.getString("cover"),
resultSet.getString("image1"),
resultSet.getString("image2"),
resultSet.getString("image3"),
resultSet.getString("image4"),
resultSet.getString("exeFile")
));
}
} catch (SQLException sqlException) {
sqlException.printStackTrace();
} finally {
try {
resultSet.close();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
close();
}
}
return results;
}
public List<Integer> getAlreadyPurchasedProducts(int customerID) {
List<Integer> results = null;
ResultSet resultSet = null;
try {
selectAlreadyPurchasedProducts.setInt(1, customerID);
// executeQuery returns a ResultSet with all the desired records
resultSet = selectAlreadyPurchasedProducts.executeQuery();
results = new ArrayList<Integer>();
while(resultSet.next()) {
int productID = resultSet.getInt("productID");
results.add(productID);
}
} catch(SQLException sqlException) {
sqlException.printStackTrace();
} finally {
try {
resultSet.close();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
close();
}
}
return results;
}
// closes the connection with the database
public void close() {
try {
connection.close();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
}
}