You are on page 1of 3

package jdbc;

import java.sql.*;
import java.util.*;
import com.mysql.jdbc.Driver;
public class JDBC
{
public static void main(String[] args) throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost/student",name,query;
Scanner in=new Scanner(System.in);
int roll,res,opt,marks,first=0,second=0,third=0;
ResultSet rs;
Connection c=DriverManager.getConnection(url, "root", "password");
if(c==null)
{
System.out.println("Could not connect");
System.exit(0);
}
Statement s = c.createStatement();
for(;;)
{
System.out.println("Enter 1. to find total number of students");
System.out.println("Enter 2. to find average marks for each subject");
System.out.println("Enter 3. to find name of student getting highest
marks");
System.out.println("Enter 4. to find no. of students getting first
second and third division");
System.out.println("Enter 5. to find subjectwise toppers");
System.out.println("Enter 6. to find average marks");
System.out.println("Enter 7. to find student getting second highest
marks");
System.out.println("Enter 8. to exit");
opt=in.nextInt();
switch(opt)
{
case 1: query="Select count(*) from student.stud;";
rs=s.executeQuery(query);
rs.next();
System.out.println("Total number of students is-
"+rs.getInt(1));
break;
case 2: query="Select avg(math) from results;";
rs=s.executeQuery(query);
rs.next();
System.out.println("Average marks for math- "+
rs.getInt(1));
query="Select avg(science) from results;";
rs=s.executeQuery(query);
rs.next();
System.out.println("Average marks for science- "+
rs.getInt(1));
break;
case 3: query="select name, r.roll, (SUM(math+science)) AS Total
FROM results r,stud s where s.roll=r.roll Group by r.roll order by Total desc ;";
rs=s.executeQuery(query);
marks=0;
System.out.print("The student getting highest marks is/are
- ");
while(rs.next())
{
if(marks<=rs.getInt(3))
{
System.out.print(rs.getString(1)+",");
marks=rs.getInt(3);
}
}
System.out.println();
break;
case 4: query="select name, r.roll, (SUM(math+science)) AS Total
FROM results r,stud s where s.roll=r.roll Group by r.roll order by Total desc ;";
rs=s.executeQuery(query);
rs.next();
marks=rs.getInt(3);
while(marks==rs.getInt(3))
{
first++;
rs.next();
}
System.out.println("The number of students getting first
division is- "+first);
marks=rs.getInt(3);
while(marks==rs.getInt(3))
{
second++;
rs.next();
}
System.out.println("The number of students getting second
division is- "+second);
marks=rs.getInt(3);
while(marks==rs.getInt(3))
{
third++;
rs.next();
}
System.out.println("The number of students getting third
division is- "+third);
break;
case 5: query="select * from stud s where s.roll in (select roll
from results where math in (Select max(math) from results));";
rs=s.executeQuery(query);
rs.next();
System.out.println("Math topper is- "+rs.getString(1));
query="select * from stud s where s.roll in (select roll
from results where science in (Select max(science) from results));";
rs=s.executeQuery(query);
rs.next();
System.out.println("Science topper is- "+rs.getString(1));
break;
case 6: query="select avg(math+science) from results;";
rs=s.executeQuery(query);
rs.next();
System.out.println("Average marks are- "+rs.getInt(1));
break;
case 7: query="select name, r.roll, (SUM(math+science)) AS Total
FROM results r,stud s where s.roll=r.roll Group by r.roll order by Total desc ;";
rs=s.executeQuery(query);
rs.next();//take virtual cursor to first row to get
topper's marks
marks=rs.getInt(3);
while(marks==rs.getInt(3))
{
rs.next();
}
marks=rs.getInt(3);
System.out.print("The student getting second highest marks
is/are - ");
do
{
System.out.print(rs.getString(1)+",");
rs.next();
}while(marks==rs.getInt(3));
System.out.println();
break;

case 8: System.exit(0);
default:System.out.println("Wrong choice");;
}
}

You might also like