DBMS Pages 1 Merged
DBMS Pages 1 Merged
connectivity with any front end language to implement Database navigation operations
(add, delete, edit etc.)
SOLUTION:
import java.sql.*;
import java.util.*;
class Jdbcdemo
{
public static void main(String args[])
{
try
{
Scanner obj=new Scanner(System.in);
int ch;
Class.forName("com.mysql.cj.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/insurance"
,"user1","user1");
do
{
System.out.println("1.Insert"+"\n"+"2.Update"+"\n"+"3.Delete"+"\
n"+"4.Display"+"\n"+"5.Exit");
System.out.println("enter choice:");
ch=obj.nextInt();
switch(ch)
{
case 1:
int did;
String pname;
String padd;
System.out.println("enter driverid:");
did=obj.nextInt();
System.out.println("enter name:");
pname=obj.next();
System.out.println("enter address:");
padd=obj.next();
String sql="insert into person
values('"+did+"','"+pname+"','"+padd+"')";
PreparedStatement
pstmt=con.prepareStatement(sql);
/*pstmt.setInt(1, did);
pstmt.setString(2, pname);
pstmt.setString(3, padd);*/
pstmt.executeUpdate();
System.out.println("Data Inserted
Successfully");
pstmt.close();
break;
case 2:
int drid;
String cadd;
System.out.println("enter the driverid of
a person:");
drid=obj.nextInt();
System.out.println("enter the new
address:");
cadd=obj.next();
String sql1="update person set
address='"+cadd+"' where driverid='"+drid+"'";
pstmt=con.prepareStatement(sql1);
pstmt.executeUpdate();
System.out.println("Data Updated
Successfully");
break;
case 3:
int driid;
System.out.println("enter the driverid of
a person to be deleted");
driid=obj.nextInt();
String sql2="delete from person where
driverid='"+driid+"'";
pstmt=con.prepareStatement(sql2);
pstmt.executeUpdate();
System.out.println("Data Deleted
Successfully");
break;
case 4:
Statement stmt=con.createStatement();
String sql3="select * from person";
ResultSet rs=stmt.executeQuery(sql3);
while(rs.next())
System.out.println(rs.getInt(1)+"
"+rs.getString(2)+" "+rs.getString(3));
break;
case 5:
System.exit(0);
break;
}
}while(ch!=5);
con.close();
obj.close();
}
catch(Exception e)
{
}
finally
{
}
}
}