-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudentRecordSystem.java
768 lines (734 loc) · 31.1 KB
/
StudentRecordSystem.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
import java.io.*;
import java.sql.*;
public class StudentRecordSystem
{
int reg_no;
String f_name;
String l_name;
int year;
char section;
int roll_no;
String email; //Variables for accepting data from user
String dob;
String phno;
String course_id;
String course_name;
double cgpa;
String query;
/* static final String DRIVER = "com.mysql.jdbc.Driver"; */
static final String DRIVER = "com.mysql.cj.jdbc.Driver";
static final String URL = "jdbc:mysql://localhost:3306/mydb"; //For connection to database
/* static final String USERNAME = "system"; */
static final String USERNAME = "root";
/* static final String PASSWORD = "mysql"; */
static final String PASSWORD = "helloworld";
static Connection con = null;
static Statement st = null;
static ResultSet rs = null;
static PreparedStatement ps = null;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//Function for clearing the screen
public static void clrscr()
{
//Clears Screen in java
try
{
if (System.getProperty("os.name").contains("Windows"))
{
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
}
else
{
Runtime.getRuntime().exec("clear");
}
}
catch (IOException | InterruptedException ex)
{
}
}
//Creating tables if not present.
public void checkTables()throws Exception
{
st = con.createStatement();
query = "SELECT * FROM information_schema.tables WHERE table_name='admins'";
rs = st.executeQuery(query);
if(!rs.next())
{
//creating Admins table
query = "CREATE TABLE IF NOT EXISTS admins(admin_name VARCHAR(50), password VARCHAR(255), PRIMARY KEY(admin_name))";
st.executeUpdate(query);
System.out.println("Admins Table Created\n");
System.out.println("Enter Admin Name:");
String u = br.readLine();
System.out.println("Enter Password:");
Console console = System.console();
char arr[] = console.readPassword();
String p = String.valueOf(arr);
query = "INSERT INTO admins VALUES(?, SHA2(?, 256))";
ps = con.prepareStatement(query);
ps.setString(1, u);
ps.setString(2, p);
ps.executeUpdate();
clrscr();
System.out.println("Admin account created\n");
//creating Students table
query = "CREATE TABLE IF NOT EXISTS students(reg_no INT PRIMARY KEY, f_name VARCHAR(50) NOT NULL, l_name VARCHAR(50), s_year INT NOT NULL, section VARCHAR(1) NOT NULL, roll_no INT NOT NULL, email VARCHAR(50) CHECK(email LIKE '%_@__%.__%') UNIQUE, dob DATE NOT NULL, phno CHAR(10) NOT NULL UNIQUE,CONSTRAINT sec_roll UNIQUE(s_year, section, roll_no))";
st.executeUpdate(query);
System.out.println("Students Table Created\n");
//creating Courses table
query = "CREATE TABLE IF NOT EXISTS courses(course_id INT PRIMARY KEY, course_name VARCHAR(50))";
st.executeUpdate(query);
System.out.println("Courses Table Created\n");
//creating Enrolls table
query = "CREATE TABLE IF NOT EXISTS enrolls(reg_no INT PRIMARY KEY,course_id INT,FOREIGN KEY (reg_no) REFERENCES students(reg_no) ON DELETE CASCADE,FOREIGN KEY (course_id) REFERENCES courses(course_id) ON DELETE CASCADE)";
st.executeUpdate(query);
System.out.println("Enrolls Table Created\n");
//creating Grades table
query = "CREATE TABLE IF NOT EXISTS grades(reg_no INT PRIMARY KEY, cgpa DECIMAL(2, 1), FOREIGN KEY (reg_no) REFERENCES students(reg_no) ON DELETE CASCADE)";
st.executeUpdate(query);
System.out.println("Grades Table Created\n");
}
}
//Adding new admin
public void adminMenu1()throws Exception
{
clrscr();
System.out.println("Enter username of new admin: ");
String u = br.readLine().toUpperCase();
System.out.println("Enter password of new admin: ");
Console console = System.console();
char arr[] = console.readPassword();
String p = String.valueOf(arr);
query = "INSERT INTO admins VALUES(?, SHA2(?, 256))";
ps = con.prepareStatement(query);
ps.setString(1, u);
ps.setString(2, p);
ps.executeUpdate();
clrscr();
System.out.println("\n"+u+" has been added to admin");
}
//Deleting admin
public void adminMenu2()throws Exception
{
clrscr();
System.out.println("Enter username of admin to delete: ");
String u = br.readLine().toUpperCase();
query = "DELETE FROM admins WHERE admin_name='"+u+"'";
int deleted = st.executeUpdate(query);
if(deleted!=0)
{
System.out.println("\n"+u+" has been deleted from admin");
}
else
{
System.out.println("\n"+u+" not found in admin");
}
}
//Generating Registration Number
public int generateRegNo()throws Exception
{
clrscr();
query = "SELECT MAX(reg_no) FROM students";
int x = 10001;
rs = st.executeQuery(query);
if(rs.next())
{
x = rs.getInt(1);
if(x == 0)
{
x = 10001;
}
else if(x >= 10001)
{
x++; //generates reg_no from 10001 and onwards.
}
}
return x;
}
//Adding all details of Student
public void adminMenu3()throws Exception
{
clrscr();
reg_no = generateRegNo();
System.out.println("Enter first name: ");
f_name = br.readLine().toUpperCase();
System.out.println("Enter last name: ");
l_name = br.readLine().toUpperCase();
System.out.println("Enter year: ");
year = Integer.parseInt(br.readLine());
System.out.println("Enter section: ");
section = Character.toUpperCase((char)br.read());
br.readLine();
System.out.println("Enter roll no.: ");
roll_no = Integer.parseInt(br.readLine());
System.out.println("Enter email: ");
email = br.readLine();
System.out.println("Enter date of birth: ");
dob = br.readLine();
System.out.println("Enter phone no.: ");
phno = br.readLine();
System.out.println("Enter course ID: ");
course_id = br.readLine().toUpperCase();
System.out.println("Enter grade: ");
cgpa = Double.parseDouble(br.readLine());
query = "INSERT INTO students VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)";
ps = con.prepareStatement(query);
ps.setInt(1, reg_no);
ps.setString(2, f_name);
ps.setString(3, l_name);
ps.setInt(4, year);
ps.setString(5, String.valueOf(section));
ps.setInt(6, roll_no);
ps.setString(7, email);
ps.setString(8, dob);
ps.setString(9, phno);
ps.executeUpdate();
query = "INSERT INTO enrolls VALUES(?, ?)";
ps = con.prepareStatement(query);
ps.setInt(1, reg_no);
ps.setString(2, course_id);
ps.executeUpdate();
query = "INSERT INTO grades VALUES(?, ?)";
ps = con.prepareStatement(query);
ps.setInt(1, reg_no);
ps.setDouble(2, cgpa);
ps.executeUpdate();
System.out.println("\nInsertion of Student details successful");
}
//Deleting student details
public void adminMenu4()throws Exception
{
clrscr();
System.out.println("Enter registration number of student to delete: ");
int r = Integer.parseInt(br.readLine());
query = "DELETE FROM students WHERE reg_no="+r;
int deleted = st.executeUpdate(query);
if(deleted!=0)
{
query = "DELETE FROM enrolls WHERE reg_no="+r;
st.executeUpdate(query);
query = "DELETE FROM grades WHERE reg_no="+r;
st.executeUpdate(query);
System.out.println("\nDeletion of student details successful");
}
else
{
System.out.println("Student with Registration Number = "+r+" not found!");
}
}
//Adding new Course details
public void adminMenu5()throws Exception
{
clrscr();
System.out.println("Enter Course ID");
String c = br.readLine().toUpperCase();
System.out.println("Enter Course Name: ");
String cn = br.readLine().toUpperCase();
query = "INSERT INTO courses VALUES(?, ?)";
ps = con.prepareStatement(query);
ps.setString(1, c);
ps.setString(2, cn);
ps.executeUpdate();
System.out.println("\nInsertion of new course details successful");
}
//Deleting a Course
public void adminMenu6()throws Exception
{
clrscr();
System.out.println("Enter course name: ");
String cn = br.readLine().toUpperCase();
query = "SELECT s.reg_no, s.f_name, s.l_name, s.year, s.section, s.roll_no, s.email, s.dob, s.phno, g.cgpa FROM students s INNER JOIN enrolls e ON s.reg_no=e.reg_no INNER JOIN courses c ON e.course_id=c.course_id INNER JOIN grades g ON s.reg_no=g.reg_no WHERE course_name='"+cn+"'";
rs = st.executeQuery(query);
if(!rs.next())
{
query = "DELETE FROM courses WHERE course_name='"+cn+"'";
int deleted = st.executeUpdate(query);
if(deleted!=0)
{
System.out.println("\nDeletion of course details successful");
}
else
{
System.out.println("\nDeletion of course details unsuccessful");
}
}
else
{
System.out.println("\nCourse is in progress. Cannot be removed!");
}
}
//Updating phone number of a student
public void adminMenu7()throws Exception
{
clrscr();
System.out.println("Enter registration number: ");
int r = Integer.parseInt(br.readLine());
System.out.println("Enter new phone number: ");
String ph = br.readLine();
query = "UPDATE students SET phno='"+ph+"' WHERE reg_no="+r;
int updated = st.executeUpdate(query);
if(updated!=0)
{
System.out.println("\nPhone number of student registration number = "+r+" has been updated");
}
else
{
System.out.println("\nUpdation Unsuccessful. Registration Number does not exist!");
}
}
//Updating section and roll number
public void adminMenu8()throws Exception
{
clrscr();
System.out.println("Enter registration number: ");
int r = Integer.parseInt(br.readLine());
System.out.println("Enter new section: ");
char s = (char)br.read();
br.readLine();
System.out.println("Enter new roll number: ");
int ro = Integer.parseInt(br.readLine());
query = "UPDATE students SET section='"+s+"', roll_no="+ro+" WHERE reg_no="+r;
int updated = st.executeUpdate(query);
if(updated!=0)
{
System.out.println("\nSection and roll number of student registration number = "+r+" has been updated");
}
else
{
System.out.println("\nUpdation Unsuccessful. Registration Number does not exist!");
}
}
//Updating the course enrolled by a student
public void adminMenu9()throws Exception
{
clrscr();
System.out.println("Enter registration number: ");
int r = Integer.parseInt(br.readLine());
System.out.println("Enter new course id: ");
String id = br.readLine();
query = "SELECT * FROM courses WHERE course_id="+id;
rs = st.executeQuery(query);
if(!rs.next())
{
System.out.println("\nCourse does not exist!");
}
else
{
query = "UPDATE enrolls SET course_id='"+id+"' WHERE reg_no="+r;
int updated = st.executeUpdate(query);
if(updated!=0)
{
System.out.println("\nUpdation of course for student registration number = "+r+" has been successful");
}
else
{
System.out.println("\nUpdation Unsuccessful. Registration Number/Course ID does not exist!");
}
}
}
//Updating the cgpa of a student
public void adminMenu10()throws Exception
{
clrscr();
st = con.createStatement();
System.out.println("Enter registration number: ");
int r = Integer.parseInt(br.readLine());
System.out.println("Enter new cgpa: ");
double c = Double.parseDouble(br.readLine());
query = "UPDATE grades SET cgpa="+c+" WHERE reg_no="+r;
int updated = st.executeUpdate(query);
if(updated!=0)
{
System.out.println("\nUpdation of cgpa of student registration number = "+r+" has been successful");
}
else
{
System.out.println("\nUpdation Unsuccessful. Registration Number does not exist!");
}
}
//Function for all the admin operations
public boolean admin()throws Exception
{
clrscr();
System.out.println("Enter admin username: ");
String u = br.readLine();
u = u.toUpperCase();
System.out.println("Enter password: ");
Console console = System.console();
char arr[] = console.readPassword();
String p = String.valueOf(arr);
query = "SELECT * FROM admins WHERE admin_name='"+u+"' AND password=SHA2('"+p+"', 256)";
rs = st.executeQuery(query);
if(rs.next())
{
clrscr();
System.out.println("\nWelcome "+u+"!");
int choice = 0;
while(choice!=11)
{
System.out.println("\n1. Add new admin");
System.out.println("2. Delete admin");
System.out.println("3. Add Student details");
System.out.println("4. Delete Student details");
System.out.println("5. Add Course details");
System.out.println("6. Delete Course details");
System.out.println("7. Update Phone Number");
System.out.println("8. Update Section and Roll No.");
System.out.println("9. Update enrolled course");
System.out.println("10. Update cgpa");
System.out.println("11. Exit");
System.out.println("\nEnter your choice: ");
choice = Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
adminMenu1();
break;
case 2:
adminMenu2();
break;
case 3:
adminMenu3();
break;
case 4:
adminMenu4();
break;
case 5:
adminMenu5();
break;
case 6:
adminMenu6();
break;
case 7:
adminMenu7();
break;
case 8:
adminMenu8();
break;
case 9:
adminMenu9();
break;
case 10:
adminMenu10();
break;
case 11:
clrscr();
break;
default:
System.out.println("Invalid Choice");
}
}
}
else
{
return false;
}
return true;
}
//For displaying the date in dd/mmm/yyyy
public String displayDate(String a)
{
String b = a.substring(0, 10);
return b;
}
//Display all students
public void userMenu1()throws Exception
{
clrscr();
query = "SELECT s.reg_no, s.f_name, s.l_name, s.s_year, s.section, s.roll_no, s.email, s.dob, s.phno, c.course_name, g.cgpa FROM students s INNER JOIN enrolls e ON s.reg_no=e.reg_no INNER JOIN courses c ON e.course_id=c.course_id INNER JOIN grades g ON s.reg_no=g.reg_no";
rs = st.executeQuery(query);
ResultSetMetaData rsm = rs.getMetaData();
System.out.printf("\n%-10s%-10s%-10s%-10s%-10s%-10s%-30s%-20s%-15s%-15s%-10s", rsm.getColumnName(1), rsm.getColumnName(2), rsm.getColumnName(3), rsm.getColumnName(4), rsm.getColumnName(5), rsm.getColumnName(6), rsm.getColumnName(7), rsm.getColumnName(8), rsm.getColumnName(9), rsm.getColumnName(10), rsm.getColumnName(11));
System.out.println();
while(rs.next())
{
System.out.printf("%-10d%-10s%-10s%-10d%-10s%-10d%-30s%-20s%-15s%-15s%-10s", rs.getInt("reg_no"), rs.getString("f_name"), rs.getString("l_name"), rs.getInt("s_year"), rs.getString("section"), rs.getInt("roll_no"), rs.getString("email"), displayDate(rs.getString("dob")), rs.getString("phno"), rs.getString("course_name"), rs.getBigDecimal("cgpa"));
System.out.println();
}
}
//Search student by name
public void userMenu2()throws Exception
{
clrscr();
System.out.println("Enter first name: ");
String f_name = br.readLine().toUpperCase();
System.out.println("Enter last name: ");
String l_name = br.readLine().toUpperCase();
query = "SELECT s.reg_no, s.s_year, s.section, s.roll_no, s.email, s.dob, s.phno, c.course_name, g.cgpa FROM students s INNER JOIN enrolls e ON s.reg_no=e.reg_no INNER JOIN courses c ON e.course_id=c.course_id INNER JOIN grades g ON s.reg_no=g.reg_no WHERE f_name='"+f_name+"' AND l_name='"+l_name+"'";
rs = st.executeQuery(query);
if(rs.next())
{
ResultSetMetaData rsm = rs.getMetaData(); //For printing the column names
System.out.printf("\n%-10s%-10s%-10s%-10s%-30s%-20s%-15s%-15s%-10s", rsm.getColumnName(1), rsm.getColumnName(2), rsm.getColumnName(3), rsm.getColumnName(4), rsm.getColumnName(5), rsm.getColumnName(6), rsm.getColumnName(7), rsm.getColumnName(8), rsm.getColumnName(9));
System.out.println();
System.out.printf("%-10d%-10d%-10s%-10d%-30s%-20s%-15s%-15s%-10s", rs.getInt("reg_no"), rs.getInt("s_year"), rs.getString("section").charAt(0), rs.getInt("roll_no"), rs.getString("email"), displayDate(rs.getString("dob")), rs.getString("phno"), rs.getString("course_name"), rs.getBigDecimal("cgpa"));
System.out.println();
}
else
{
System.out.println("\nGiven name does not exist!");
}
}
//Search student by year, section, roll no
public void userMenu3()throws Exception
{
clrscr();
System.out.println("Enter year: ");
year = Integer.parseInt(br.readLine());
System.out.println("Enter section: ");
section = (char)br.read();
br.readLine();
System.out.println("Enter roll number: ");
roll_no = Integer.parseInt(br.readLine());
query = "SELECT s.reg_no, s.f_name, s.l_name, s.email, s.dob, s.phno, c.course_name, g.cgpa FROM students s INNER JOIN enrolls e ON s.reg_no=e.reg_no INNER JOIN courses c ON e.course_id=c.course_id INNER JOIN grades g ON s.reg_no=g.reg_no WHERE s_year="+year+" AND section='"+section+"' AND roll_no="+roll_no;
rs = st.executeQuery(query);
if(rs.next())
{
ResultSetMetaData rsm = rs.getMetaData(); //For printing the column names
System.out.printf("\n%-10s%-10s%-10s%-30s%-20s%-15s%-15s%-10s", rsm.getColumnName(1), rsm.getColumnName(2), rsm.getColumnName(3), rsm.getColumnName(4), rsm.getColumnName(5), rsm.getColumnName(6), rsm.getColumnName(7), rsm.getColumnName(8));
System.out.println();
System.out.printf("%-10d%-10s%-10s%-30s%-20s%-15s%-15s%-10s", rs.getInt("reg_no"), rs.getString("f_name"), rs.getString("l_name"), rs.getString("email"), displayDate(rs.getString("dob")), rs.getString("phno"), rs.getString("course_name"), rs.getBigDecimal("cgpa"));
System.out.println();
}
else
{
System.out.println("\nStudent does not exist!");
}
}
//Search students by registration number
public void userMenu4()throws Exception
{
clrscr();
System.out.println("Enter registration no.: ");
reg_no = Integer.parseInt(br.readLine());
query = "SELECT s.f_name, s.l_name, s.s_year, s.section, s.roll_no, s.email, s.dob, s.phno, c.course_name, g.cgpa FROM students s INNER JOIN enrolls e ON s.reg_no=e.reg_no INNER JOIN courses c ON e.course_id=c.course_id INNER JOIN grades g ON s.reg_no=g.reg_no WHERE s.reg_no="+reg_no;
rs = st.executeQuery(query);
if(rs.next())
{
ResultSetMetaData rsm = rs.getMetaData(); //For printing the column names
System.out.printf("\n%-10s%-10s%-10s%-10s%-10s%-30s%-20s%-15s%-15s%-10s", rsm.getColumnName(1), rsm.getColumnName(2), rsm.getColumnName(3), rsm.getColumnName(4), rsm.getColumnName(5), rsm.getColumnName(6), rsm.getColumnName(7), rsm.getColumnName(8), rsm.getColumnName(9), rsm.getColumnName(10));
System.out.println();
System.out.printf("%-10s%-10s%-10d%-10s%-10d%-30s%-20s%-15s%-15s%-10s", rs.getString("f_name"), rs.getString("l_name"), rs.getInt("s_year"), rs.getString("section"), rs.getInt("roll_no"), rs.getString("email"), displayDate(rs.getString("dob")), rs.getString("phno"), rs.getString("course_name"), rs.getBigDecimal("cgpa"));
System.out.println();
}
else
{
System.out.println("\nGiven registration number does not exist!");
}
}
//Search students studying in a specific year
public void userMenu5()throws Exception
{
clrscr();
System.out.println("Enter year: ");
year = Integer.parseInt(br.readLine());
query = "SELECT s.reg_no, s.f_name, s.l_name, s.section, s.roll_no, s.email, s.dob, s.phno, c.course_name, g.cgpa FROM students s INNER JOIN enrolls e ON s.reg_no=e.reg_no INNER JOIN courses c ON e.course_id=c.course_id INNER JOIN grades g ON s.reg_no=g.reg_no WHERE s_year="+year;
rs = st.executeQuery(query);
ResultSetMetaData rsm = rs.getMetaData(); //For printing the column names
System.out.printf("\n%-10s%-10s%-10s%-10s%-10s%-30s%-20s%-15s%-15s%-10s", rsm.getColumnName(1), rsm.getColumnName(2), rsm.getColumnName(3), rsm.getColumnName(4), rsm.getColumnName(5), rsm.getColumnName(6), rsm.getColumnName(7), rsm.getColumnName(8), rsm.getColumnName(9), rsm.getColumnName(10));
System.out.println();
int count = 0;
while(true)
{
if(rs.next())
{
System.out.printf("%-10d%-10s%-10s%-10s%-10d%-30s%-20s%-15s%-15s%-10s", rs.getInt("reg_no"), rs.getString("f_name"), rs.getString("l_name"), rs.getString("section"), rs.getInt("roll_no"), rs.getString("email"), displayDate(rs.getString("dob")), rs.getString("phno"), rs.getString("course_name"), rs.getBigDecimal("cgpa"));
System.out.println();
count++;
}
else
{
break;
}
}
if(count==0)
{
System.out.println("\nGiven year does not exist!");
}
}
//Search students studying a specific course
public void userMenu6()throws Exception
{
clrscr();
System.out.println("Enter course name: ");
course_name = br.readLine().toUpperCase();
query = "SELECT s.reg_no, s.f_name, s.l_name, s.s_year, s.section, s.roll_no, s.email, s.dob, s.phno, g.cgpa FROM students s INNER JOIN enrolls e ON s.reg_no=e.reg_no INNER JOIN courses c ON e.course_id=c.course_id INNER JOIN grades g ON s.reg_no=g.reg_no WHERE course_name='"+course_name+"'";
rs = st.executeQuery(query);
ResultSetMetaData rsm = rs.getMetaData();
System.out.printf("\n%-10s%-10s%-10s%-10s%-10s%-10s%-30s%-20s%-15s%-15s", rsm.getColumnName(1), rsm.getColumnName(2), rsm.getColumnName(3), rsm.getColumnName(4), rsm.getColumnName(5), rsm.getColumnName(6), rsm.getColumnName(7), rsm.getColumnName(8), rsm.getColumnName(9), rsm.getColumnName(10));
System.out.println();
int count = 0;
while(true)
{
if(rs.next())
{
System.out.printf("%-10d%-10s%-10s%-10d%-10s%-10d%-30s%-20s%-15s%-15s", rs.getInt("reg_no"), rs.getString("f_name"), rs.getString("l_name"), rs.getInt("s_year"), rs.getString("section"), rs.getInt("roll_no"), rs.getString("email"), displayDate(rs.getString("dob")), rs.getString("phno"), rs.getString("cgpa"));
System.out.println();
count++;
}
else
{
break;
}
}
if(count==0)
{
System.out.println("\nGiven course does not exist!");
}
}
//Higher than a specific cgpa
public void userMenu7()throws Exception
{
clrscr();
System.out.println("Enter a cgpa:");
cgpa = Double.parseDouble(br.readLine());
query = "SELECT s.reg_no, s.f_name, s.l_name, s.s_year, s.section, s.roll_no, s.email, s.dob, s.phno, c.course_name FROM students s INNER JOIN enrolls e ON s.reg_no=e.reg_no INNER JOIN courses c ON e.course_id=c.course_id INNER JOIN grades g ON s.reg_no=g.reg_no WHERE g.cgpa > "+cgpa;
rs = st.executeQuery(query);
ResultSetMetaData rsm = rs.getMetaData();
System.out.printf("\n%-10s%-10s%-10s%-10s%-10s%-10s%-30s%-20s%-15s%-15s", rsm.getColumnName(1), rsm.getColumnName(2), rsm.getColumnName(3), rsm.getColumnName(4), rsm.getColumnName(5), rsm.getColumnName(6), rsm.getColumnName(7), rsm.getColumnName(8), rsm.getColumnName(9), rsm.getColumnName(10));
System.out.println();
while(rs.next())
{
System.out.printf("%-10d%-10s%-10s%-10d%-10s%-10d%-30s%-20s%-15s%-15s", rs.getInt("reg_no"), rs.getString("f_name"), rs.getString("l_name"), rs.getInt("s_year"), rs.getString("section"), rs.getInt("roll_no"), rs.getString("email"), displayDate(rs.getString("dob")), rs.getString("phno"), rs.getString("course_name"));
System.out.println();
}
}
//Display highest cgpa in respective course and year
public void userMenu8()throws Exception
{
clrscr();
query = "SELECT s.s_year, c.course_name, MAX(g.cgpa) AS cgpa FROM students s, enrolls e, courses c, grades g WHERE s.reg_no=e.reg_no AND c.course_id=e.course_id AND s.reg_no=g.reg_no GROUP BY c.course_name, s.s_year ORDER BY s_year, course_name";
rs = st.executeQuery(query);
ResultSetMetaData rsm = rs.getMetaData();
System.out.printf("\n%-10s%-20s%-10s", rsm.getColumnName(1), rsm.getColumnName(2), rsm.getColumnName(3));
System.out.println();
while(rs.next())
{
System.out.printf("%-10d%-20s%-10s", rs.getInt("s_year"), rs.getString("course_name"), rs.getBigDecimal("cgpa"));
System.out.println();
}
}
//Function for all user operations
public void user()throws Exception
{
clrscr();
int choice = 0;
while(choice!=9)
{
System.out.println("\n1. Display all students");
System.out.println("2. Search student by name");
System.out.println("3. Search student by year, section and roll no.");
System.out.println("4. Search student by registration number");
System.out.println("5. Search students studying in a specific year");
System.out.println("6. Search students studying a specific course");
System.out.println("7. Search students having cgpa higher than given");
System.out.println("8. Display highest cgpa in respective course and year");
System.out.println("9. Exit");
System.out.println("\nEnter your choice: ");
choice = Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
userMenu1();
break;
case 2:
userMenu2();
break;
case 3:
userMenu3();
break;
case 4:
userMenu4();
break;
case 5:
userMenu5();
break;
case 6:
userMenu6();
break;
case 7:
userMenu7();
break;
case 8:
userMenu8();
break;
case 9:
clrscr();
break;
default:
System.out.println("Invalid Choice");
}
}
}
public static void main(String args[])throws Exception
{
int choice = 0;
int fail_count = 0;
while(choice!=3)
{
try
{
Class.forName(DRIVER);
con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
st = con.createStatement();
StudentRecordSystem s = new StudentRecordSystem();
s.checkTables();
System.out.println("\n1. Admin Mode");
System.out.println("2. User Mode");
System.out.println("3. Exit");
System.out.println("\nEnter your choice: ");
choice = Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
boolean r = s.admin();
if(r==false)
{
fail_count++;
if(fail_count<3)
{
System.out.println("Wrong username or password. You have "+(3-fail_count)+" chances left");
}
else
{
System.out.println("You have entered wrong credentials 3 times. Terminating the program.");
System.exit(0);
}
}
else
{
clrscr();
}
break;
case 2:
s.user();
break;
case 3:
System.out.println("\nProgram Ends");
if(ps!=null)
{
ps.close();
}
if(st!=null)
{
st.close();
}
con.close();
System.exit(0);
break;
default:
System.out.println("\nInvalid Choice");
}
}
catch(SQLException e)
{
System.out.println("\nError in SQL\n");
continue;
}
catch(ClassNotFoundException e)
{
System.out.println("\nDriver Error\n");
}
}
}
}