-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab_11_Java.java
354 lines (251 loc) · 12.1 KB
/
Lab_11_Java.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// ENSF 608
// Demo java program for connecting with databases
import java.sql.*;
public class Registration{
public final String DBURL;
public final String USERNAME;
public final String PASSWORD;
//Optional to include, but recommended
private Connection dbConnect;
private ResultSet results;
public Registration(String url, String user, String pw){
// Database URL
this.DBURL = url;
// Database credentials
this.USERNAME = user;
this.PASSWORD = pw;
}
//Must create a connection to the database, no arguments, no return value
public void initializeConnection(){
try{
dbConnect = DriverManager.getConnection(this.DBURL, this.USERNAME, this.PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
}
String getDburl(){
return this.DBURL;
}
String getUsername(){
return this.USERNAME;
}
String getPassword(){
return this.PASSWORD;
}
public String selectAllNames(String tableName){
StringBuffer foundNames = new StringBuffer();
try {
Statement myStmt = dbConnect.createStatement();
// Execute SQL query
results = myStmt.executeQuery("SELECT * FROM " + tableName);
// Process the results set
while (results.next()){
foundNames.append(results.getString("LName") + ", " + results.getString("FName"));
foundNames.append('\n');
}
foundNames.deleteCharAt(foundNames.length()-1);
myStmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return foundNames.toString();
}
public String showStudios(){
StringBuffer studioNames = new StringBuffer();
try {
Statement myStmt = dbConnect.createStatement();
// Execute SQL query
results = myStmt.executeQuery("SELECT * FROM studio");
// Process the results set
while (results.next()){
studioNames.append(results.getString("Name"));
studioNames.append('\n');
}
myStmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
studioNames.deleteCharAt(studioNames.length()-1);
return studioNames.toString();
}
public void insertNewCompetitor(String id, String lName, String fName, int age, String instrument, String teacherID){
if(!validateTeacher(teacherID)){
throw new IllegalArgumentException("Student must have a registered teacher.");
}
if(age < 5 || age > 18){
throw new IllegalArgumentException("Student must be between the ages of 5 and 18.");
}
try {
String query = "INSERT INTO competitor (CompetitorID, LName, FName, Age, Instrument, TeacherID) VALUES (?,?,?,?,?,?)";
PreparedStatement myStmt = dbConnect.prepareStatement(query);
myStmt.setString(1, id);
myStmt.setString(2, lName);
myStmt.setString(3, fName);
myStmt.setInt(4, age);
myStmt.setString(5, instrument);
myStmt.setString(6, teacherID);
int rowCount = myStmt.executeUpdate();
myStmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public void registerNewTeacher(String id, String lName, String fName, String phone, String studio, String studioPhone, String studioAddress){
if(validateTeacher(id)){
throw new IllegalArgumentException("Teacher is already registered.");
}
if(!validateStudio(studio)){
try {
String query = "INSERT INTO studio (Name, Phone, Address) VALUES (?,?,?)";
PreparedStatement myStmt = dbConnect.prepareStatement(query);
myStmt.setString(1, studio);
myStmt.setString(2, studioPhone);
myStmt.setString(3, studioAddress);
int rowCount = myStmt.executeUpdate();
myStmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
try {
String query = "INSERT INTO teacher (TeacherID, LName, FName, Phone, StudioName) VALUES (?,?,?,?,?)";
PreparedStatement myStmt = dbConnect.prepareStatement(query);
myStmt.setString(1, id);
myStmt.setString(2, lName);
myStmt.setString(3, fName);
myStmt.setString(4, phone);
myStmt.setString(5, studio);
int rowCount = myStmt.executeUpdate();
myStmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public boolean validateTeacher(String teacherID){
boolean validTeacher = false;
try {
Statement myStmt = dbConnect.createStatement();
// Execute SQL query
results = myStmt.executeQuery("SELECT * FROM teacher");
// Process the results set
while (results.next()){
if(results.getString("TeacherID").equals(teacherID))
validTeacher = true;
}
myStmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return validTeacher;
}
public boolean validateStudio(String studioName){
boolean studioExists = false;
try {
Statement myStmt = dbConnect.createStatement();
// Execute SQL query
results = myStmt.executeQuery("SELECT * FROM studio");
// Process the results set
while (results.next()){
if(results.getString("Name").equals(studioName))
studioExists = true;
}
myStmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return studioExists;
}
public void deleteCompetitor(String id){
try {
String query = "DELETE FROM competitor WHERE CompetitorID = ?";
PreparedStatement myStmt = dbConnect.prepareStatement(query);
myStmt.setString(1, id);
int rowCount = myStmt.executeUpdate();
myStmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public void deleteTeacher(String id){
try {
String query = "DELETE FROM teacher WHERE TeacherID = ?";
PreparedStatement myStmt = dbConnect.prepareStatement(query);
myStmt.setString(1, id);
int rowCount = myStmt.executeUpdate();
myStmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public void close() {
try {
results.close();
dbConnect.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//Use whatever account information you want on your system.
Registration myJDBC = new Registration("jdbc:mysql://localhost/competition","Marasco","ensf409");
myJDBC.initializeConnection();
System.out.println("------------------------------");
System.out.println("***Testing getter methods:***");
System.out.println(myJDBC.getDburl());
System.out.println(myJDBC.getUsername());
System.out.println(myJDBC.getPassword());
System.out.println("------------------------------");
System.out.println("***Printing list of competitors:***");
System.out.println(myJDBC.selectAllNames("competitor"));
System.out.println("------------------------------");
System.out.println("***Printing list of teachers:***");
System.out.println(myJDBC.selectAllNames("teacher"));
System.out.println("------------------------------");
System.out.println("***Printing list of studios:***");
System.out.println(myJDBC.showStudios());
System.out.println("------------------------------");
System.out.println("***Now testing insert statements...***");
myJDBC.insertNewCompetitor("234", "Robertson", "Ebba", 15, "Trombone", "9202");
System.out.println("Competitor #234 Robertson, Ebba should now be added.");
//Insertion should fail and throw an IllegalArgumentException
try{
myJDBC.insertNewCompetitor("678", "Jordan", "Ali", 10, "Oboe", "9807");
}catch(IllegalArgumentException e){
System.out.println("Sucessfully threw exception when no registered teacher can be found for a competitor.");
}
//Insertion should fail and throw an IllegalArgumentException
try{
myJDBC.insertNewCompetitor("654", "Smyth", "Ace", 4, "Oboe", "1012");
}catch(IllegalArgumentException e){
System.out.println("Sucessfully threw exception when competitor is outside valid age range.");
}
//Inserts a new teacher into the database
myJDBC.registerNewTeacher("2849", "Lacombe", "Trent", "403-764-3323", "Lyrica", "403-357-4457", "62 Heron Drive NW");
System.out.println("Teacher #2849 Lacombe, Trent should now be added.");
//Insertion should fail and throw an Illegal Argument Exception
try{
myJDBC.registerNewTeacher("0077", "Belma", "Doug", "403-646-3302", "Harmonius Learning", "403-954-5232", "294 Spruce Lane NE");
}catch(IllegalArgumentException e){
System.out.println("Sucessfully threw exception teacher is already registered.");
}
//A new studio should be added when the teacher's studio is not yet in the database
//Check to make sure the database has been updated with a new teacher AND a new studio
myJDBC.registerNewTeacher("6887", "McMurray", "Jill", "587-101-8790", "Music Notes", "587-746-9980", "99 Pinewood Way NE");
System.out.println("Teacher #6887 McMurray, Jill should now be added.");
System.out.println("Studio Music Notes should now be added.");
System.out.println("------------------------------");
System.out.println("***Now testing delete statements...***");
//Deletes the specified competitor from the database
myJDBC.deleteCompetitor("205");
System.out.println("Competitor #205 Kamilla, Mala should now be deleted.");
//Deletes the specified teacher from the database
myJDBC.deleteTeacher("3575");
System.out.println("Teacher #3575 Steele, Lucio should now be deleted.");
//Demonstrate that all Statement, ResultSet, and Connection objects are closed.
//Flexible implementation- likely Statements/PreparedStatements are closed in the methods that use them and
//ResultSet/Connection objects are likely closed in some sort of close method.
myJDBC.close();
System.out.println("------------------------------");
System.out.println("***End of tests.***");
}
}