Program 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

package jdbc1;

import java.sql. *;
import java.util.*;
import java.io. *;
import java.io.PrintWriter;
public class PROGRAM5
{
public static void main(String[] args) throws Exception
{
System.out.println("Enter your choice:");
DataInputStream br=new DataInputStream(System.in);
int v_total=0;
float v_per=0;
while(true)
{
System.out.println("1. New Student Entry \n 2. Calculate student result \n 3.
Display specified student’s formatted Mark sheet \n 4. Delete Student Entry \n
5.Modify the Student info \n 6.Exit");
int x=Integer.parseInt(br.readLine());
if(x==1)
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/emp","root","root");
System.out.println("Inserting records");
PreparedStatement ps=con.prepareStatement("insert into student
values(?,?,?,?,?,?,?)");
System.out.println("Enter student number : ");
int v_stdno=Integer.parseInt(br.readLine());
System.out.println("Enter student name : ");
String v_stdname=br.readLine();
System.out.println("Enter marks of subject 1 : ");
int v_s1=Integer.parseInt(br.readLine());
System.out.println("Enter marks of subject 2: ");
int v_s2=Integer.parseInt(br.readLine());
System.out.println("Enter marks of subject 3 : ");
int v_s3=Integer.parseInt(br.readLine());

ps.setInt(1,v_stdno); ps.setString(2,v_stdname);
ps.setInt(3,v_s1); ps.setInt(4,v_s2);
ps.setInt(5,v_s3); ps.setInt(6,v_total);
ps.setFloat(7,v_per);
int count=ps.executeUpdate();
System.out.println("Records affected "+count);
con.close();
}
else if(x==2)
{

Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/emp","root","root");
ResultSet rs=null;
v_total=0;
int v_stdno=0;
v_per=0;
System.out.println("Enter student number : ");
v_stdno=Integer.parseInt(br.readLine());
Statement st=con.createStatement();
rs=st.executeQuery("select * from student");
while(rs.next())
{
if(v_stdno==rs.getInt(1))
v_total=rs.getInt(3)+ rs.getInt(4)+rs.getInt(5);
}
v_per=v_total/3;
System.out.println("The total of specified student is "+v_total);
System.out.println("The percentage of specified student is "+v_per);
PreparedStatement ps=con.prepareStatement(" update student set
total=?,percentage=? where std_ID=?");
ps.setInt(1,v_total);
ps.setFloat(2,v_per);
ps.setInt(3,v_stdno);
ps.executeUpdate();
System.out.println("Record is update with total and percentage");
con.close();
}
else if(x==3)
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/emp","root","root");
ResultSet rs=null;
int v_stdno=0,v_sno=0,v_s1=0,v_s2=0,v_s3=0;
v_total=0;
String v_sname="";
v_per=0;
System.out.println("Enter student number : ");
v_stdno=Integer.parseInt(br.readLine());
Statement st=con.createStatement();
rs=st.executeQuery("select * from student");
while(rs.next())
{
if(v_stdno==rs.getInt(1))
{
v_sno=rs.getInt(1);
v_sname=rs.getString(2);
v_s1=rs.getInt(3);
v_s2=rs.getInt(4);
v_s3=rs.getInt(5);
v_total=rs.getInt(6);
v_per=rs.getFloat(7);

System.out.println("**************************************************");
System.out.println("S.NO\tS.NAME\tSUB1\tSUB2\tSUB3\tTOTAL\tPERCENTAGE");
System.out.println(""+v_sno+"\t"+v_sname+"\t"+v_s1+"\t"+v_s2+"\t"+v_s3+"\t"+v_to
tal+"\t"+v_per+"");

System.out.println("**************************************************");
}
}
con.close();
}
else if(x==4)
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/emp","root","root");
PreparedStatement ps=con.prepareStatement("delete from student where
std_no=?");
System.out.println("Enter student number :");
int v_stdno=0;
v_stdno=Integer.parseInt(br.readLine());
ps.setInt(1,v_stdno);
ps.executeUpdate();
System.out.println("Student data deleted ");
}
else if(x==5)
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/emp","root","root");
System.out.println("Enter student number to modify : ");
int v_stdno=Integer.parseInt(br.readLine());

System.out.println("Enter new student number : ");


int n_stdno=Integer.parseInt(br.readLine());
System.out.println("Enter new student name : ");
String v_stdname=br.readLine();
System.out.println("Enter marks of subject 1 : ");
int v_s1=Integer.parseInt(br.readLine());
System.out.println("Enter marks of subject 2: ");
int v_s2=Integer.parseInt(br.readLine());
System.out.println("Enter marks of subject 3 : ");
int v_s3=Integer.parseInt(br.readLine());
v_total=v_s1+v_s2+v_s3;
v_per=v_total/3;
PreparedStatement ps=con.prepareStatement(" update student set
std_no=?,std_name=?,std_sub1=?,std_sub2=?,std_sub3=?,total=?,percentage=? where
std_no=?");
ps.setInt(1,n_stdno);
ps.setString(2,v_stdname); ps.setInt(3,v_s1);
ps.setInt(4,v_s2);
ps.setInt(5,v_s3);
ps.setInt(6,v_total);
ps.setFloat(7,v_per);
ps.setInt(8,v_stdno);
ps.executeUpdate();
}
else if(x==6)
{ break; }
else
{ }
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy