create a doctor table with fields doc_no, doc_name, address, city, area.write a menu driven java program to perform the following operation on Doctor Table |EasyCoding45
create a doctor table with fields doc_no, doc_name, address, city, area.write a menu driven java program to perform the following operation on Doctor Table.
1.insert
2.Delete
3.View All
4.Exit
In this tutorial we are going to learn how we can Write a java program to create a doctor table with fields doc_no, doc_name, address, city, area.and also write a menu driven java program to perform the following operation on Doctor Table.
Program :-
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.*;
class doctor
{
public static void main(String a[])throws Exception
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con;
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/anand?useSSL=false&serverTimezone=Asia/Kolkata","root","Admin@123");
Statement st;
st=con.createStatement();
PreparedStatement ps;
ResultSet rs;
int c;
Scanner sc=new Scanner(System.in);
do
{
System.out.println("\n1:Insert\n2:Delete\n3:View All\n4:Exit");
System.out.println("Enter Your Choice :");
c=sc.nextInt();
switch(c)
{
case 1:
ps=con.prepareStatement("insert into doctor(doc_no,doc_name,address,city,area)values(?,?,?,?,?)");
System.out.println("Enter Doctor No :");
int dno=sc.nextInt();
System.out.println("Enter Doctor Name :");
String dname=sc.next();
System.out.println("Enter Doctor Address :");
String addr=sc.next();
System.out.println("Enter Doctor City :");
String city=sc.next();
System.out.println("Enter Doctor Area :");
String area=sc.next();
ps.setInt(1,dno);
ps.setString(2,dname);
ps.setString(3,addr);
ps.setString(4,city);
ps.setString(5,area);
int ans=ps.executeUpdate();
if(ans>0)
System.out.println("-----------Record inserted :-----------");
else
System.out.println("-----------Record not Inserted :----------");
break;
case 2:
ps=con.prepareStatement("delete from doctor where doc_no=?");
System.out.println("Enter Doctor No :");
int d=sc.nextInt();
ps.setInt(1,d);
int del=ps.executeUpdate();
if(del>0)
System.out.println("-----------Record deleted :--------------");
else
System.out.println("-----------Record not deleted :-----------");
break;
case 3:
rs=st.executeQuery("select * from doctor ");
int cnt=0;
while(rs.next())
{
cnt++; System.out.println("\n"+rs.getInt(1)+"||"+rs.getString(2)+"||"+rs.getString(3)+"||"+rs.getString(4)+"||"+rs.getString(5));
}
if(cnt==0)
System.out.println("-------No Records ---------");
break;
case 4:
System.exit(0);
break;
}
}while(c!=4);
}
}
Comments
Post a Comment