forked from Aingty/CECS-343-Project
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Database_Client.java
587 lines (525 loc) · 29.2 KB
/
Database_Client.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
import java.sql.*;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
/* This portion is used to set up the Database Client. User will be asked
to present his/her Username and Password to connect to the Database.
*/
public class Database_Client {
static String USER;
static String PASS;
static String DBNAME;
static String displayFormat="%-15s\n";
static final String JDBC_DRIVER = "org.apache.derby.jdbc.ClientDriver";
static String DB_URL = "jdbc:derby://localhost:1527/";
static String dispNull (String input) {
//because of short circuiting, if it's null, it never checks the length.
if (input == null || input.length() == 0)
return "N/A";
else
return input;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Scanner in1 = new Scanner(System.in);
System.out.print("Name of the database (not the user account): ");
//DBNAME = "Java DataBase Project";
DBNAME = in.nextLine();
System.out.print("Database user name: ");
//USER = "Aingty";
USER = in.nextLine();
System.out.print("Database password: ");
//PASS = "12345";
PASS = in.nextLine();
DB_URL = DB_URL + DBNAME + ";user="+ USER + ";password=" + PASS;
Connection conn = null;
Statement stmt = null;
PreparedStatement preStmt = null;
String userMenuChoice;
int userInnerChoice = 0;
ArrayList<String> myArray;
ArrayList<String> myArray1; // This is only used for selecting from 2 primary keys of book
String sql;
boolean keepGoing = true;
try {
//STEP 2: Register JDBC driver
Class.forName("org.apache.derby.jdbc.ClientDriver");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL);
System.out.println("Database Connected");
while (keepGoing)
{
System.out.println("Please choose what to do");
System.out.println(" 1. List all Writing Group");
System.out.println(" 2. List all Data of a Group (User's input required)");
System.out.println(" 3. List all Publishers");
System.out.println(" 4. List all Data of a Publisher (User's input required)");
System.out.println(" 5. List all Book Titles (Titles Only)");
System.out.println(" 6. List all Data of a Book (User's input required)");
System.out.println(" 7. Insert a new Book");
System.out.println(" 8. Insert a new Publisher (Followed by a replacing of an old Publisher)");
System.out.println(" 9. Remove a Book");
System.out.println(" 10. Exit");
userMenuChoice = in.nextLine();
//While loop to check user's valid input
while(!(userMenuChoice.equals("1") | userMenuChoice.equals("2") | userMenuChoice.equals("3") | userMenuChoice.equals("4") | userMenuChoice.equals("5") | userMenuChoice.equals("6") | userMenuChoice.equals("7") | userMenuChoice.equals("8") | userMenuChoice.equals("9") | userMenuChoice.equals("10")))
{
System.out.println("Your input is invalid, please choose a valid input from 1-10.");
userMenuChoice = in.nextLine();
}
//If user picked to list all Writing Group
if (userMenuChoice.equals("1"))
{
stmt = conn.createStatement();
sql = "SELECT * FROM writinggroup ORDER BY groupName ASC";
ResultSet rs = stmt.executeQuery(sql);
displayFormat = "%-25s%-25s%-25s%-25s\n";
System.out.printf(displayFormat, "GroupName:", "HeadWriter:", "YearFormed:", "Subject:");
while (rs.next())
{
String groupName = rs.getString("GroupName");
String headWriter = rs.getString("HeadWriter");
String yearFormed = rs.getString("YearFormed");
String subject = rs.getString("Subject");
System.out.printf(displayFormat, dispNull(groupName), dispNull(headWriter), dispNull(yearFormed), dispNull(subject));
}
System.out.println("");
}
//If user picked to List all data for a group
if (userMenuChoice.equals("2"))
{
stmt = conn.createStatement();
sql = "SELECT distinct groupName FROM writinggroup ORDER BY groupName ASC";
ResultSet rs = stmt.executeQuery(sql);
System.out.print(" "); //Print this 3 spaces for indenting when numbering
displayFormat = "%-25s\n";
System.out.printf(displayFormat, "GroupName:");
// Instantiating Array and User's internal option
myArray = new ArrayList<String>();
userInnerChoice = 0;
int i = 1; //Used for numbering
while (rs.next())
{
String groupName = rs.getString("GroupName");
myArray.add(groupName);
System.out.print(i+". ");
System.out.printf(displayFormat, dispNull(groupName));
i++;
}
System.out.println("");
if (myArray.size() == 0)
{
System.out.println("There are no Writing Groups in the Database. Back to Main Menu.");
}
else
{
//Validating User's input for the book to be listed
while (userInnerChoice <= 0 || userInnerChoice > myArray.size())
{
System.out.println("Please Choose the Writing Group to be listed: (1-"+myArray.size()+")");
try{
userInnerChoice = in1.nextInt();
}catch (InputMismatchException e){
System.out.println("Sorry, invalid input. Please choose a number between 1 and "+ myArray.size()+".");
in1.next();
continue;
}
if (userInnerChoice <= 0 || userInnerChoice > myArray.size())
{
System.out.println("Sorry, invalid input. Please choose a number between 1 and "+ myArray.size()+".");
}
else
{
break;
}
}
sql = "SELECT * FROM writinggroup WHERE groupName=?";
preStmt = conn.prepareStatement(sql);
preStmt.setString(1, myArray.get(userInnerChoice-1));
rs = preStmt.executeQuery();
System.out.println("Retriving the group \""+myArray.get(userInnerChoice-1)+"\" Data.\n");
displayFormat = "%-25s%-25s%-25s%-25s\n";
System.out.printf(displayFormat, "GroupName:", "HeadWriter:", "YearFormed:", "Subject:");
while (rs.next())
{
String groupName = rs.getString("GroupName");
String headWriter = rs.getString("HeadWriter");
String yearFormed = rs.getString("YearFormed");
String subject = rs.getString("Subject");
System.out.printf(displayFormat, dispNull(groupName), dispNull(headWriter), dispNull(yearFormed), dispNull(subject));
}
}
System.out.println("");
}
//If user picked to list all Publisher
if (userMenuChoice.equals("3"))
{
stmt = conn.createStatement();
sql = "SELECT * FROM publishers ORDER BY publishername ASC";
ResultSet rs = stmt.executeQuery(sql);
displayFormat = "%-25s%-25s%-25s%-25s\n";
System.out.printf(displayFormat, "PublisherName:","PublisherAddress:", "PublisherPhone:", "PublisherEmail:");
while (rs.next())
{
String pubName = rs.getString("PublisherName");
String pubAddress = rs.getString("PublisherAddress");
String pubPhone = rs.getString("PublisherPhone");
String pubEmail = rs.getString("PublisherEmail");
System.out.printf(displayFormat, dispNull(pubName), dispNull(pubAddress), dispNull(pubPhone), dispNull(pubEmail));
}
System.out.println("");
}
//If user picked to list all data of a Publisher
if (userMenuChoice.equals("4"))
{
stmt = conn.createStatement();
sql = "SELECT distinct publishername FROM publishers ORDER BY publishername ASC";
ResultSet rs = stmt.executeQuery(sql);
System.out.print(" "); //Print this 3 spaces for indenting when numbering
displayFormat = "%-25s\n";
System.out.printf(displayFormat, "PublisherName:");
// Instantiating Array and User's internal option
myArray = new ArrayList<String>();
userInnerChoice = 0;
int i = 1; //Used for numbering
while (rs.next())
{
String pubName = rs.getString("PublisherName");
myArray.add(pubName);
System.out.print(i+". ");
System.out.printf(displayFormat, dispNull(pubName));
i++;
}
System.out.println("");
if (myArray.size() == 0)
{
System.out.println("There are no Publishers in the Database. Back to Main Menu.");
}
else
{
//Validating User's input for the book to be listed
while (userInnerChoice <= 0 || userInnerChoice > myArray.size())
{
System.out.println("Please Choose the Publishers to be listed: (1-"+myArray.size()+")");
try{
userInnerChoice = in1.nextInt();
}catch (InputMismatchException e){
System.out.println("Sorry, invalid input. Please choose a number between 1 and "+ myArray.size()+".");
in1.next();
continue;
}
if (userInnerChoice <= 0 || userInnerChoice > myArray.size())
{
System.out.println("Sorry, invalid input. Please choose a number between 1 and "+ myArray.size()+".");
}
else
{
break;
}
}
sql = "SELECT * FROM publishers WHERE publishername=?";
preStmt = conn.prepareStatement(sql);
preStmt.setString(1, myArray.get(userInnerChoice-1));
rs = preStmt.executeQuery();
System.out.println("Retriving the Publisher \""+myArray.get(userInnerChoice-1)+"\" Data.\n");
displayFormat = "%-25s%-25s%-25s%-25s\n";
System.out.printf(displayFormat, "PublisherName:", "PublisherAddress:", "PublisherPhone:", "PublisherEmail:");
while (rs.next())
{
String pubName = rs.getString("PublisherName");
String pubAddress = rs.getString("PublisherAddress");
String pubPhone = rs.getString("PublisherPhone");
String pubEmail = rs.getString("PublisherEmail");
System.out.printf(displayFormat, dispNull(pubName), dispNull(pubAddress), dispNull(pubPhone), dispNull(pubEmail));
}
}
System.out.println("");
}
//If user picked to list all book titles
if (userMenuChoice.equals("5"))
{
stmt = conn.createStatement();
sql = "SELECT booktitle FROM book ORDER BY booktitle ASC";
ResultSet rs = stmt.executeQuery(sql);
displayFormat = "%-25s\n";
System.out.printf(displayFormat, "BookTitle:");
while (rs.next())
{
String bookTitle = rs.getString("BookTitle");
System.out.printf(displayFormat, dispNull(bookTitle));
}
System.out.println("");
}
//If user picked to List a book
if (userMenuChoice.equals("6"))
{
stmt = conn.createStatement();
sql = "SELECT * FROM book ORDER BY bookTitle ASC";
ResultSet rs = stmt.executeQuery(sql);
System.out.print(" "); //Print this 3 spaces for indenting when numbering
displayFormat = "%-25s%-25s\n";
System.out.printf(displayFormat, "GroupName:","BookTitle:");
// Instantiating Array and User's internal option
myArray = new ArrayList<String>();
myArray1 = new ArrayList<String>();
userInnerChoice = 0;
int i = 1; //Used for numbering
while (rs.next())
{
String bookGroupName = rs.getString("GroupName");
String bookTitle = rs.getString("BookTitle");
myArray1.add(bookGroupName);
myArray.add(bookTitle);
System.out.print(i+". ");
System.out.printf(displayFormat, dispNull(bookGroupName), dispNull(bookTitle));
i++;
}
if (myArray.size() == 0)
{
System.out.println("There are no books in the Database. Back to Main Menu.");
}
else
{
//Validating User's input for the book to be listed
while (userInnerChoice <= 0 || userInnerChoice > myArray.size())
{
System.out.println("Please Choose the book to be listed: (1-"+myArray.size()+")");
try{
userInnerChoice = in1.nextInt();
}catch (InputMismatchException e){
System.out.println("Sorry, invalid input. Please choose a number between 1 and "+ myArray.size()+".");
in1.next();
continue;
}
if (userInnerChoice <= 0 || userInnerChoice > myArray.size())
{
System.out.println("Sorry, invalid input. Please choose a number between 1 and "+ myArray.size()+".");
}
else
{
break;
}
}
sql = "SELECT * FROM book WHERE groupName=? and bookTitle=?";
preStmt = conn.prepareStatement(sql);
preStmt.setString(1, myArray1.get(userInnerChoice-1));
preStmt.setString(2, myArray.get(userInnerChoice-1));
rs = preStmt.executeQuery();
System.out.println("Retriving the book \""+myArray.get(userInnerChoice-1)+"\", by "+myArray1.get(userInnerChoice-1)+", Data.\n");
displayFormat = "%-25s%-25s%-25s%-25s%-25s\n";
System.out.printf(displayFormat, "GroupName:", "BookTitle:", "PublisherName:", "YearPublished:", "NumberPages:");
while (rs.next())
{
String bookGroupName = rs.getString("GroupName");
String bookTitle = rs.getString("BookTitle");
String bookPublisherName = rs.getString("PublisherName");
String bookYearPublished = rs.getString("YearPublished");
String bookNumberPages = rs.getString("NumberPages");
System.out.printf(displayFormat, dispNull(bookGroupName), dispNull(bookTitle), dispNull(bookPublisherName), dispNull(bookYearPublished), dispNull(bookNumberPages));
}
}
System.out.println("");
}
//If user picked to Insert a new Book
if (userMenuChoice.equals("7"))
{
boolean foreignKeyConstraint = true;
while (foreignKeyConstraint)
{
System.out.print("Please input the new Book Group Name: ");
String bookName = in.nextLine();
System.out.print("Please input the new Book Title: ");
String bookTitle = in.nextLine();
System.out.print("Please input the new Book Publisher Name: ");
String bookPubName = in.nextLine();
System.out.print("Please input the new Book Year Published: ");
String bookYearPub = in.nextLine();
System.out.print("Please input the new Book Number of Pages: ");
String bookNumPage = in.nextLine();
System.out.println("Inputting the new Book, \""+bookTitle+"\" into the Database.");
sql = "INSERT INTO book VALUES (?,?,?,?,?)";
preStmt = conn.prepareStatement(sql);
preStmt.setString(1, bookName);
preStmt.setString(2, bookTitle);
preStmt.setString(3, bookPubName);
preStmt.setString(4, bookYearPub);
preStmt.setString(5, bookNumPage);
//Try-Catch in the case of foreign keys violation
try{
preStmt.executeUpdate();
break;
}catch (org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException e){
System.out.println("Violation of Foreign Key, Please input the Book again.");
}
}
}
//If user picked to Insert a new Publisher
if (userMenuChoice.equals("8"))
{
System.out.print("Please input the Publisher's Name: ");
String pubName = in.nextLine();
System.out.print("Please input the Publisher's Address: ");
String pubAddress = in.nextLine();
System.out.print("Please input the Publisher's Phone: ");
String pubPhone = in.nextLine();
System.out.print("Please input the Publisher's Email: ");
String pubEmail = in.nextLine();
System.out.println("Inputting the new Publisher, \""+pubName+"\" into the Database.");
sql = "INSERT INTO publishers VALUES (?,?,?,?)";
preStmt = conn.prepareStatement(sql);
preStmt.setString(1, pubName);
preStmt.setString(2, pubAddress);
preStmt.setString(3, pubPhone);
preStmt.setString(4, pubEmail);
preStmt.executeUpdate();
System.out.println("Please choose an old publisher to replace the new one: ");
stmt = conn.createStatement();
sql = "SELECT distinct * FROM publishers ORDER BY publisherName ASC";
ResultSet rs = stmt.executeQuery(sql);
System.out.print(" "); //Print this 3 spaces for indenting when numbering
displayFormat = "%-25s%-25s%-25s%-25s\n";
System.out.printf(displayFormat, "PublisherName:", "PublisherAddress:","PublisherPhone:","PublisherEmail:");
// Instantiating Array and User's internal option
myArray = new ArrayList<String>();
userInnerChoice = 0;
int i = 1; //Used for numbering
while (rs.next())
{
String publisherName = rs.getString("PublisherName");
String publisherAddress = rs.getString("publisheraddress");
String publisherPhone = rs.getString("PublisherPhone");
String publisherEmail = rs.getString("PublisherEmail");
myArray.add(publisherName);
System.out.print(i+". ");
System.out.printf(displayFormat, dispNull(publisherName), dispNull(publisherAddress), dispNull(publisherPhone), dispNull(publisherEmail));
i++;
}
System.out.println("");
if (myArray.size() == 0)
{
System.out.println("There are no Publisher in the Database. Back to Main Menu.");
}
else
{
//Validating User's input for the book to be removed
while (userInnerChoice <= 0 || userInnerChoice > myArray.size())
{
System.out.println("Please Choose the Publisher to be replaced: (1-"+myArray.size()+")");
try{
userInnerChoice = in1.nextInt();
}catch (InputMismatchException e){
System.out.println("Sorry, invalid input. Please choose a number between 1 and "+ myArray.size()+".");
in1.next();
continue;
}
if (userInnerChoice <= 0 || userInnerChoice > myArray.size())
{
System.out.println("Sorry, invalid input. Please choose a number between 1 and "+ myArray.size()+".");
}
else
{
break;
}
}
}
//Updating books to the new Publisher
System.out.println("Updating books from an old Publisher to the new Publisher");
sql = "UPDATE book SET publishername=? WHERE publishername=?";
preStmt = conn.prepareStatement(sql);
preStmt.setString(1, pubName);
preStmt.setString(2, myArray.get(userInnerChoice-1));
preStmt.executeUpdate();
}
//If user picked to remove a book
if (userMenuChoice.equals("9"))
{
stmt = conn.createStatement();
sql = "SELECT * FROM book ORDER BY bookTitle ASC";
ResultSet rs = stmt.executeQuery(sql);
System.out.print(" "); //Print this 3 spaces for indenting when numbering
displayFormat = "%-25s%-25s%-25s%-25s%-25s\n";
System.out.printf(displayFormat, "GroupName:", "BookTitle:", "PublisherName:", "YearPublished:", "NumberPages:");
// Instantiating Array and User's internal option
myArray = new ArrayList<String>();
userInnerChoice = 0;
int i = 1; //Used for numbering
while (rs.next())
{
String bookGroupName = rs.getString("GroupName");
String bookTitle = rs.getString("BookTitle");
String bookPublisherName = rs.getString("PublisherName");
String bookYearPublished = rs.getString("YearPublished");
String bookNumberPages = rs.getString("NumberPages");
myArray.add(bookTitle);
System.out.print(i+". ");
System.out.printf(displayFormat, dispNull(bookGroupName), dispNull(bookTitle), dispNull(bookPublisherName), dispNull(bookYearPublished), dispNull(bookNumberPages));
i++;
}
System.out.println("");
if (myArray.size() == 0)
{
System.out.println("There are no books in the Database. Back to Main Menu.");
}
else
{
//Validating User's input for the book to be removed
while (userInnerChoice <= 0 || userInnerChoice > myArray.size())
{
System.out.println("Please Choose the book to be removed: (1-"+myArray.size()+")");
try{
userInnerChoice = in1.nextInt();
}catch (InputMismatchException e){
System.out.println("Sorry, invalid input. Please choose a number between 1 and "+ myArray.size()+".");
in1.next();
continue;
}
if (userInnerChoice <= 0 || userInnerChoice > myArray.size())
{
System.out.println("Sorry, invalid input. Please choose a number between 1 and "+ myArray.size()+".");
}
else
{
break;
}
}
System.out.println("Deleting \""+myArray.get(userInnerChoice-1)+"\" from the Database.");
sql = "DELETE FROM book WHERE bookTitle=?";
preStmt = conn.prepareStatement(sql);
preStmt.setString(1, myArray.get(userInnerChoice-1));
preStmt.executeUpdate();
System.out.println("Successfully Deleted \""+myArray.get(userInnerChoice-1)+"\" from the Database.");
}
}
//If user's input is to Exit
if (userMenuChoice.equals("10"))
{
keepGoing = false;
}
}
} catch (SQLException se) {
//Handle errors for JDBC
System.out.println("Connection Refused. Please make sure Database is connected.");
se.printStackTrace();
} catch (Exception e) {
//Handle errors for Class.forName
System.out.println("No class for SQL, Returning to Menu.");
} finally {
//finally block used to close resources
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se2) {
}// nothing we can do
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end FirstExample}