-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMemberSQL.java
227 lines (208 loc) · 8.2 KB
/
MemberSQL.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import java.sql.*;
import java.text.ParseException;
import java.sql.SQLException;
public class MemberSQL{
//This connects to the database by calling the login file
static Connection connection = Login.connection;
static Statement statement = Login.statement;
static ResultSet result = Login.result;
/**
* Method is called in Member file where user wants to see all the Members in the database. This method
* will perform the sql statement and generate a result set of all the Members in the database.
* @return ResultSet
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static ResultSet viewAllMembers() throws ClassNotFoundException, SQLException, ParseException{
//A table of data representing a database result set, which is usually generated by executing a
//statement that queries the database.
ResultSet returnSet = null;
//Object that represents a precompiled SQL statement
PreparedStatement ps = null;
try {
ps = connection.prepareStatement("SELECT * FROM Member;");
returnSet = ps.executeQuery();
ps.close();
} catch (SQLException e) {
System.out.println("SQL Exception");
e.getStackTrace();
return null;
}
return returnSet;
}
/**
* Method is called in the Member file and is used to find a specific member.
* @param memberID
* @return ResultSet
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static ResultSet viewMember(int memberID) throws ClassNotFoundException, SQLException, ParseException{
//A table of data representing a database result set, which is usually generated by executing a
//statement that queries the database.
ResultSet returnSet = null;
//Object that represents a precompiled SQL statement
PreparedStatement ps = null;
try {
ps = connection.prepareStatement("SELECT * FROM Member WHERE memberID = ?;");
ps.setInt(1, memberID);
returnSet = ps.executeQuery();
ps.close();
} catch (SQLException e) {
System.out.println("SQL Exception");
e.getStackTrace();
return null;
}
return returnSet;
}
/**
* Method is called from Member file and is called after the user has finished filling all the attributes for a Member
* @param memberID
* @param firstName
* @param lastName
* @param level
* @param email
* @param phone
* @param address
* @param activeStatus
* @param rewardAmount
* @throws ParseException
*/
static void addMember(int memberID, String firstName, String lastName, String level, String email, String phone, String address, boolean activeStatus, double rewardAmount) throws SQLException, ParseException{
//Object that represents a precompiled SQL statement
PreparedStatement ps = null;
int id = 0;
try{
ps = connection.prepareStatement("INSERT INTO Member (memberID, firstname, lastname, level, email, phone, address, activeStatus, rewardAmount) VALUES (?,?,?,?,?,?,?,?,?);");
ps.setInt(1,memberID);
ps.setString(2,firstName);
ps.setString(3,lastName);
ps.setString(4,level);
ps.setString(5, email);
ps.setString(6,phone);
ps.setString(7,address);
ps.setBoolean(8, activeStatus);
ps.setDouble(9, rewardAmount);
id = ps.executeUpdate();
connection.commit();
ps.close();
System.out.println(id);
if(id > 0){
System.out.println("Member added successfully");
} else{
System.out.println("Member not added");
}
}
catch (SQLException e) {
System.out.println("SQL Exception");
connection.rollback();
e.printStackTrace();
}
}
/**
* Method is called from Member file and is called after the user has finished filling all the attributes that they want to edit
* for a Member
* @param memberID
* @param firstName
* @param lastName
* @param level
* @param email
* @param phone
* @param address
* @param activeStatus
* @param rewardAmount
* @throws ParseException
*/
static void editMember( int memberID,String firstName, String lastName, String level, String email, String phone, String address, boolean activeStatus, double rewardAmount) throws SQLException, ParseException{
//Object that represents a precompiled SQL statement
PreparedStatement ps = null;
int id = 0;
try{
ps = connection.prepareStatement("UPDATE Member SET firstname = ?, lastname = ?, level = ?, email = ?, phone = ?, address = ?, rewardAmount = ?, activeStatus = ? WHERE memberID = ?;");
ps.setString(1,firstName);
ps.setString(2,lastName);
ps.setString(3,level);
ps.setString(4, email);
ps.setString(5,phone);
ps.setString(6,address);
ps.setBoolean(7, activeStatus);
ps.setDouble(8, rewardAmount);
ps.setInt(9,memberID);
id = ps.executeUpdate();
connection.commit();
ps.close();
System.out.println(id);
if(id > 0){
System.out.println("Member updated successfully");
} else{
System.out.println("Member not updated");
}
}
catch (SQLException e) {
System.out.println("SQL Exception");
connection.rollback();
e.printStackTrace();
}
}
/**
* Method is called from Member file and is called to delete a member from the database
* @param memberID
*/
static void deleteMember(int memberID){
try {
PreparedStatement ps = connection.prepareStatement("DELETE FROM Member WHERE memberID = ?;");
ps.setInt(1, memberID);
int id = ps.executeUpdate();
System.out.println(id);
if (id > 0) {
System.out.println("Member deleted");
} else {
System.out.println("Member not deleted");
}
} catch (SQLException e) {
System.out.println("SQL Exception");
e.printStackTrace();
}
}
/**
* Method is called in Member file under rewardCheck. This method will perform the sql query in order to
* generate a members yearly rewards check. Before the query is generated, it will check if the member is
* Platinum level.
* @param memberID
* @throws SQLException
* @throws ParseException
*/
static ResultSet rewardCheck(int memberID) throws SQLException, ParseException{
//Object that represents a precompiled SQL statement
PreparedStatement ps = null;
PreparedStatement memberSearch = null;
//A table of data representing a database result set, which is usually generated by executing a
//statement that queries the database.
ResultSet member = null;
ResultSet resultSet = null;
try{
// Check if member is Platinum level
memberSearch = connection.prepareStatement("SELECT * FROM Member WHERE memberID = ?;");
memberSearch.setInt(1, memberID);
member = memberSearch.executeQuery();
if (!member.next()) {
return null;
}
boolean isPlatinum = member.getString("level").toLowerCase().equals("platinum");
if (isPlatinum) {
ps = connection.prepareStatement("SELECT rewardAmount FROM Member WHERE memberID = ?;");
ps.setInt(1,memberID);
resultSet = ps.executeQuery();
}
ps.close();
}
catch(SQLException e){
System.out.println("SQL Exception");
e.printStackTrace();
return null;
}
return resultSet;
}
}