-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCustomerQueries.java
144 lines (118 loc) · 5.38 KB
/
CustomerQueries.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// 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 CustomerQueries {
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 selectCustomerByUserName;
private PreparedStatement selectCustomerByPassword;
private PreparedStatement insertNewCustomer;
// Constructor
public CustomerQueries() {
try {
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
// creates the query for the records of Customer table with specific UserName
selectCustomerByUserName = connection.prepareStatement("SELECT * FROM customers WHERE UserName = ?");
// creates the query for the records of Customer table with specific Password
selectCustomerByPassword = connection.prepareStatement("SELECT * FROM customers WHERE Password = ?");
//creates the query for inserting an new record in the Customer table
insertNewCustomer = connection.prepareStatement("INSERT INTO customers " + " (UserName, Password, Email, CCNumber) " + "VALUES (?, ?, ?, ?)");
} catch (SQLException sqlException) {
sqlException.printStackTrace();
System.exit(1);
}
}
//Selects Customer with UserName for Login Form
public List<Customer> getCustomerByUserName(String userName) {
List<Customer> results = null;
ResultSet resultSet = null;
try {
selectCustomerByUserName.setString(1, userName);
// executeQuery returns a ResultSet that contains the desired records
resultSet = selectCustomerByUserName.executeQuery();
results = new ArrayList<Customer>();
while(resultSet.next()) {
results.add(new Customer(
resultSet.getInt("customerID"),
resultSet.getString("userName"),
resultSet.getString("email"),
resultSet.getString("password"),
resultSet.getString("ccNumber"),
resultSet.getDouble("balance")));
}
} catch (SQLException sqlException) {
sqlException.printStackTrace();
} finally {
try {
resultSet.close();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
close();
}
}
return results;
}
// Selects Customer with Password for Login Form
public List<Customer> getCustomerByPassword(String password) {
List<Customer> results = null;
ResultSet resultSet = null;
try {
selectCustomerByPassword.setString(1, password);
// executeQuery returns a ResultSet that contains the desired records
resultSet = selectCustomerByPassword.executeQuery();
results = new ArrayList<Customer>();
while (resultSet.next()) {
results.add(new Customer(resultSet.getInt("customerID"),
resultSet.getString("userName"),
resultSet.getString("email"),
resultSet.getString("password"),
resultSet.getString("ccNumber"),
resultSet.getDouble("balance"))
);
}
} catch (SQLException sqlException) {
sqlException.printStackTrace();
} finally {
try {
resultSet.close();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
close();
}
}
return results;
}
// Insert new Customer for Register Form
public int addCustomer(String userName, String password, String email, String ccNumber) {
int result = 0;
// defines parameters and then runs insertNewCustomer
try {
insertNewCustomer.setString(1, userName);
insertNewCustomer.setString(2, password);
insertNewCustomer.setString(3, email);
insertNewCustomer.setString(4, ccNumber);
// inserts the new record and returns the number of the lines that update
result = insertNewCustomer.executeUpdate();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
close();
}
return result;
}
// close database connection
public void close() {
try {
connection.close();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
}
}