import java.util.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; class methods { void create() { try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root"); Scanner sc = new Scanner(System.in); System.out.print("Enter Id : "); int id = sc.nextInt(); System.out.print("Enter name : "); String name = sc.next(); System.out.print("Enter age : "); int age = sc.nextInt(); System.out.print("Enter salary : "); int salary = sc.nextInt(); System.out.print("Enter desig : "); String desig = sc.next(); PreparedStatement ps = con.prepareStatement("insert into employee value (?,?,?,?,?)"); ps.setInt(1, id); ps.setString(2, name); ps.setInt(3, age); ps.setInt(4, salary); ps.setString(5, desig); ps.execute(); System.out.println("Data Inserted successfuly"); } catch (Exception e) { System.out.println(e); } } void Display() { try { Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root"); String sql = "SELECT * FROM employee"; try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { ResultSet resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); int age = resultSet.getInt("age"); int salary = resultSet.getInt("salary"); String designation = resultSet.getString("designation"); System.out.println("ID: " + id); System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Salary: " + salary); System.out.println("Designation: " + designation); System.out.println(); // Add a blank line between entries } } connection.close(); } catch (SQLException e) { e.printStackTrace(); } } void Update() { try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root"); Scanner sc = new Scanner(System.in); System.out.print("Enter Id : "); int id = sc.nextInt(); System.out.print("Enter salary : "); int salary = sc.nextInt(); PreparedStatement ps = con.prepareStatement("update employee set salary=? where id=?"); ps.setInt(1, salary); ps.setInt(2, id); ps.execute(); System.out.println("Data Updated successfuly"); } catch (Exception e) { System.out.println(e); } } void Delete() { try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root"); Scanner sc = new Scanner(System.in); System.out.print("Enter Id : "); int id = sc.nextInt(); PreparedStatement ps = con.prepareStatement("delete from employee where id=?"); System.out.println("do you want to delete ? y/n"); String op = sc.next(); if (op.equalsIgnoreCase("y")) { ps.setInt(1, id); ps.execute(); System.out.println("Data deleted successfuly"); } else { System.out.println("Thank you...!"); } } catch (Exception e) { System.out.println(e); } } } public class Project { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (true) { System.out.println("======================================="); System.out.println("Choose an Option:"); System.out.println("1. Create"); System.out.println("2. Display"); System.out.println("3. Update"); System.out.println("4. Delete"); System.out.println("5. Exit"); System.out.println("======================================="); int roleChoice = scanner.nextInt(); methods m1=new methods(); switch (roleChoice) { case 1: m1.create(); break; case 2: m1.Display(); break; case 3: m1.Update(); break; case 4: m1.Delete(); break; case 5: return; default: System.out.println("Invalid choice."); } } } }