0% found this document useful (0 votes)
6 views

JDBC Assignment

Uploaded by

Akshay Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

JDBC Assignment

Uploaded by

Akshay Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

JDBC CONNECTIVITY - ASSIGNMENT 2

DBMS

Submitted Byr

Kavin.T - 71762108019
PROBLEM STATEMENT:

Schema:
Stud(stud_id,Stud_name,birth_date, total_credits);
Course(course_id,course_name, credits);
Takes(stud_id,course_id,sec_id,sem,year,grade)

1. Write a Java program using JDBC to


display the student details from the stud
relation for the ‘stud_id’ given as an input
from the terminal/console. Additionally, fetch
the course relation and display ‘course_id’,
‘course_name’ and ‘credits’ for all the
courses offered.

SQL CODE:

create table stud(stud_id varchar(37) not null,stud_name varchar(90),DOB


date,total_credits int,primary key(stud_id));
insert into stud values('001','Dharunika','2007-08-17',8);
insert into stud values('002','Kavi','2007-08-18',6);
insert into stud values('003','Hari','2007-08-17',7);
insert into stud values('004','Bala','2007-08-17',7);
select * from stud;
Create table Course(Course_id varchar(90) not null,course_name
varchar(90),credits int,primary key(Course_id));
insert into Course values('21AD41','Dis Maths',4);
insert into Course values('21AD42','DBMS',4);
insert into Course values('21AD43','ML',3);
insert into Course values('21AD44','Data Networks',3);
select * from Course;
create table Takes(stud_id varchar(90),course_id varchar(90),sec_id
varchar(90),sem varchar(90),year int,grade varchar(4),foreign key(stud_id)
references stud(stud_id),foreign key(course_id) references course(Course_id));
insert into Takes values('001','21AD41','SEC2','II',1,'A+');
insert into Takes values('001','21AD42','SEC2','II',1,'A');
insert into Takes values('003','21AD42','SEC1','III',2,'O');
insert into Takes values('003','21AD43','SEC1','III',2,'A');
insert into Takes values('002','21AD43','SEC2','II',1,'A');
insert into Takes values('002','21AD44','SEC2','II',1,'O');
insert into Takes values('004','21AD44','SEC1','IV',2,'B+');
insert into Takes values('004','21AD41','SEC1','IV',2,'O');
SELECT * FROM TAKES;

JDBC CODE :
public static void showDetails() throws Exception {
String url= "jdbc:mysql://localhost:3306/mydb";
String userName= "root";
String password = "Kavin@2003";
Scanner myObj=new Scanner(System.in);
System.out.print("Enter the ID: ");
String id=myObj.nextLine();
String query="select course_id from Takes where stud_id=?";
Connection con =
DriverManager.getConnection(url,userName,password);

PreparedStatement pst=con.prepareStatement(query);
pst.setString(1, id);
ResultSet rs=pst.executeQuery();

while(rs.next()) {
String cour_id=rs.getString(1);

String query2="select course_id,course_name,credits from


Course where course_id=?";

PreparedStatement pst1=con.prepareStatement(query2);
pst1.setString(1, cour_id);
ResultSet rs1=pst1.executeQuery();

while(rs1.next()) {
System.out.println("Course ID : "+rs1.getString(1));
System.out.println("Course Name: "+rs1.getString(2));
System.out.println("Course Credits: "+rs1.getInt(3));
}

;
con.close();
myObj.close();

2. Write a Java program using JDBC to update


the grade of a student as follows: take as
input from the terminal/console `stud_id
course_id sec_id semester year grade`
(separated by spaces) and update the ’
grade’ of an existing tuple in the takes
relation; but also, depending on change from
pass to fail or vice-versa, update the
students `tot_cred` correctly.
- -- null and F are fail grades, all other grades
are pass grades; grade values provided by
the user above can be assumed to be valid
grades, and you can assume the user will not
type null as a grade.
- -- We assume for simplicity that a student
who has already passed the course will not
be allowed to take it again.
- -- You can assume that tot_cred is not null;
JDBC CODE :

static int credits,total;


static String grd;

public static void updatecommit() throws Exception {

String url= "jdbc:mysql://localhost:3306/mydb";


String userName= "root";
String password = "Kavin@2003";
Connection con =
DriverManager.getConnection(url,userName,password);
Scanner myObj=new Scanner(System.in);

System.out.print("Enter the Student ID: ");


String stud_id=myObj.nextLine();
System.out.print("Enter the Course ID: ");
String cour_id=myObj.nextLine();
System.out.print("Enter the Section ID: ");
String sec_id=myObj.nextLine();
System.out.print("Enter the Semester: ");
String sem=myObj.nextLine();
System.out.print("Enter the Grade: ");
String grade=myObj.nextLine();
System.out.print("Enter the Year: ");
int year=myObj.nextInt();

String query="update Takes set grade=? where stud_id=? and


course_id=? and sec_id=? and sem=? and year=?;";
PreparedStatement pst=con.prepareStatement(query);
pst.setString(1, grade);
pst.setString(2,stud_id);
pst.setString(3, cour_id);
pst.setString(4, sec_id);
pst.setString(5,sem);
pst.setInt(6, year);

String query3="select grade from Takes where stud_id=? and


course_id=? and sec_id=? and sem=? and year=?;";
PreparedStatement pst3=con.prepareStatement(query3);
pst3.setString(1,stud_id);
pst3.setString(2, cour_id);
pst3.setString(3, sec_id);
pst3.setString(4,sem);
pst3.setInt(5, year);
ResultSet rs3=pst3.executeQuery();

while(rs3.next()) {
grd=rs3.getString(1);
}

int res=pst.executeUpdate();

if(res==0) {
System.out.println(" Record Not Found ");
}

System.out.println(" OLD GRADE : "+grd);


System.out.println(" NEW GRADE : "+grade);

if(grade.equals("F") && !grd.equals("F")) {


String query1="select credits from Course where
course_id=?;";
PreparedStatement pst1=con.prepareStatement(query1);
pst1.setString(1, cour_id);
ResultSet rs1=pst1.executeQuery();

while(rs1.next()) {
credits=rs1.getInt(1);

String query2="select total_credits from stud where


stud_id=?";
PreparedStatement pst2=con.prepareStatement(query2);
pst2.setString(1, stud_id);
ResultSet rs2=pst2.executeQuery();

while(rs2.next()) {
total=rs2.getInt(1);
}
System.out.println(" OLD CREDITS : "+ total);

total=total-credits;
System.out.println(" NEW CREDITS : " + total);

String query4="update stud set total_credits=? where


stud_id=?";
PreparedStatement pst4=con.prepareStatement(query4);
pst4.setInt(1, total);
pst4.setString(2, stud_id);
int res2=pst4.executeUpdate();
System.out.println(" ROWS AFFECTED IN STUD TABLE : "+res2);
}

if(!grade.equals("F") && grd.equals("F")) {


String query1="select credits from Course where
course_id=?;";
PreparedStatement pst1=con.prepareStatement(query1);
pst1.setString(1, cour_id);
ResultSet rs1=pst1.executeQuery();

while(rs1.next()) {
credits=rs1.getInt(1);
}

String query2="select total_credits from stud where


stud_id=?";
PreparedStatement pst2=con.prepareStatement(query2);
pst2.setString(1, stud_id);
ResultSet rs2=pst2.executeQuery();

while(rs2.next()) {
total=rs2.getInt(1);

}
System.out.println(" OLD CREDITS : "+ total);

total=total+credits;
System.out.println(" NEW CREDITS : " + total);

String query4="update stud set total_credits=? where


stud_id=?";
PreparedStatement pst4=con.prepareStatement(query4);
pst4.setInt(1, total);
pst4.setString(2, stud_id);
int res2=pst4.executeUpdate();
System.out.println(" ROWS AFFECTED IN STUD TABLE : "+res2);

}
System.out.println(" ROWS AFFECTED IN TAKES TABLE : "+res);

con.close();

myObj.close();
Before Updation in SQL:
After Updation In SQL:

You might also like