|
| 1 | +importjava.sql.*; |
| 2 | +importjava.io.*; |
| 3 | +classAggregateFunction { |
| 4 | + |
| 5 | +privatestaticConnectioncon; |
| 6 | + |
| 7 | +inttotalStudents() { |
| 8 | + |
| 9 | +Stringq ="Select count(Name) from Student"; |
| 10 | +try { |
| 11 | +Statementstmt =con.createStatement(); |
| 12 | + |
| 13 | +ResultSetset =stmt.executeQuery(q); |
| 14 | +if(set.next()) { |
| 15 | + |
| 16 | +returnset.getInt(1); |
| 17 | +} |
| 18 | +}catch(Exceptione) { |
| 19 | + |
| 20 | +e.printStackTrace(); |
| 21 | +} |
| 22 | +return -1; |
| 23 | +} |
| 24 | +floatavgMarks() { |
| 25 | + |
| 26 | +Stringq ="Select avg(marks) from Student"; |
| 27 | +try { |
| 28 | +Statementstmt =con.createStatement(); |
| 29 | + |
| 30 | +ResultSetset =stmt.executeQuery(q); |
| 31 | +if(set.next()) { |
| 32 | + |
| 33 | +returnset.getFloat(1); |
| 34 | +} |
| 35 | +}catch(Exceptione) { |
| 36 | + |
| 37 | +e.printStackTrace(); |
| 38 | +} |
| 39 | +return0.0f; |
| 40 | +} |
| 41 | +voidmaxMarksDetails() { |
| 42 | + |
| 43 | + |
| 44 | +Stringq ="Select * from Student where marks = (Select max(marks) from Student)"; |
| 45 | +try { |
| 46 | +Statementstmt =con.createStatement(); |
| 47 | + |
| 48 | +ResultSetset =stmt.executeQuery(q); |
| 49 | +if(set.next()) { |
| 50 | + |
| 51 | +System.out.println("Roll No : " +set.getInt(1)); |
| 52 | +System.out.println("Name : " +set.getString(2)); |
| 53 | +System.out.println("Marks : " +set.getFloat(3)); |
| 54 | +System.out.println("City : " +set.getString(4)); |
| 55 | +System.out.println("Grade : " +set.getString(5)); |
| 56 | +}else { |
| 57 | + |
| 58 | +System.out.println("Student Not Found....!"); |
| 59 | +} |
| 60 | +}catch(Exceptione) { |
| 61 | + |
| 62 | +e.printStackTrace(); |
| 63 | +} |
| 64 | +} |
| 65 | +voidminMarksDetails() { |
| 66 | + |
| 67 | +Stringq ="Select * from Student where marks = (Select min(marks) from Student)"; |
| 68 | +try { |
| 69 | +Statementstmt =con.createStatement(); |
| 70 | + |
| 71 | +ResultSetset =stmt.executeQuery(q); |
| 72 | +if(set.next()) { |
| 73 | + |
| 74 | +System.out.println("Roll No : " +set.getInt(1)); |
| 75 | +System.out.println("Name : " +set.getString(2)); |
| 76 | +System.out.println("Marks : " +set.getFloat(3)); |
| 77 | +System.out.println("City : " +set.getString(4)); |
| 78 | +System.out.println("Grade : " +set.getString(5)); |
| 79 | +}else { |
| 80 | + |
| 81 | +System.out.println("Student Not Found....!"); |
| 82 | +} |
| 83 | +}catch(Exceptione) { |
| 84 | + |
| 85 | +e.printStackTrace(); |
| 86 | +} |
| 87 | +} |
| 88 | +floatsumMarks() { |
| 89 | + |
| 90 | +Stringq ="Select sum(marks) from Student"; |
| 91 | +try { |
| 92 | +Statementstmt =con.createStatement(); |
| 93 | + |
| 94 | +ResultSetset =stmt.executeQuery(q); |
| 95 | +if(set.next()) { |
| 96 | + |
| 97 | +returnset.getFloat(1); |
| 98 | +} |
| 99 | +}catch(Exceptione) { |
| 100 | + |
| 101 | +e.printStackTrace(); |
| 102 | +} |
| 103 | +return0.0f; |
| 104 | +} |
| 105 | +publicstaticvoidmain(String[]args)throwsIOException{ |
| 106 | + |
| 107 | +BufferedReaderbr =newBufferedReader(newInputStreamReader(System.in)); |
| 108 | +AggregateFunctionobj =newAggregateFunction(); |
| 109 | + |
| 110 | +Stringurl ="jdbc:mysql://localhost:3306/College"; |
| 111 | +Stringusr ="root"; |
| 112 | +Stringpwd ="Dhiraj@7465"; |
| 113 | +try { |
| 114 | + |
| 115 | +con =DriverManager.getConnection(url,usr,pwd); |
| 116 | +charch; |
| 117 | +do { |
| 118 | + |
| 119 | +System.out.println("1. Count of Total Students : "); |
| 120 | +System.out.println("2. Average of Student Marks : "); |
| 121 | +System.out.println("3. Max Marks Student Details : "); |
| 122 | +System.out.println("4. Min Marks Student Details : "); |
| 123 | +System.out.println("5. Sum of Student Marks :\n"); |
| 124 | + |
| 125 | +System.out.println("Enter your Choice : "); |
| 126 | +intchoice =Integer.parseInt(br.readLine()); |
| 127 | + |
| 128 | +switch(choice) { |
| 129 | + |
| 130 | +case1 : |
| 131 | +System.out.println("Total Student Count : " +obj.totalStudents() +"\n"); |
| 132 | +break; |
| 133 | +case2 : |
| 134 | +System.out.println("Average of Student Marks: " +obj.avgMarks() +"\n"); |
| 135 | +break; |
| 136 | +case3 : |
| 137 | +obj.maxMarksDetails(); |
| 138 | +break; |
| 139 | +case4 : |
| 140 | +obj.minMarksDetails(); |
| 141 | +break; |
| 142 | +case5 : |
| 143 | +System.out.println("Sum of Student Marks : " +obj.sumMarks() +"\n"); |
| 144 | +break; |
| 145 | +default : |
| 146 | +System.out.println("Wrong Choice"); |
| 147 | +} |
| 148 | +System.out.println("Do want Continue : "); |
| 149 | +ch = (char)br.readLine().charAt(0); |
| 150 | +}while(ch =='Y' ||ch =='y'); |
| 151 | +}catch(Exceptione) { |
| 152 | + |
| 153 | +e.printStackTrace(); |
| 154 | +} |
| 155 | +} |
| 156 | +} |