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

Name: Vaibhav Dobe Roll No:13144

The document contains Java code for a student database program that allows the user to insert, update, delete and display student records from a MySQL database table. The program uses a menu-driven interface to allow the user to choose an operation and then prompts for input values. Based on the operation selected, it executes the appropriate SQL query to manipulate the student table.

Uploaded by

Shubham Sagar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Name: Vaibhav Dobe Roll No:13144

The document contains Java code for a student database program that allows the user to insert, update, delete and display student records from a MySQL database table. The program uses a menu-driven interface to allow the user to choose an operation and then prompts for input values. Based on the operation selected, it executes the appropriate SQL query to manipulate the student table.

Uploaded by

Shubham Sagar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Vaibhav Dobe

Roll No:13144
import java.io.InputStreamReader;
import java.sql.*;
import java.util.*;
import java.io.*;
public class VaibhavDB
{

public static void main(String arg[])


{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db","Vaibhav","
vaibh23");
PreparedStatement stm=null;
Statement stmt=conn.createStatement();
int ch;
Scanner sc=new Scanner(System.in);
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));

do{
int r;
String n = null;
System.out.println("Enter Your choice \n1 Insert\n2
Update\n3 Delete\n4 Display\n5 Exit");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter Roll No");
r=Integer.parseInt(br.readLine());
System.out.println("Enter Name");
n=br.readLine();

String sql = "insert into student "+ "


(roll,name)" + " values (?, ?)";
stm = conn.prepareStatement(sql);

// set param values


stm.setInt(1, r);
stm.setString(2, n);

// 3. Execute SQL query


stm.executeUpdate();

break;
case 2:
System.out.println("Enter Roll No to
update");
r=Integer.parseInt(br.readLine());
System.out.println("Enter Name");
n=br.readLine();
String query = "update student set
name=? where roll=? ";
stm = conn.prepareStatement(query);
Name: Vaibhav Dobe
Roll No:13144
stm.setString(1, n);
stm.setInt(2, r);
stm.executeUpdate();
System.out.println("Record updated");
break;
case 3:
System.out.println("Enter Roll No to
update");
r=Integer.parseInt(br.readLine());

String sql1="DELETE FROM student WHERE


roll=?";
stm= conn.prepareStatement(sql1);
stm.setInt(1, r);
stm.execute();
System.out.println("Record Deleted");

break;

case 4:

String qr= "SELECT * FROM student ORDER


BY roll";
ResultSet rs=stmt.executeQuery(qr);
while(rs.next())
{
r=rs.getInt("roll");
n=rs.getString("name");
System.out.println("Roll No:"+r+
"\tName:"+n);
}
break;

case 5:System.out.println("Thank You");

break;
default:System.out.println("Invalid Choice");

}while(ch!=5);
stmt.close();
conn.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT

Enter Your choice


1 Insert
2 Update
3 Delete
Name: Vaibhav Dobe
Roll No:13144
4 Display
5 Exit

1
Enter Roll No
1
Enter Name
Vaibhav
Enter Your choice
1 Insert
2 Update
3 Delete
4 Display
5 Exit
1
Enter Roll No
2
Enter Name
Amey
Enter Your choice
1 Insert
2 Update
3 Delete
4 Display
5 Exit
1
Enter Roll No
3
Enter Name
Hemant
Enter Your choice
1 Insert
2 Update
3 Delete
4 Display
5 Exit
4
Roll No:1 Name:Vaibhav
Roll No:2 Name:Amey
Roll No:3 Name:Hemant
Enter Your choice
1 Insert
2 Update
3 Delete
4 Display
5 Exit
2
Enter Roll No to update
3
Enter Name
Priyanka
Record updated
Enter Your choice
1 Insert
2 Update
3 Delete
4 Display
Name: Vaibhav Dobe
Roll No:13144
5 Exit
4
Roll No:1 Name:Vaibhav
Roll No:2 Name:Amey
Roll No:3 Name:Priyanka

Enter Your choice


1 Insert
2 Update
3 Delete
4 Display
5 Exit
3
Enter Roll No to update
3
Record Deleted
Enter Your choice
1 Insert
2 Update
3 Delete
4 Display
5 Exit
4
Roll No:1 Name:Vaibhav
Roll No:2 Name:Amey
Enter Your choice
1 Insert
2 Update
3 Delete
4 Display
5 Exit
5
Thank You

You might also like