Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How to write a Java Program using the concept of Database? University grading sy

ID: 2246527 • Letter: H

Question

How to write a Java Program using the concept of Database?

University grading system maintains number of tables to store, retrieve and manipulate student marks These tables reside in a centrally or locally located server known as 'GradeProcessing'. 'TTC521' is one of the tables which contains following information/attributes for all the students enrolled in ITC521: "StudentD", "StudentName", "Quiz", "Assignmentl", "Assignment2", "Assignment3 "Exam", 'Results" and"Grade". A sample of the table may look like as follows StudentID StudentName Quiz Assigumentl Assigument2 Assignment3 Exam Results Grade 100 100 85 60 100 80 90 S0 90 91.75 HD 75 75.5 DI Write a JAVA program that would perform following tasks:

Explanation / Answer

import java.util.*;

import java.sql.*;

public class GradingSystem {

static Scanner sc;

static boolean isTable;

static Connection con;

static Statement st;

static ResultSet rs;

static String query;

  

public static void createTable()

{

if(isTable)

{

System.out.println("Table is already present ");

return;

}

try{

query = "create table ITC521(StudentID char(8),StudentName char(50),Quiz float,Assignment1 float,Assignment2 float,Assignment3 float,Exam float,Results float,Grade char)";

st = con.createStatement();

rs = st.executeQuery(query);

  

rs.close();

st.close();

  

isTable=true;

}

catch(Exception e)

{

e.printStackTrace();

}

}

  

public static void insert()

{

System.out.println("Enter the student ID");

String id = sc.next();

System.out.println("Enter Student Name");

String name = sc.next();

System.out.println("Enter the Quiz field");

float quiz = sc.nextFloat();

System.out.println("Enter the marks of Assignment 1");

float asg1 = sc.nextFloat();

System.out.println("Enter the marks of Assignment 2");

float asg2 = sc.nextFloat();

System.out.println("Enter the marks of Assignment 3");

float asg3 = sc.nextFloat();

System.out.println("Enter the Exam marks");

float ex = sc.nextFloat();

System.out.println("Enter the result");

float result = sc.nextFloat();

System.out.println("Enter the grade");

String grade = sc.next();

try{

query = "insert into ITC521 values(id,name,quiz,asg1,asg2,asg3,ex,result,grade)";

st = con.createStatement();

rs = st.executeQuery(query);

System.out.println("Record successfully inserted ");

rs.close();

st.close();

}

catch(Exception e)

{

e.printStackTrace();

}

}

  

public static void search()

{

System.out.println("To search by the field, please enter the right number StudentID -> 1 StudentName -> 2 Quiz -> 3 Assignment1 -> 4 Assignment2 -> 5 Assignment3 -> 6 Exam -> 7Results -> 8Grade -> 9");

int n = sc.nextInt();

String temp1="";

float temp2=0;

if(n==1 || n==2 || n==9)

temp1 = sc.nextLine();

else

temp2 = sc.nextFloat();

try{

query = "select * from ITC521";

st = con.createStatement();

rs = st.executeQuery(query);

  

System.out.println("StudentID StudentName Quiz Assignment1 Assignment2 Assignment3 Exam Results Grade");

while(rs.next())

{

if(n==1 || n==2 || n==9)

{

if(rs.getString(n).equals(temp1))

{

int i;

System.out.print(rs.getString(1) + " " + rs.getString(2) + " ");

for(i=3;i<=8;i++)

System.out.print(rs.getFloat(i) + " ");

System.out.print(rs.getFloat(9));

System.out.println();

}

}

else

{

if(rs.getFloat(n) == temp2)

{

int i;

System.out.print(rs.getString(1) + " " + rs.getString(2) + " ");

for(i=3;i<=8;i++)

System.out.print(rs.getFloat(i) + " ");

System.out.print(rs.getFloat(9));

System.out.println();

}

}   

}

  

rs.close();

st.close();

}

catch(Exception e)

{

e.printStackTrace();

}

}

  

public static void update()

{

System.out.println("Enter the student id whose record is to be updated ");

int id=sc.nextInt();

  

System.out.println("To search by the field, please enter the right number studentID -> 1 StudentName -> 2 Quiz -> 3 Assignment1 -> 4 Assignment2 -> 5 Assignment3 -> 6 Exam -> 7Results -> 8Grade -> 9");

int n = sc.nextInt();

  

String temp1="";

float temp2=0;

String[] arr={"StudentID","StudentName","Quiz","Assignment1","Assignment2","Assignment3","Exam","Results","Grade"};

  

if(n==1 || n==2 || n==9)

temp1 = sc.nextLine();

else

temp2 = sc.nextFloat();

  

try{

if(n==1 || n==2 || n==9)

query = "update ITC521 set "+arr[n-1]+"="+temp1+" where StudentID="+id;

else

query = "update ITC521 set "+arr[n-1]+"="+temp2+" where StudentID="+id;

  

st = con.createStatement();

st.executeUpdate(query);

  

rs.close();

st.close();

}

catch(Exception e)

{

e.printStackTrace();

}

}

  

public static double CalculateResult()

{

try

{

System.out.println("Enter the student id whose record is to be updated ");

int id=sc.nextInt();

  

query = "select * from ITC521 where StudentID="+id;

st = con.createStatement();

rs = st.executeQuery(query);

  

double result = rs.getFloat(3)*0.05 + rs.getFloat(4)*0.15 + rs.getFloat(5)*0.2 + rs.getFloat(6)*0.1 + rs.getFloat(7)*0.5;

rs.close();

st.close();

return result;

}

catch(Exception e)

{

e.printStackTrace();

}

return 0;

}

  

public static void CalculateGrade(Double result)

{

if(result >= 85)

System.out.println("HD");

else if(result >= 75 && result < 85)

System.out.println("DI");

else if(result >= 65 && result < 75)

System.out.println("CR");

else if(result >= 50 && result < 65)

System.out.println("PS");

else if(result >= 75 && result < 50)

System.out.println("FL");

}

  

public static void main(String[] args)

{

sc = new Scanner(System.in);

try

{

Class.forName("com.mysql.jdbc.Driver");

// assuming the name of database is Grade

Connection con=DriverManager.getConnection("jdbc:mysql://GradeProcessing/Grade","root","root");

  

while(true)

{

System.out.println("For the following operations 1. Create Table 2. Insert Record 3. Search 4. Update 5. Calculate Result 6. Calculate Grade Enter the option number to perform them");

int n=sc.nextInt();

double result;

if(n==1)

createTable();

else if(n==2)

insert();

else if(n==3)

search();

else if(n==4)

update();

else if(n==5)

result = CalculateResult();

else if(n==6)

{

result=CalculateResult();

CalculateGrade(result);

}

  

System.out.println("If you want to perform another operation press 1, else press 0 ");

int op=sc.nextInt();

if(op!=1)

break;

}

  

con.close();

}

catch(SQLException | ClassNotFoundException e)

{

e.printStackTrace();

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote