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

Assignment 7

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)
18 views

Assignment 7

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/ 5

Assignment 7

Student Name: Maitreyee Kaushik Shendye


USN Number: UCE2023460
Division: A
Batch: A3
Problem Statement: Design a database system and implement
MySQL Java Connectivity (2 tier) for given table. Consider employee relation:
emp(empno,ename,job,salary,commission,date_of_joining,dept_no)
Implement the following menu. MENU
1. Insert
2. Display
3. Update
4. Delete

Code:
package assignment;
import java.util.*;
import java.sql.*;
import java.sql.Date;
public class EmployeeManagement {
private static final String url =
"jdbc:mysql://localhost:3306/company_assignment7";
private static final String username = "root";
private static final String password = "Princess0404";
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}
catch(ClassNotFoundException e) {
System.out.println("Error: class Not found");
return;
}
int choice=0,employee_id,department_number,salary,commission;
String employee_name,job,dateOfJoining;
do {
System.out.println("Enter \n1: Insert\n2: Display\n3: Update\n4: Delete");
choice = sc.nextInt();
switch(choice) {
case 1:{
try {
String query;
Connection connection = DriverManager.getConnection(url,username,password);
System.out.print("Enter employee id: ");
employee_id = sc.nextInt();
System.out.print("Enter employee name: ");
employee_name = sc.next();
System.out.print("Enter job: ");
job = sc.next();
System.out.print("Enter salary: ");
salary = sc.nextInt();
System.out.print("Enter commission: ");
commission = sc.nextInt();
System.out.print("Enter date_of_joining (YYYY-MM-DD): ");
dateOfJoining = sc.next();
System.out.print("Enter dept_no: ");
department_number = sc.nextInt();
query = "INSERT INTO employee VALUES (?,?,?,?,?,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, employee_id);
preparedStatement.setString(2, employee_name);
preparedStatement.setString(3, job);
preparedStatement.setDouble(4, salary);
preparedStatement.setDouble(5, commission);
preparedStatement.setDate(6, Date.valueOf(dateOfJoining));
preparedStatement.setInt(7, department_number);
int rowsInserted = preparedStatement.executeUpdate();
if (rowsInserted > 0) {
System.out.println("Data inserted successfully!");
}
else {
System.out.println("Insertion failed.");
}
preparedStatement.close();
connection.close();
}
catch(SQLException e) {
e.printStackTrace();
}
break;
}
case 2:
{
try {
Date date;
Connection connection = DriverManager.getConnection(url, username,
password);
Statement statement = connection.createStatement();
ResultSet resultset = statement.executeQuery("SELECT * FROM employee");
while(resultset.next()) {
employee_id = resultset.getInt(1);
employee_name = resultset.getString(2);
job = resultset.getString(3);
salary = resultset.getInt(4);
commission = resultset.getInt(5);
date = resultset.getDate(6);
department_number = resultset.getInt(7);
System.out.println(employee_id+" "+employee_name+" "+job+" "+salary+"
"+commission+" "+date+" "+department_number);
}
statement.close();
connection.close();
}
catch(SQLException e) {
e.getMessage();
}
break;
}
case 3:{
try {
int search,update_value1=0,opt;
String field,update_value2=null,query=null;
Connection connection = DriverManager.getConnection(url, username,
password);
Statement statement = connection.createStatement();
System.out.println("Enter employee id of which data is to be updated");
search=sc.nextInt();
System.out.println("Enter field \n1: employee_name\n2: job\n3: salary\n4:
commission\n5: department number");
opt = sc.nextInt();
if (opt==1) {
System.out.println("Enter updated name");
update_value2 = sc.next();
query = "UPDATE employee SET employee_name = ? WHERE employee_number = ?";
}
else if(opt==2) {
System.out.println("Enter updated job title");
update_value2 = sc.next();
query = "UPDATE employee SET job = ? WHERE employee_number = ?";
}
else if(opt==3) {
System.out.println("Enter updated salary");
update_value1 = sc.nextInt();
query = "UPDATE employee SET salary = ? WHERE employee_number = ?";
}
else if(opt==4)
{
System.out.println("Enter updated commission");
update_value1 = sc.nextInt();
query = "UPDATE employee SET commission = ? WHERE employee_number = ?";
}
else if(opt==5)
{
System.out.println("Enter updated department number");
update_value1 = sc.nextInt();
query = "UPDATE employee SET dept_no = ? WHERE employee_number = ?";
}
PreparedStatement preparedstatement = connection.prepareStatement(query);
if(opt==1||opt==2) {
preparedstatement.setString(1, update_value2);
}
else {
preparedstatement.setInt(1, update_value1);
}
preparedstatement.setInt(2, search);
int rowsInserted = preparedstatement.executeUpdate();
if (rowsInserted > 0) {
System.out.println("Data updated successfully!");
}
else {
System.out.println("Updation failed.");
}
preparedstatement.close();
connection.close();
}
catch(SQLException e) {
e.getMessage();
}
break;
}
case 4:
{
try {
int search;
String query=null;
Connection connection = DriverManager.getConnection(url, username,
password);
System.out.println("Enter employee id of which data is to be deleted");
search=sc.nextInt();
query = "DELETE FROM employee WHERE employee_number=?";
PreparedStatement preparedstatement = connection.prepareStatement(query);
preparedstatement.setInt(1, search);
preparedstatement.executeUpdate();
preparedstatement.close();
connection.close();
}
catch(SQLException e) {
e.getMessage();
}
break;
}
}
}while(choice!=5);
}
}

Output:
Enter
1: Insert
2: Display
3: Update
4: Delete
3
Enter employee id of which data is to be updated
3
Enter field
1: employee_name
2: job
3: salary
4: commission
5: department number
5
Enter updated department number
3
Data updated successfully!

Enter
1: Insert
2: Display
3: Update
4: Delete
2
1 Minnie Coder 9000000 100000 2005-02-28 3
2 Pranali Manager 78000000 789998990 2007-12-04 5
3 Sunil Manager 784453629 192748294 2005-04-04 3

You might also like