-
Notifications
You must be signed in to change notification settings - Fork 0
/
Challa_Meghnath_IP_Task5b.java
1166 lines (1092 loc) · 46.1 KB
/
Challa_Meghnath_IP_Task5b.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 statements
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.Statement;
import java.sql.Time;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
//Class
public class finalproject {
// Database credentials
final static String HOSTNAME = "chal0018-sql-server.database.windows.net";
final static String DBNAME = "HW-2";
final static String USERNAME = "chal0018";
final static String PASSWORD = "Sridevi$123";
// Database connection string
final static String URL = String.format(
"jdbc:sqlserver://%s:1433;database=%s;user=%s;password=%s;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;",
HOSTNAME, DBNAME, USERNAME, PASSWORD);
// Query templates
final static String QUERY_TEMPLATE_1 = "{call insertintoTeam(?,?,?)}";
final static String QUERY_TEMPLATE_2 = "{call insertintoClient(?,?,?,?,?,?)}";
final static String QUERY_TEMPLATE_3 = "{call insertintoCare(?,?,?)}";
final static String QUERY_TEMPLATE_4 = "{call insertintoPerson(?,?,?,?,?,?,?,?,?,?,?,?)}";
final static String QUERY_TEMPLATE_5 = "{call insertintoVolunteer(?,?,?,?)}";
final static String QUERY_TEMPLATE_6 = "{call insertintoServes(?,?,?,?,?)}";
final static String QUERY_TEMPLATE_7 = "{call insertintoEmployee(?,?,?,?)}";
final static String QUERY_TEMPLATE_8 = "{call insertintoReports(?,?,?,?)}";
final static String QUERY_TEMPLATE_9 = "{call insertintoExpense(?,?,?,?)}";
final static String QUERY_TEMPLATE_10 = "{call insertintoExternal_Org(?,?,?,?)}";
final static String QUERY_TEMPLATE_11 = "{call insertintoSponsors(?,?)}";
final static String QUERY_TEMPLATE_12 = "{call insertintoDonor(?,?)}";
final static String QUERY_TEMPLATE_13 = "{call insertintoCheck(?,?,?,?,?,?)}";
final static String QUERY_TEMPLATE_14 = "{call insertintoCredit(?,?,?,?,?,?,?,?)}";
final static String QUERY_TEMPLATE_15 = "{call insertintoExternalDonor(?,?)}";
final static String QUERY_TEMPLATE_16 = "{call insertintoExCheck(?,?,?,?,?,?)}";
final static String QUERY_TEMPLATE_17 = "{call insertintoExCredit(?,?,?,?,?,?,?,?)}";
final static String QUERY_TEMPLATE_18 = "select * from Employees";
final static String QUERY_TEMPLATE_19 = "select * from Client";
// User input prompt//
final static String PROMPT = "\nPlease select one of the options below: \n" + "1) Insert new Team: \n"
+"2) Insert new Client and associate him/her to Team \n" + "3) Insert new Volunteer and associate him/her to Team \n" +
"4) Enter number of hrs volunteer worked for a Team \n"+"5) Insert new Employee and associate him/her to Team \n" +
"6) Enter a new Expense charged by Employee \n"+
"7) Insert new Organization and associate it to one/more Teams \n"+
"8) Insert new Donors and associate it to one/more donations \n"+
"9) Insert new Organization and associate it to one/more donations \n"+
"10) Retrieve Doctor Name and Phone for particular client \n"+
"11) Retrieve total amount of expenses for particular period of an employee date format-YYYYMMDD \n"+
"12) Retrieve list of Volunteers that are members of team that support particular client \n"+
"13) Retrieve Name,Contact info of Client supported by an Org with name starts between B & K \n"+
"14) yet to implement \n"+
"15) Retrieve Team Name that are found after a particular date(yyyymmdd) \n"+
"16) Update Salary of Employee who has more than one team reporting \n"+
"17) Delete Employee who does not have Insurance and type is transportation \n"+
"18) Import file \n"+"19) Export File \n"+"20) Exit!";
public static void main(String[] args) throws SQLException{
System.out.println("Welcome to the sample application!");
final Scanner sc = new Scanner(System.in); // Scanner is used to collect the user input
String option = ""; // Initialize user option selection as nothing
Connection conn = DriverManager.getConnection(URL); //establish connection
Statement stmt = conn.createStatement(); //to execute sql queries
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //used for export/ import read input from user
//Insert a person at the start
/*
System.out.println("Please enter SSN:");
final int SSN_P= sc.nextInt();
sc.nextLine();
System.out.println("Please enter Person Name:");
final String P_Name = sc.nextLine();
System.out.println("Please enter Birth Date:");
final String Birthdate=sc.nextLine();
Date Bdate=Date.valueOf(Birthdate);
System.out.println("Please enter Race:");
final String Race = sc.nextLine();
System.out.println("Please enter Gender:");
final String Gender = sc.nextLine();
System.out.println("Please enter Profession:");
final String Profession = sc.nextLine();
System.out.println("Please enter Mail_address:");
final String Mail_address = sc.nextLine();
System.out.println("Please enter Email_address:");
final String Email_address = sc.nextLine();
System.out.println("Please enter Home_phone:");
final int Home_phone= sc.nextInt();
System.out.println("Please enter Work_phone:");
final int Work_phone= sc.nextInt();
System.out.println("Please enter Cell_phone:");
final int Cell_phone= sc.nextInt();
System.out.println("Please enter Mailing_list state:");
final int Mailing_list= sc.nextInt();
try (final Connection connection = DriverManager.getConnection(URL)) {
try (final CallableStatement statement = connection.prepareCall(QUERY_TEMPLATE_4)) {
// Populate the query template with the data collected from the user
statement.setInt(1,SSN_P );
statement.setString(2, P_Name);
statement.setDate(3, Bdate);
statement.setString(4, Race);
statement.setString(5, Gender);
statement.setString(6, Profession);
statement.setString(7, Mail_address);
statement.setString(8, Email_address);
statement.setInt(9, Home_phone);
statement.setInt(10, Work_phone);
statement.setInt(11, Cell_phone);
statement.setInt(12, Mailing_list);
System.out.println("Dispatching the query...");
// Actually execute the populated query
final int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
}
*/
while (!option.equals("20")) { // Ask user for options until option 20 is selected
System.out.println(PROMPT); // Print the available options
option = sc.next(); // Read in the user option selection
switch (option) { // Switch between different options
case "1": // Insert a new Team option
System.out.println("Please enter Team Name:");
try {
sc.nextLine();
final String T_Name = sc.nextLine(); // Read in the user input of Team Name
System.out.println("Please enter Team Type:");
// We call nextLine to consume that newline character, so that subsequent
// nextLine doesn't return nothing.
final String Type = sc.nextLine(); // Read in user input of faculty Name (white-spaces allowed).
System.out.println("Please Date assigned in format YYYYMMDD:");
final int date_a=sc.nextInt();
System.out.println("Connecting to the database...");
// Get a database connection and prepare a query statement
try (final Connection connection = DriverManager.getConnection(URL)) {
try (final CallableStatement statement = connection.prepareCall(QUERY_TEMPLATE_1)) {
// Populate the query template with the data collected from the user
statement.setString(1,T_Name );
statement.setString(2, Type);
statement.setInt(3, date_a);
System.out.println("Dispatching the query...");
// Actually execute the populated query
final int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
}
}
//Error handling
catch(InputMismatchException e)
{
System.out.println("Caught Input mismatch -- ");
sc.nextLine();
}
catch (Exception e)
{
System.out.println("Couldn't execute the above query!");
System.out.println("Reason -- ");
System.out.println(e.getMessage());
System.out.println("Try again !!!");
}
break;
case "2": // Insert a new Client
try {
System.out.println("Please enter SSN:");
final int SSN = sc.nextInt();
sc.nextLine();
System.out.println("Please enter Doctor name:");
final String Doctor_Name = sc.nextLine();
System.out.println("Please enter Attor Name:");
final String Attor_Name= sc.nextLine();
System.out.println("Please Doctor Phone:");
final int Doctor_phone = sc.nextInt();
System.out.println("Please enter Attor Phone:");
final int Attor_Phone= sc.nextInt();
sc.nextLine();
System.out.println("Please Date assigned:");
final String date_a=sc.nextLine();
Date date=Date.valueOf(date_a);//converting string into sql date
System.out.println("Connecting to the database...");
// Get a database connection and prepare a query statement
try (final Connection connection = DriverManager.getConnection(URL)) {
try (final CallableStatement statement = connection.prepareCall(QUERY_TEMPLATE_2))
{
// Populate the query template with the data collected from the user
statement.setInt(1,SSN );
statement.setString(2, Doctor_Name);
statement.setString(3, Attor_Name);
statement.setInt(4, Doctor_phone);
statement.setInt(5, Attor_Phone);
statement.setDate(6, date);
System.out.println("Dispatching the query...");
// Actually execute the populated query
final int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
}
}
catch(InputMismatchException e)
{
System.out.println("Caught Input mismatch");
sc.nextLine();
break;
}
catch (Exception e)
{
System.out.println("Couldn't execute the above query!");
System.out.println("Reason -- ");
System.out.println(e.getMessage());
System.out.println("Try again !!!");
}
//for care
System.out.println("Number of teams to assosciate to");
int count=sc.nextInt();
sc.nextLine();
while (count!=0) {
System.out.println("Please enter SSN:");
final int SSN = sc.nextInt();
sc.nextLine();
System.out.println("Please enter Team Name to which you want to assosciate:");
final String Team_Name= sc.nextLine();
int C_ActiveState=0;
try (final Connection connection = DriverManager.getConnection(URL)) {
try (final CallableStatement statement1 = connection.prepareCall(QUERY_TEMPLATE_3))
{
// Populate the query template with the data collected from the user
statement1.setInt(1,SSN );
statement1.setString(2, Team_Name);
statement1.setInt(3, C_ActiveState);
System.out.println("Dispatching the query...");
// Actually execute the populated query
final int rows_inserted = statement1.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
}
count--;}
break;
case "3": // Insert a new Volunteer
try {
System.out.println("Please enter SSN:");
final int SSN = sc.nextInt();
sc.nextLine();
System.out.println("Please enter Joining Date:");
final String date_J=sc.nextLine();
Date date=Date.valueOf(date_J);
System.out.println("Please Training Date:");
final String date_T = sc.nextLine();
Date date1=Date.valueOf(date_T);
System.out.println("Please enter Training Loc :");
final String Training_Loc = sc.nextLine();
//converting string into sql date
System.out.println("Connecting to the database...");
// Get a database connection and prepare a query statement
try (final Connection connection = DriverManager.getConnection(URL)) {
try (final CallableStatement statement = connection.prepareCall(QUERY_TEMPLATE_5))
{
// Populate the query template with the data collected from the user
statement.setInt(1,SSN );
statement.setDate(2, date);
statement.setDate(3, date1);
statement.setString(4, Training_Loc);
System.out.println("Dispatching the query...");
// Actually execute the populated query
final int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
}
}
catch(InputMismatchException e)
{
System.out.println("Caught Input mismatch");
sc.nextLine();
break;
}
catch (Exception e)
{
System.out.println("Couldn't execute the above query!");
System.out.println("Reason -- ");
System.out.println(e.getMessage());
System.out.println("Try again !!!");
}
//for SERVES
System.out.println("Number of teams to assosciate to");
int count1=sc.nextInt();
sc.nextLine();
while (count1!=0) {
System.out.println("Please enter SSN:");
final int SSN = sc.nextInt();
sc.nextLine();
System.out.println("Please enter Team Name to which you want to assosciate:");
final String Team_Name= sc.nextLine();
System.out.println("Please enter HOURS:");
final int hours = sc.nextInt();
sc.nextLine();
int C_ActiveState=0;
System.out.println("Please enter Specific month:");
final String month=sc.nextLine();
try (final Connection connection = DriverManager.getConnection(URL)) {
try (final CallableStatement statement1 = connection.prepareCall(QUERY_TEMPLATE_6))
{
// Populate the query template with the data collected from the user
statement1.setInt(1,SSN );
statement1.setString(2, Team_Name);
statement1.setInt(3, hours);
statement1.setInt(4, C_ActiveState);
statement1.setString(5, month);
System.out.println("Dispatching the query...");
// Actually execute the populated query
final int rows_inserted = statement1.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
}
count1--;}
break;
case "4":
try {
//for SERVES
System.out.println("Please enter SSN:");
final int SSN = sc.nextInt();
sc.nextLine();
System.out.println("Please enter Team Name to which you want to assosciate:");
final String Team_Name= sc.nextLine();
System.out.println("Please enter HOURS:");
final int hours = sc.nextInt();
sc.nextLine();
int C_ActiveState=0;
System.out.println("Please enter Specific month:");
final String month=sc.nextLine();
try (final Connection connection = DriverManager.getConnection(URL)) {
try (final CallableStatement statement1 = connection.prepareCall(QUERY_TEMPLATE_6))
{
// Populate the query template with the data collected from the user
statement1.setInt(1,SSN );
statement1.setString(2, Team_Name);
statement1.setInt(3, hours);
statement1.setInt(4, C_ActiveState);
statement1.setString(5, month);
System.out.println("Dispatching the query...");
// Actually execute the populated query
final int rows_inserted = statement1.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
}
}
catch(InputMismatchException e)
{
System.out.println("Caught Input mismatch");
sc.nextLine();
break;
}
catch (Exception e)
{
System.out.println("Couldn't execute the above query!");
System.out.println("Reason -- ");
System.out.println(e.getMessage());
System.out.println("Try again !!!");
}
break;
case "5": // Insert a new Employee
// Collect the new Employee data from the user
try {
System.out.println("Please enter SSN:");
final int SSN = sc.nextInt();
sc.nextLine();
System.out.println("Please enter Salary of Employee:");
final Float Salary=sc.nextFloat();
sc.nextLine();
System.out.println("Please enter Marital Status:");
final String Status = sc.nextLine();
System.out.println("Please enter Hire Date:");
final String date_J=sc.nextLine();
Date date=Date.valueOf(date_J);
//converting string into sql date
System.out.println("Connecting to the database...");
// Get a database connection and prepare a query statement
try (final Connection connection = DriverManager.getConnection(URL)) {
try (final CallableStatement statement = connection.prepareCall(QUERY_TEMPLATE_7))
{
// Populate the query template with the data collected from the user
statement.setInt(1,SSN );
statement.setFloat(2, Salary);
statement.setString(3, Status);
statement.setDate(4, date);
System.out.println("Dispatching the query...");
// Actually execute the populated query
final int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
}
}
catch(InputMismatchException e)
{
System.out.println("Caught Input mismatch");
sc.nextLine();
break;
}
catch (Exception e)
{
System.out.println("Couldn't execute the above query!");
System.out.println("Reason -- ");
System.out.println(e.getMessage());
System.out.println("Try again !!!");
}
//for Reports
System.out.println("Number of teams to assosciate to");
int count2=sc.nextInt();
sc.nextLine();
while (count2!=0) {
System.out.println("Please enter SSN:");
final int SSN = sc.nextInt();
sc.nextLine();
System.out.println("Please enter Team Name to which you want to assosciate:");
final String Team_Name= sc.nextLine();
System.out.println("Please enter Report Date:");
final String date_J=sc.nextLine();
Date date=Date.valueOf(date_J);
System.out.println("Please enter Report Description:");
final String Desc=sc.nextLine();
try (final Connection connection = DriverManager.getConnection(URL)) {
try (final CallableStatement statement1 = connection.prepareCall(QUERY_TEMPLATE_8))
{
// Populate the query template with the data collected from the user
statement1.setInt(1,SSN );
statement1.setString(2, Team_Name);
statement1.setDate(3, date);
statement1.setString(4, Desc);
System.out.println("Dispatching the query...");
// Actually execute the populated query
final int rows_inserted = statement1.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
}
count2--;}
break;
case "6":
try {
//for Expense
System.out.println("Please enter SSN:");
final int SSN = sc.nextInt();
sc.nextLine();
System.out.println("Please enter Expense Amount:");
final Float Expense_A= sc.nextFloat();
sc.nextLine();
System.out.println("Please enter Expense Description:");
final String Description=sc.nextLine();
System.out.println("Please enter Expense Date:");
final int date_J=sc.nextInt();
//Date date=Date.valueOf(date_J);
try (final Connection connection = DriverManager.getConnection(URL)) {
try (final CallableStatement statement1 = connection.prepareCall(QUERY_TEMPLATE_9))
{
// Populate the query template with the data collected from the user
statement1.setInt(1,SSN );
statement1.setFloat(2, Expense_A);
statement1.setString(3, Description);
statement1.setInt(4, date_J);
System.out.println("Dispatching the query...");
// Actually execute the populated query
final int rows_inserted = statement1.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
}
}
catch(InputMismatchException e)
{
System.out.println("Caught Input mismatch");
sc.nextLine();
break;
}
catch (Exception e)
{
System.out.println("Couldn't execute the above query!");
System.out.println("Reason -- ");
System.out.println(e.getMessage());
System.out.println("Try again !!!");
}
break;
case "7": // Insert a new Organization
// Collect the new Organization data from the user
System.out.println("Please enter Organization Name:");
try {
sc.nextLine();
final String Org_Name = sc.nextLine();
System.out.println("Please enter Organization Address:");
final String Org_Address = sc.nextLine();
System.out.println("Please Org Number:");
final int Org_Number = sc.nextInt();
sc.nextLine();
System.out.println("Please enter Org Contact :");
final String Org_Contact = sc.nextLine();
//converting string into sql date
System.out.println("Connecting to the database...");
// Get a database connection and prepare a query statement
try (final Connection connection = DriverManager.getConnection(URL)) {
try (final CallableStatement statement = connection.prepareCall(QUERY_TEMPLATE_10))
{
// Populate the query template with the data collected from the user
statement.setString(1,Org_Name );
statement.setString(2, Org_Address);
statement.setInt(3, Org_Number);
statement.setString(4, Org_Contact);
System.out.println("Dispatching the query...");
// Actually execute the populated query
final int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
}
}
catch(InputMismatchException e)
{
System.out.println("Caught Input mismatch");
sc.nextLine();
break;
}
catch (Exception e)
{
System.out.println("Couldn't execute the above query!");
System.out.println("Reason -- ");
System.out.println(e.getMessage());
System.out.println("Try again !!!");
}
//for Sponsor
System.out.println("Number of teams to assosciate to");
int count3=sc.nextInt();
sc.nextLine();
while (count3!=0) {
System.out.println("Please enter Team Name to which you want to assosciate:");
final String Team_Name= sc.nextLine();
System.out.println("Please enter Organization Name:");
final String Org_Name = sc.nextLine();
try (final Connection connection = DriverManager.getConnection(URL)) {
try (final CallableStatement statement1 = connection.prepareCall(QUERY_TEMPLATE_11))
{
// Populate the query template with the data collected from the user
statement1.setString(1,Team_Name );
statement1.setString(2, Org_Name);
System.out.println("Dispatching the query...");
// Actually execute the populated query
final int rows_inserted = statement1.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
}
count3--;}
break;
case "8": // Insert a new Donor
// Collect the new Donor data from the user
try {
System.out.println("Please enter SSN:");
final int SSN = sc.nextInt();
sc.nextLine();
System.out.println("Please enter Anonymous state of Donor :");
final String Anonymous_S = sc.nextLine();
//converting string into sql date
System.out.println("Connecting to the database...");
// Get a database connection and prepare a query statement
try (final Connection connection = DriverManager.getConnection(URL)) {
try (final CallableStatement statement = connection.prepareCall(QUERY_TEMPLATE_12))
{
// Populate the query template with the data collected from the user
statement.setInt(1,SSN );
statement.setString(2,Anonymous_S );
System.out.println("Dispatching the query...");
// Actually execute the populated query
final int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
}
}
catch(InputMismatchException e)
{
System.out.println("Caught Input mismatch");
sc.nextLine();
break;
}
catch (Exception e)
{
System.out.println("Couldn't execute the above query!");
System.out.println("Reason -- ");
System.out.println(e.getMessage());
System.out.println("Try again !!!");
}
//for Donations
System.out.println("Number of donations to assosciate to");
int count4=sc.nextInt();
sc.nextLine();
while (count4!=0) {
System.out.println("Please enter Donation payment 1:check and 2:Credit:");
int Don_type= sc.nextInt();
if (Don_type==1) {
System.out.println("Please enter SSN:");
final int SSN = sc.nextInt();
sc.nextLine();
System.out.println("Please enter Date of Donation:");
final String date_J=sc.nextLine();
Date date=Date.valueOf(date_J);
System.out.println("Please enter Donation Amount:");
final float D_amount=sc.nextFloat();
sc.nextLine();
System.out.println("Please enter Donation type:");
final String D_Type = sc.nextLine();
System.out.println("Please enter Campaign Name:");
final String Camp_Name = sc.nextLine();
System.out.println("Please enter Cheque Number:");
final int Check_Num = sc.nextInt();
try (final Connection connection = DriverManager.getConnection(URL)) {
try (final CallableStatement statement1 = connection.prepareCall(QUERY_TEMPLATE_13))
{
// Populate the query template with the data collected from the user
statement1.setInt(1,SSN );
statement1.setDate(2, date);
statement1.setFloat(3, D_amount);
statement1.setString(4, D_Type);
statement1.setString(5, Camp_Name);
statement1.setInt(6, Check_Num);
System.out.println("Dispatching the query...");
// Actually execute the populated query
final int rows_inserted = statement1.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
}
}
else {
System.out.println("Please enter SSN:");
final int SSN = sc.nextInt();
sc.nextLine();
System.out.println("Please enter Date of Donation:");
final String date_J=sc.nextLine();
Date date=Date.valueOf(date_J);
System.out.println("Please enter Donation Amount:");
final float D_amount=sc.nextFloat();
sc.nextLine();
System.out.println("Please enter Donation type:");
final String D_Type = sc.nextLine();
System.out.println("Please enter Campaign Name:");
final String Camp_Name = sc.nextLine();
System.out.println("Please enter Card Number:");
final int Check_Num = sc.nextInt();
sc.nextLine();
System.out.println("Please enter Card Type:");
final String CardType= sc.nextLine();
System.out.println("Please enter Expiry Date:");
final String date_E=sc.nextLine();
Date dateE=Date.valueOf(date_E);
try (final Connection connection = DriverManager.getConnection(URL)) {
try (final CallableStatement statement1 = connection.prepareCall(QUERY_TEMPLATE_14))
{
// Populate the query template with the data collected from the user
statement1.setInt(1,SSN );
statement1.setDate(2, date);
statement1.setFloat(3, D_amount);
statement1.setString(4, D_Type);
statement1.setString(5, Camp_Name);
statement1.setInt(6, Check_Num);
statement1.setString(7, CardType);
statement1.setDate(8, dateE);
System.out.println("Dispatching the query...");
// Actually execute the populated query
final int rows_inserted = statement1.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
}
}
count4--;}
break;
case "9": // Insert a new Organization+Donations
// Collect the new Organization data from the user
System.out.println("Please enter Organization Name:");
try {
sc.nextLine();
final String Org_Name = sc.nextLine();
System.out.println("Please enter Organization Address:");
final String Org_Address = sc.nextLine();
System.out.println("Please Org Number:");
final int Org_Number = sc.nextInt();
sc.nextLine();
System.out.println("Please enter Org Contact :");
final String Org_Contact = sc.nextLine();
//converting string into sql date
System.out.println("Connecting to the database...");
// Get a database connection and prepare a query statement
try (final Connection connection = DriverManager.getConnection(URL)) {
try (final CallableStatement statement = connection.prepareCall(QUERY_TEMPLATE_10))
{
// Populate the query template with the data collected from the user
statement.setString(1,Org_Name );
statement.setString(2, Org_Address);
statement.setInt(3, Org_Number);
statement.setString(4, Org_Contact);
System.out.println("Dispatching the query...");
// Actually execute the populated query
final int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
}
}
catch(InputMismatchException e)
{
System.out.println("Caught Input mismatch");
sc.nextLine();
break;
}
catch (Exception e)
{
System.out.println("Couldn't execute the above query!");
System.out.println("Reason -- ");
System.out.println(e.getMessage());
System.out.println("Try again !!!");
}
try {
System.out.println("Please enter Organization Name:");
final String Organization_N = sc.nextLine();
System.out.println("Please enter Anonymous state of Donor :");
final String Anonymous_S = sc.nextLine();
//converting string into sql date
System.out.println("Connecting to the database...");
// Get a database connection and prepare a query statement
try (final Connection connection = DriverManager.getConnection(URL)) {
try (final CallableStatement statement = connection.prepareCall(QUERY_TEMPLATE_15))
{
// Populate the query template with the data collected from the user
statement.setString(1,Organization_N );
statement.setString(2,Anonymous_S );
System.out.println("Dispatching the query...");
// Actually execute the populated query
final int rows_inserted = statement.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
}
}
catch(InputMismatchException e)
{
System.out.println("Caught Input mismatch");
sc.nextLine();
break;
}
catch (Exception e)
{
System.out.println("Couldn't execute the above query!");
System.out.println("Reason -- ");
System.out.println(e.getMessage());
System.out.println("Try again !!!");
}
//for Donations
System.out.println("Number of donations to assosciate to");
int count5=sc.nextInt();
sc.nextLine();
while (count5!=0) {
System.out.println("Please enter Donation payment 1:check and 2:Credit:");
int Don_type= sc.nextInt();
sc.nextLine();
if (Don_type==1) {
System.out.println("Please enter Org Name:");
final String Org_Name = sc.nextLine();
System.out.println("Please enter Date of Donation:");
final String date_J=sc.nextLine();
Date date=Date.valueOf(date_J);
System.out.println("Please enter Donation Amount:");
final float D_amount=sc.nextFloat();
sc.nextLine();
System.out.println("Please enter Donation type:");
final String D_Type = sc.nextLine();
System.out.println("Please enter Campaign Name:");
final String Camp_Name = sc.nextLine();
System.out.println("Please enter Cheque Number:");
final int Check_Num = sc.nextInt();
try (final Connection connection = DriverManager.getConnection(URL)) {
try (final CallableStatement statement1 = connection.prepareCall(QUERY_TEMPLATE_16))
{
// Populate the query template with the data collected from the user
statement1.setString(1,Org_Name );
statement1.setDate(2, date);
statement1.setFloat(3, D_amount);
statement1.setString(4, D_Type);
statement1.setString(5, Camp_Name);
statement1.setInt(6, Check_Num);
System.out.println("Dispatching the query...");
// Actually execute the populated query
final int rows_inserted = statement1.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
}
}
else {
System.out.println("Please enter Org Name:");
final String Org_Name = sc.nextLine();
System.out.println("Please enter Date of Donation:");
final String date_J=sc.nextLine();
Date date=Date.valueOf(date_J);
System.out.println("Please enter Donation Amount:");
final float D_amount=sc.nextFloat();
sc.nextLine();
System.out.println("Please enter Donation type:");
final String D_Type = sc.nextLine();
System.out.println("Please enter Campaign Name:");
final String Camp_Name = sc.nextLine();
System.out.println("Please enter Card Number:");
final int Check_Num = sc.nextInt();
sc.nextLine();
System.out.println("Please enter Card Type:");
final String CardType= sc.nextLine();
System.out.println("Please enter Expiry Date:");
final String date_E=sc.nextLine();
Date dateE=Date.valueOf(date_E);
try (final Connection connection = DriverManager.getConnection(URL)) {
try (final CallableStatement statement1 = connection.prepareCall(QUERY_TEMPLATE_17))
{
// Populate the query template with the data collected from the user
statement1.setString(1,Org_Name );
statement1.setDate(2, date);
statement1.setFloat(3, D_amount);
statement1.setString(4, D_Type);
statement1.setString(5, Camp_Name);
statement1.setInt(6, Check_Num);
statement1.setString(7, CardType);
statement1.setDate(8, dateE);
System.out.println("Dispatching the query...");
// Actually execute the populated query
final int rows_inserted = statement1.executeUpdate();
System.out.println(String.format("Done. %d rows inserted.", rows_inserted));
}
}
}
count5--;}
break;
case "10": //Retrieve data of the client
try {
System.out.println("Please enter SSN:");
final int SSN = sc.nextInt();
//converting string into sql date
ResultSet resultSet=stmt.executeQuery("select Doctor_Name,Doctor_phone from Client WHERE SSN="+SSN+"");
System.out.println("Contents of the Client table:");
System.out.println("Doctor Name | Doctor Phone");
while (resultSet.next()) {
System.out.println(
String.format("%s | %s ", resultSet.getString(1), resultSet.getInt(2)));
}}
catch(InputMismatchException e)
{
System.out.println("Caught Input mismatch");
sc.nextLine();
break;
}
catch (Exception e)
{
System.out.println("Couldn't execute the above query!");
System.out.println("Reason -- ");
System.out.println(e.getMessage());
System.out.println("Try again !!!");
}
break;
case "11": //Retrieve the data of Expenses
try {
System.out.println("Please enter Start Date:");
sc.nextLine();
final int date_Start=sc.nextInt();
System.out.println("Please enter End Date:");
final int date_End=sc.nextInt();
ResultSet resultSet1=stmt.executeQuery("select SSN,sum(Amount) from Expenses WHERE Expense_Date "
+ "between "+date_Start+" and "+date_End+" group by SSN order by sum(Amount) asc");
System.out.println("Contents of the Expense table:");
System.out.println("SSN| Sum in total");
while (resultSet1.next()) {
System.out.println(
String.format("%s| %s ", resultSet1.getInt(1), resultSet1.getFloat(2)));
}}
catch(InputMismatchException e)
{
System.out.println("Caught Input mismatch");
sc.nextLine();
break;
}
catch (Exception e)
{
System.out.println("Couldn't execute the above query!");
System.out.println("Reason -- ");
System.out.println(e.getMessage());
System.out.println("Try again !!!");
}
break;
case "12": //Retriece list of Volunteers
try {
System.out.println("Please enter SSN:");
final int SSN = sc.nextInt();
ResultSet resultSet2=stmt.executeQuery("select SSN,Joining_Date,Training_Date,Training_Loc"
+ " FROM Volunteers WHERE SSN in "
+ "(SELECT C.SSN FROM Cares C,Serves S "
+ "WHERE C.SSN="+SSN+" and S.SSN=C.SSN)");
System.out.println("Contents of the Volunteers table:");
System.out.println("SSN|Joining_Date|Training_Date|Training_Loc");
if (resultSet2!=null) {
while (resultSet2.next()) {
System.out.println(
String.format("%s| %s|%s|%s ", resultSet2.getInt(1), resultSet2.getDate(2),resultSet2.getDate(3),resultSet2.getString(4)));
}}
}
catch(InputMismatchException e)
{
System.out.println("Caught Input mismatch");
sc.nextLine();
break;
}
catch (Exception e)
{
System.out.println("Couldn't execute the above query!");
System.out.println("Reason -- ");
System.out.println(e.getMessage());
System.out.println("Try again !!!");
}
break;
case "13": //Retrieve info of client
try {
ResultSet resultSet2=stmt.executeQuery("SELECT Name,Mail_address,Email_address,Home_phone,"
+ "Work_phone,Cell_phone from Person P,Client C "
+ "WHERE C.SSN=P.SSN AND C.SSN IN (SELECT R.SSN FROM Cares R,Client C "
+ "WHERE C.SSN=R.SSN AND R.T_Name IN (SELECT R.T_Name FROM Cares R,Teams T WHERE "
+ "R.T_Name=T.T_Name AND T.T_Name IN (SELECT T.T_Name FROM Teams T,Sponsors S WHERE"
+ " T.T_Name=S.T_Name AND S.Org_Name LIKE '[B-K]%' )))"
+ " ORDER BY Name");
System.out.println("Contents of the Volunteers table:");
System.out.println("SSN|Joining_Date|Training_Date|Training_Loc");
while (resultSet2.next()) {
System.out.println(
String.format("%s| %s|%s|%s|%s|%s ", resultSet2.getString(1), resultSet2.getString(2),resultSet2.getString(3),resultSet2.getInt(4),resultSet2.getInt(5),resultSet2.getInt(6)));
}}
catch(InputMismatchException e)
{
System.out.println("Caught Input mismatch");
sc.nextLine();