0% found this document useful (0 votes)
15 views2 pages

DBMS Pages 1 Merged

This document provides a solution to implement database connectivity and navigation operations using Java and MySQL. It includes: 1) Importing necessary Java packages and classes for database connectivity like JDBC, SQL, and ResultSet. 2) Establishing a connection to a MySQL database called "insurance" and creating a prepared statement. 3) Implementing database operations like insert, update, delete, and display through a menu driven program using switch case. Validating the operations and closing all open resources.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

DBMS Pages 1 Merged

This document provides a solution to implement database connectivity and navigation operations using Java and MySQL. It includes: 1) Importing necessary Java packages and classes for database connectivity like JDBC, SQL, and ResultSet. 2) Establishing a connection to a MySQL database called "insurance" and creating a prepared statement. 3) Implementing database operations like insert, update, delete, and display through a menu driven program using switch case. Validating the operations and closing all open resources.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

❖ Database Connectivity: Write a program to implement MySQL/Oracle database

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
{

}
}
}

You might also like