-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_queries.java
1663 lines (1661 loc) · 50.1 KB
/
database_queries.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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Scanner;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class sample {
// Database credentials
final static String HOSTNAME = "<username>-sql-server.database.windows.net";
final static String DBNAME = "cs-dsa-4513-sql-db";
final static String USERNAME = "<username>";
final static String PASSWORD = "<password>";
static Connection conn;
// Database connection string
final static String URL =
String.format("jdbc:sqlserver://%s:1433;database=%s;user=%s;password=%s;encrypt=true;trust
ServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;",
HOSTNAME, DBNAME, USERNAME, PASSWORD);
public static Scanner sc = new Scanner(System.in); // Scanner is used to collect the user input
final static String Query1 = "INSERT INTO Team(t_name, t_type, date_formed) VALUES
(?,?,?)";
final static String Query2 = "INSERT INTO CLIENT (SSN, d_name, d_phone, a_name,
a_phone, date_first_assigned) VALUES (?,?,?,?,?,?)";
final static String Query2_care = "INSERT INTO Cares (SSN, t_name, active) VALUES
(?,?,?)";
final static String Query2_insurance = "INSERT INTO Insurance_Policy (SSN, pol_id,
pro_id, pro_address, i_type) VALUES (?,?,?,?,?)";
final static String Query2_need = "INSERT INTO Needs (SSN, needs, importance_value)
VALUES (?,?,?)";
final static String Query3 = "INSERT INTO Volunteers (SSN, date_first_join,
date_recent_train, location_recent_train) VALUES (?,?,?,?)";
final static String Query34_serve = "INSERT INTO Serves (SSN, t_name, months, hour,
active) VALUES (?,?,?,?,?)";
final static String Query34_active = "INSERT INTO Active (SSN, t_name, active) VALUES
(?,?,?)"; /* might not need */
final static String Query5 = "INSERT INTO Employee (SSN,salary,marital_Status,hired_date)
VALUES (?,?,?,?)";
final static String Query5_report = "INSERT INTO Reporting
(SSN,t_name,r_date,r_description) VALUES (?,?,?,?)";
final static String Query6 = "INSERT INTO Expenses (SSN,e_date,amount,e_description)
VALUES (?,?,?,?)";
final static String Query7 = "INSERT INTO External_Organization
(org_name,org_mailing,org_phone,contact_People) VALUES (?,?,?,?)";
final static String Query7_sponsor = "INSERT INTO Sponsor (org_name, t_name) VALUES
(?,?)";
final static String Query8 = "INSERT INTO Donor (SSN, anonymous) VALUES (?,?)";
final static String Query8_Donate = "INSERT INTO Donor_Donate (SSN, d_date,
d_amount,d_type,d_campaign) VALUES (?,?,?,?,?)";
final static String Query8_Card = "INSERT INTO Donor_Donate_Card (SSN, d_date,
d_amount, d_card_num, d_card_type, d_card_exp) VALUES (?,?,?,?,?,?)";
final static String Query8_Check = "INSERT INTO Donor_Donate_Check (SSN, d_date,
d_amount, d_check_num) VALUES (?,?,?,?)";
final static String Query9 = "INSERT INTO Organization_Donor VALUES (?,?)";
final static String Query9_donate = "INSERT INTO Organization_Donate VALUES
(?,?,?,?,?)";
final static String Query9_Card = "INSERT INTO Organization_Donate_Card VALUES
(?,?,?,?,?,?)";
final static String Query9_Check = "INSERT INTO Organization_Donate_Check VALUES
(?,?,?,?)";
// User input prompt//
final static String PROMPT =
"Enter you option(1-17):\n" +
"1. Enter a new team into the database\n" +
"2. Enter a new client into the database and associate him or her
with one or more teams\n" +
"3. Enter a new volunteer into the database and associate him or
her with one or more teams\n" +
"4. Enter the number of hours a volunteer worked this month for a
particular team\n" +
"5. Enter a new employee into the database and associate him or
her with one or more teams\n" +
"6. Enter an expense charged by an employee\n" +
"7. Enter a new organization and associate it to one or more PAN
teams\n" +
"8. Enter a new donor and associate him or her with several
donations.\n" +
"9. Enter a new organization and associate it with several
donations\n" +
"10. Retrieve the name and phone number of the doctor of a
particular client\n" +
"11. Retrieve the total amount of expenses charged by each
employee for a particular period of time.\r\n" +
"The list should be sorted by the total amount of expenses\n" +
"12. Retrieve the list of volunteers that are members of teams that
support a particular client\n" +
"13. Retrieve the names and contact information of the clients that
are supported by teams sponsored by\r\n" +
"an organization whose name starts with a letter between B and K.
The client list should be sorted by name\n" +
"14. Retrieve the name and total amount donated by Donor that are
also employees. The list should be\r\n" +
"sorted by the total amount of the donations, and indicate if each
donor wishes to remain anonymous\n" +
"15. Retrieve the names of all teams that were founded after a
particular date(1/month)\n" +
"16. Increase the salary by 10% of all employees to whom more
than one team must report\n"+
"17. Delete all clients who do not have health insurance and whose
value of importance for transportation\r\n" +
"is less than 5\n"+
"18. Import\n"+
"19. Export\n" +
"20. Add a Person\n" +
"21. Close";
public static void main(String[] args) throws SQLException {
conn = DriverManager.getConnection(URL);
String option = ""; // Initialize user option selection as nothing
while (!option.equals("21")) { // As user for options until option 4 is selected
System.out.println(PROMPT); // Print the available options
option = sc.nextLine(); // Read in the user option selection
switch (option) { // Switch between different options
case "1": // Insert a new faculty with first salary calculation method option
Q1();
break;
case "2":
Q2();
break;
case "3":
Q3();
break;
case "4":
Q4();
break;
case "5":
Q5();
break;
case "6":
Q6();
break;
case "7":
Q7();
break;
case "8":
Q8();
break;
case "9":
Q9();
break;
case "10":
Q10();
break;
case "11":
Q11();
break;
case "12":
Q12();
break;
case "13":
Q13();
break;
case "14":
Q14();
break;
case "15":
Q15();
break;
case "16":
Q16();
break;
case "17":
Q17();
break;
case "18":
Q18();
break;
case "19":
Q19();
break;
case "20":
Q20();
break;
case "21":
sc.close();
System.out.println("Later!");
break;
default:
System.out.println("Sorry, Unrecognized input");
break;
}
}
sc.close(); // Close the scanner before exiting the application
}
public static void Q1() throws SQLException
{
System.out.println("Please enter team name:");
String name = sc.nextLine();
//sc.nextLine();
System.out.println("Please enter team type:");
String type = sc.nextLine();
System.out.println("Please enter date formed: 010112 = Jan 1, 2012");
String date = sc.nextLine();
try (final PreparedStatement statement = conn.prepareStatement(Query1))
{
// Populate the query template with the data collected from the user
statement.setString(1, name);
statement.setString(2, type);
statement.setString(3, date);
final int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
displayTeam();
}
/* TO TEST YOU NEED to insert FOREIGN KEY before running*/
public static void Q2() throws SQLException
{
/* Client */
System.out.println("Enter client SSN from People table:");
int SSN = Integer.parseInt(sc.nextLine());
System.out.println("Enter Doctor's name");
String d_name = sc.nextLine();
System.out.println("Enter Doctor's phone number: ex - 4051234567");
int d_phone = Integer.parseInt(sc.nextLine());
System.out.println("Enter Attorney's name");
String a_name = sc.nextLine();
System.out.println("Enter Attorney's phone number: ex - 4051234567");
int a_phone = Integer.parseInt(sc.nextLine());
System.out.println("Enter date client was first assigned: 010112 = Jan 1, 2012");
int date = Integer.parseInt(sc.nextLine());
try (final PreparedStatement statement = conn.prepareStatement(Query2))
{
// Populate the query template with the data collected from the user
statement.setInt(1, SSN);
statement.setString(2, d_name);
statement.setInt(3, d_phone);
statement.setString(4, a_name);
statement.setInt(5, a_phone);
statement.setInt(6, date);
final int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
catch(SQLException e)
{
e.printStackTrace();
}
displayClient();
/* Team Association & Active */
System.out.println("Enter Team Name Associated With:");
String team = sc.nextLine();
System.out.println("Client active or inactive?");
String active = sc.nextLine();
try (final PreparedStatement statement = conn.prepareStatement(Query2_care))
{
// Populate the query template with the data collected from the user
statement.setInt(1, SSN);
statement.setString(2, team);
statement.setString(3, active);
final int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
catch(SQLException e)
{
e.printStackTrace();
}
displayCares();
/* Insurance */
System.out.println("Enter Insurance Info");
System.out.println("Enter Policy ID:");
int pol_id = Integer.parseInt(sc.nextLine());
System.out.println("Enter Provider ID:");
int pro_id = Integer.parseInt(sc.nextLine());
System.out.println("Enter Provider Address:");
String address = sc.nextLine();
String type = "";
boolean constraint_met = false;
while(!constraint_met)
{
System.out.println("Type: \n1.Auto\n2.Life\n3.Health\n4.Home");
String typed = sc.nextLine();
switch(typed) {
case "1":
type = "Auto";
constraint_met = true;
break;
case "2":
type = "Life";
constraint_met = true;
break;
case "3":
type = "Health";
constraint_met = true;
break;
case "4":
type = "Home";
constraint_met = true;
break;
default:
System.out.println("Choose 1, 2, 3, or 4 ONLY");
break;
}
}
// Insert into insurance
try (final PreparedStatement statement = conn.prepareStatement(Query2_insurance))
{
// Populate the query template with the data collected from the user
statement.setInt(1, SSN);
statement.setInt(2, pol_id);
statement.setInt(3, pro_id);
statement.setString(4, address);
statement.setString(5, type);
final int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
catch(SQLException e)
{
e.printStackTrace();
}
displayInsurance();
boolean need_stuff = false;
int importance_value = 0;
// Need choices option for multiple needs and a quit option
while (!need_stuff)
{
String need = "";
System.out.println("Select
Need\n1.Visiting\n2.Shopping\n3.HouseKeeping\n4.Transportation\n5.Yard
Work\n6.Food\n7.Enter yourself\n8.No more needs");
int select =Integer.parseInt(sc.nextLine());
switch (select)
{
case 1:
need = "Visiting";
System.out.println("Enter importance value");
importance_value = Integer.parseInt(sc.nextLine());
try (final PreparedStatement statement = conn.prepareStatement(Query2_need))
{
// Populate the query template with the data collected from the user
statement.setInt(1, SSN);
statement.setString(2, need);
statement.setInt(3, importance_value);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
break;
case 2:
need = "Shopping";
System.out.println("Enter importance value");
importance_value = Integer.parseInt(sc.nextLine());
try (final PreparedStatement statement = conn.prepareStatement(Query2_need))
{
// Populate the query template with the data collected from the user
statement.setInt(1, SSN);
statement.setString(2, need);
statement.setInt(3, importance_value);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
break;
case 3:
need = "HouseKeeping";
System.out.println("Enter importance value");
importance_value = Integer.parseInt(sc.nextLine());
try (final PreparedStatement statement = conn.prepareStatement(Query2_need))
{
// Populate the query template with the data collected from the user
statement.setInt(1, SSN);
statement.setString(2, need);
statement.setInt(3, importance_value);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
break;
case 4:
need = "Transportation";
System.out.println("Enter importance value");
importance_value =Integer.parseInt(sc.nextLine());
try (final PreparedStatement statement = conn.prepareStatement(Query2_need))
{
// Populate the query template with the data collected from the user
statement.setInt(1, SSN);
statement.setString(2, need);
statement.setInt(3, importance_value);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
break;
case 5:
need = "Yard Work";
System.out.println("Enter importance value");
importance_value =Integer.parseInt(sc.nextLine());
try (final PreparedStatement statement = conn.prepareStatement(Query2_need))
{
// Populate the query template with the data collected from the user
statement.setInt(1, SSN);
statement.setString(2, need);
statement.setInt(3, importance_value);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
break;
case 6:
need = "Food";
System.out.println("Enter importance value");
importance_value = Integer.parseInt(sc.nextLine());
try (final PreparedStatement statement = conn.prepareStatement(Query2_need))
{
// Populate the query template with the data collected from the user
statement.setInt(1, SSN);
statement.setString(2, need);
statement.setInt(3, importance_value);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
break;
case 7:
System.out.println("What do you need?");
need = sc.nextLine();
System.out.println("Enter importance value");
importance_value = Integer.parseInt(sc.nextLine());
try (final PreparedStatement statement = conn.prepareStatement(Query2_need))
{
// Populate the query template with the data collected from the user
statement.setInt(1, SSN);
statement.setString(2, need);
statement.setInt(3, importance_value);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
break;
case 8:
need_stuff = true;
break;
}
}
// display Need table for project report purpose
displayNeeds();
}
public static void Q3() throws SQLException
{
// Volunteer
System.out.println("1. Enter Volunteer\n2. Add Leader");
int ad = Integer.parseInt(sc.nextLine());
switch (ad)
{
case 1:
System.out.println("Enter Volunteer SSN:");
int ssn = Integer.parseInt(sc.nextLine());
System.out.println("Enter date first join: 010112 - Jan 1, 2012");
int date_join = Integer.parseInt(sc.nextLine());
System.out.println("Enter Recent Training Date");
int date_train = Integer.parseInt(sc.nextLine());
System.out.println("Enter Recent Training Location");
String location = sc.nextLine();
try (final PreparedStatement statement = conn.prepareStatement(Query3))
{
// Populate the query template with the data collected from the user
statement.setInt(1, ssn);
statement.setInt(2, date_join);
statement.setInt(3, date_train);
statement.setString(4, location);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
// display for pdf purpose
displayVolunteer();
// Serve table (relation between team and volunteer)
System.out.println("Enter Team Associated With:");
String t_name = sc.nextLine();
boolean correct = false;
int month = 0;
while (!correct)
{
System.out.println("Enter month worked");
month = sc.nextInt();
if (month > 0 && month < 13)
{
correct = true;
}
else
{
System.out.println("Enter a correct month: 4 - April");
}
}
System.out.println("Enter hours worked ");
int hour = sc.nextInt();
sc.nextLine();
System.out.println("Active or inactive?");
String active = sc.nextLine();
try (final PreparedStatement statement = conn.prepareStatement(Query34_serve))
{
// Populate the query template with the data collected from the user
statement.setInt(1, ssn);
statement.setString(2, t_name);
statement.setInt(3, month);
statement.setInt(4, hour);
statement.setString(5, active);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
// display volunteer/team relation table
displayServe();
case 2:
// add a leader to a team (add to table Leads)
System.out.println("Enter SSN:");
int ssn2 = Integer.parseInt(sc.nextLine());
System.out.println("Team name:");
String name2 = sc.nextLine();
try (final PreparedStatement statement = conn.prepareStatement("INSERT INTO
Leads VALUES (" + ssn2 + ",'" + name2 + "')"))
{
// Populate the query template with the data collected from the user
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
Statement statement2 = conn.createStatement();
ResultSet r2 = statement2.executeQuery("SELECT * FROM Leads");
System.out.println("Leads Table");
while(r2.next()){
System.out.println(String.format("SSN: %s| d_name: %s| ",
r2.getString(1),
r2.getString(2)));
}
}
}
public static void Q4() throws SQLException
{
System.out.println("Enter Volunteer SSN:");
int ssn2 = sc.nextInt();
sc.nextLine();
System.out.println("Enter Team Associated With:");
String t_name2 = sc.nextLine();
boolean correct2 = false;
int month2 = 0;
while (!correct2)
{
System.out.println("Enter month worked");
month2 = sc.nextInt();
if (month2 > 0 && month2 < 13)
{
correct2 = true;
}
else
{
System.out.println("Enter a correct month: 4 - April");
}
}
System.out.println("Enter hours worked ");
int hour2 = sc.nextInt();
sc.nextLine();
System.out.println("Active or inactive?");
String active2 = sc.nextLine();
try (final PreparedStatement statement = conn.prepareStatement(Query34_serve))
{
// Populate the query template with the data collected from the user
statement.setInt(1, ssn2);
statement.setString(2, t_name2);
statement.setInt(3, month2);
statement.setInt(4, hour2);
statement.setString(5, active2);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
displayServe();
}
public static void Q5() throws SQLException
{
System.out.println("1. Enter Employee\n2. Associated with more Team");
int chose = sc.nextInt();
sc.nextLine();
switch(chose)
{
// Employee table
case 1:
System.out.println("Enter Employee SSN:");
int ssn = Integer.parseInt(sc.nextLine());
System.out.println("Enter salary:");
int salary = Integer.parseInt(sc.nextLine());
System.out.println("Enter martial status");
String status = sc.nextLine();
System.out.println("Enter hired date: 010112 - Jan 1, 2012");
int date = Integer.parseInt(sc.nextLine());
try (final PreparedStatement statement = conn.prepareStatement(Query5))
{
// Populate the query template with the data collected from the user
statement.setInt(1, ssn);
statement.setInt(2, salary);
statement.setString(3, status);
statement.setInt(4, date);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
displayEmployee();
/* // Team associate
System.out.println("Enter team associated with:");
String team = sc.nextLine();
System.out.println("Enter report date: 010112 - Jan 1,2012");
int report_date = Integer.parseInt(sc.nextLine());
System.out.println("Enter report description:");
String des = sc.nextLine();
try (final PreparedStatement statement = conn.prepareStatement(Query5_report))
{
// Populate the query template with the data collected from the user
statement.setInt(1, ssn);
statement.setString(2, team);
statement.setInt(3, report_date);
statement.setString(4, des);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
displayReport();*/
break;
case 2:
// reporting table for employee and team
System.out.println("Enter Employee SSN:");
int ssn2 = Integer.parseInt(sc.nextLine());
System.out.println("Enter team associated with");
String team2 = sc.nextLine();
System.out.println("Enter report description");
String des2 = sc.nextLine();
System.out.println("Enter report date ");
int report_date2 = Integer.parseInt(sc.nextLine());
//execute query
try (final PreparedStatement statement = conn.prepareStatement(Query5_report))
{
// Populate the query template with the data collected from the user
statement.setInt(1, ssn2);
statement.setString(2, team2);
statement.setInt(3, report_date2);
statement.setString(4, des2);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
displayReport();
break;
}
}
public static void Q6() throws SQLException
{
// Expenses table
System.out.println("Enter Employee SSN:");
int ssn = sc.nextInt();
sc.nextLine();
System.out.println("Enter date:");
int date = sc.nextInt();
sc.nextLine();
System.out.println("Enter amount:");
int amount = sc.nextInt();
sc.nextLine();
System.out.println("Enter description:");
String des = sc.nextLine();
try (final PreparedStatement statement = conn.prepareStatement(Query6))
{
// Populate the query template with the data collected from the user
statement.setInt(1, ssn);
statement.setInt(2, date);
statement.setInt(3, amount);
statement.setString(4, des);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
displayExpense();
}
public static void Q7() throws SQLException
{
System.out.println("1. Enter a new organization\n2. Associate with team\n3. Affiliate
with a person");
int choice = Integer.parseInt(sc.nextLine());
switch(choice)
{
case 1:
// External Org table
System.out.println("Enter Orgazniation's Name:");
String name = sc.nextLine();
System.out.println("Enter Address:");
String address = sc.nextLine();
System.out.println("Enter phone number:");
int phone = Integer.parseInt(sc.nextLine());
System.out.println("Enter contact People");
String contact = sc.nextLine();
try (final PreparedStatement statement = conn.prepareStatement(Query7))
{
// Populate the query template with the data collected from the user
statement.setString(1, name);
statement.setString(2, address);
statement.setInt(3, phone);
statement.setString(4, contact);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
// display External Org table after insert
displayOrg();
break;
case 2:
// Sponsor table
System.out.println("Enter Orgazniation Name:");
String name2 = sc.nextLine();
System.out.println("Enter Team Name:");
String t_name = sc.nextLine();
try (final PreparedStatement statement =
conn.prepareStatement(Query7_sponsor))
{
// Populate the query template with the data collected from the user
statement.setString(1, name2);
statement.setString(2, t_name);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
// display sponsor table after insert
displaySponsor();
break;
case 3:
break;
}
}
public static void Q8() throws SQLException
{
//displayDCard();
System.out.println("1. Enter a new donor\n2. Associate donor with 1 or more
Donation(s)");
int choice = Integer.parseInt(sc.nextLine());
switch (choice)
{
case 1:
// Donor table
System.out.println("Enter SSN:");
int ssn = Integer.parseInt(sc.nextLine());
System.out.println("Is it anonymous");
String ano = sc.nextLine();
try (final PreparedStatement statement = conn.prepareStatement(Query8))
{
// Populate the query template with the data collected from the user
statement.setInt(1, ssn);
statement.setString(2, ano);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
displayDonor();
break;
case 2:
// info for donor card or check
System.out.println("Donor SSN:");
int ssn2 = Integer.parseInt(sc.nextLine());
System.out.println("Date:");
int date = Integer.parseInt(sc.nextLine());
System.out.println("Amount");
int amount = Integer.parseInt(sc.nextLine());
System.out.println("Campaign:");
String camp = sc.nextLine();
System.out.println("Type:\n1. Card\n2. Check");
int type = Integer.parseInt(sc.nextLine());
boolean chose = false;
while (!chose)
{
switch (type)
{
case 1:
// donor card table
String c = "card";
System.out.println("Card number:");
int card = Integer.parseInt(sc.nextLine());
System.out.println("Card type:");
String c_type = sc.nextLine();
System.out.println("Exp date: 010112 - Jan 1, 2012");
int exp = Integer.parseInt(sc.nextLine());
// add to the donor donate table
try (final PreparedStatement statement =
conn.prepareStatement(Query8_Donate))
{
// Populate the query template with the data collected from the user
statement.setInt(1, ssn2);
statement.setInt(2, date);
statement.setInt(3, amount);
statement.setString(4, c);
statement.setString(5, camp);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
// add to donor donate card table
try (final PreparedStatement statement =
conn.prepareStatement(Query8_Card))
{
// Populate the query template with the data collected from the user
statement.setInt(1, ssn2);
statement.setInt(2, date);
statement.setInt(3, amount);
statement.setInt(4, card);
statement.setString(5, c_type);
statement.setInt(6, exp);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
chose = true;
displayDCard();
break;
case 2:
// add to donor check info
String ch = "check";
System.out.println("Check number:");
int check = Integer.parseInt(sc.nextLine());
// add to table donor donate
try (final PreparedStatement statement =
conn.prepareStatement(Query8_Donate))
{
// Populate the query template with the data collected from the user
statement.setInt(1, ssn2);
statement.setInt(2, date);
statement.setInt(3, amount);
statement.setString(4, ch);
statement.setString(5, camp);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
displayDD();
// add to table donor donate check
try (final PreparedStatement statement =
conn.prepareStatement(Query8_Check))
{
// Populate the query template with the data collected from the user
statement.setInt(1, ssn2);
statement.setInt(2, date);
statement.setInt(3, amount);
statement.setInt(4, check);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
chose = true;
displayDCH();
break;
default:
System.out.println("Chose 1 or 2");
break;
}
}
break;
}
}
// basically mirroring Q8 except adding into different tables
public static void Q9() throws SQLException
{
System.out.println("1. Enter new org that donates \n2. Associated with 1 or more
Donation(s)");
int choice = Integer.parseInt(sc.nextLine());
switch (choice)
{
case 1:
// Org Donate table
System.out.println("Enter Org name:");
String name =sc.nextLine();
System.out.println("Is it anonymous");
String ano = sc.nextLine();
try (final PreparedStatement statement = conn.prepareStatement(Query9))
{
// Populate the query template with the data collected from the user
statement.setString(1, name);
statement.setString(2, ano);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
displayOD();
break;
case 2:
System.out.println("Org Name:");
String o_name = sc.nextLine();
System.out.println("Date:");
int d = Integer.parseInt(sc.nextLine());
System.out.println("Amount");
int amount = Integer.parseInt(sc.nextLine());
System.out.println("Campaign:");
String camp = sc.nextLine();
System.out.println("Type:\n1. Card\n2. Check");
int type = Integer.parseInt(sc.nextLine());
boolean chose = false;
while (!chose)
{
switch (type)
{
case 1:
String c = "card";
System.out.println("Card number:");
int card = Integer.parseInt(sc.nextLine());
System.out.println("Card type:");
String c_type = sc.nextLine();
System.out.println("Exp date: 010112 - Jan 1, 2012");
int exp = Integer.parseInt(sc.nextLine());
try (final PreparedStatement statement =
conn.prepareStatement(Query9_donate))
{
// Populate the query template with the data collected from the user
statement.setString(1, o_name);
statement.setInt(2, d);
statement.setInt(3, amount);
statement.setString(4, c);
statement.setString(5, camp);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
displayOD();
try (final PreparedStatement statement =
conn.prepareStatement(Query9_Card))
{
// Populate the query template with the data collected from the user
statement.setString(1, o_name);
statement.setInt(2, d);
statement.setInt(3, amount);
statement.setInt(4, card);
statement.setString(5, c_type);
statement.setInt(6, exp);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
chose = true;
displayODCard();
break;
case 2:
String ch = "check";
System.out.println("Check number:");
int check = Integer.parseInt(sc.nextLine());
try (final PreparedStatement statement =
conn.prepareStatement(Query9_donate))
{
// Populate the query template with the data collected from the user
statement.setString(1, o_name);
statement.setInt(2, d);
statement.setInt(3, amount);
statement.setString(4, ch);
statement.setString(5, camp);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
displayOD();
try (final PreparedStatement statement =
conn.prepareStatement(Query9_Check))
{
// Populate the query template with the data collected from the user
statement.setString(1, o_name);
statement.setInt(2, d);
statement.setInt(3, amount);
statement.setInt(4, check);
int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
chose = true;
displayODCheck();
break;
default:
System.out.println("Chose 1 or 2");
break;
}
}
break;
}
}
public static void Q10() throws SQLException
{
System.out.println("Enter SSN of Client:");
int SSN = sc.nextInt();
String q10 = "SELECT SSN,d_name,d_phone FROM Client WHERE SSN ="+SSN;
Statement statement = conn.createStatement();
ResultSet r = statement.executeQuery(q10);
//display
while(r.next()){
System.out.println(String.format("SSN: %s|Doctor name: %s | Phone %s
|",
r.getString("SSN"),
r.getString("d_name"),
r.getString("d_phone")));
}
}
public static void Q11() throws SQLException
{
System.out.println("Expense date:");
int date = Integer.parseInt(sc.nextLine());
String q10 = "SELECT SSN, amount FROM Expenses WHERE e_date ="+date;
Statement statement = conn.createStatement();
ResultSet r = statement.executeQuery(q10);
//display
while(r.next()){
System.out.println(String.format("SSN: %s|Amount: %s |",
r.getString("SSN"),
r.getString("amount")));
}
}
public static void Q12() throws SQLException
{
System.out.println("Enter client's SSN");
int ssn = Integer.parseInt(sc.nextLine());
String q12 = "SELECT DISTINCT (SSN) FROM Serves WHERE t_name in(SELECT
t_name from Cares WHERE SSN in(SELECT SSN FROM Client where SSN="+ ssn +"))";
Statement statement = conn.createStatement();
ResultSet r = statement.executeQuery(q12);
// display
while(r.next()){
System.out.println(String.format("SSN: %s|",
r.getString("SSN")));
}
}
public static void Q13() throws SQLException
{
String q13 = "Select P_name, M_address, E_address, H_number, W_number, C_number
FROM People where SSN IN(SELECT SSN from Cares where t_name " +
"IN(SELECT t_name FROM Sponsor WHERE org_name IN(SELECT
org_name FROM External_Organization WHERE SUBSTRING(org_name,1,1) between 'B' and