├── employeeDAO ├── databaceconnect - sql .java ├── model.java ├── client.java └── employeedaoimple.java /employeeDAO: -------------------------------------------------------------------------------- 1 | package dao; 2 | import java.util.List; 3 | 4 | import model.Employee; 5 | 6 | 7 | public interface EmployeeDAO { 8 | void createEmployee(Employee employee); 9 | Employee getEmploeeById(int employeeId); 10 | void updateEmployeeEmailById(String newEmail,int employeeId); 11 | void deleteEmployeeById(int employeeId); 12 | List getAllEmployeesInfo(); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /databaceconnect - sql .java: -------------------------------------------------------------------------------- 1 | package com.util; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.SQLException; 6 | 7 | public class Dbconnection { 8 | public static Connection connectDb() { 9 | Connection con=null; 10 | try { 11 | Class.forName("com.mysql.cj.jdbc.Driver"); 12 | con = DriverManager.getConnection("jdbc:mysql://localhost:3306/enterdatabasename","username","password"); 13 | } 14 | catch(ClassNotFoundException c) { 15 | System.out.println(c); 16 | } 17 | catch(SQLException e) { 18 | System.out.println(e); 19 | } 20 | return con; 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /model.java: -------------------------------------------------------------------------------- 1 | package model; 2 | 3 | import java.sql.Date; 4 | 5 | public class Employee { 6 | private int emp_id; 7 | private String emp_name; 8 | private String email; 9 | private Double salary; 10 | private String doj ; 11 | private int bonous; 12 | public int getEmp_id() { 13 | return emp_id; 14 | } 15 | public void setEmp_id(int emp_id) { 16 | this.emp_id = emp_id; 17 | } 18 | public String getEmp_name() { 19 | return emp_name; 20 | } 21 | public void setEmp_name(String emp_name) { 22 | this.emp_name = emp_name; 23 | } 24 | public String getEmail() { 25 | return email; 26 | } 27 | public void setEmail(String email) { 28 | this.email = email; 29 | } 30 | public Double getSalary() { 31 | return salary; 32 | } 33 | public void setSalary(Double salary) { 34 | this.salary = salary; 35 | } 36 | public String getDoj() { 37 | return doj; 38 | } 39 | public void setDoj(String doj) { 40 | this.doj= doj; 41 | } 42 | public int getBonous() { 43 | return bonous; 44 | } 45 | public void setBonous(int bonous) { 46 | this.bonous = bonous; 47 | } 48 | @Override 49 | public String toString() { 50 | return "Employee [emp_id=" + emp_id + ", emp_name=" + emp_name + ", email=" + email + ", salary=" + salary 51 | + ", bonous=" + bonous + ", getEmp_id()=" + getEmp_id() + ", getEmp_name()=" + getEmp_name() 52 | + ", getEmail()=" + getEmail() + ", getSalary()=" + getSalary() + ", getBonous()=" + getBonous() 53 | + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() 54 | + "]"; 55 | } 56 | public Employee( String emp_name, String email, Double salary, String doj, int bonous) { 57 | super(); 58 | 59 | this.emp_name = emp_name; 60 | this.email = email; 61 | this.salary = salary; 62 | this.doj = doj; 63 | this.bonous = bonous; 64 | } 65 | public Employee() { 66 | 67 | } 68 | 69 | 70 | } 71 | -------------------------------------------------------------------------------- /client.java: -------------------------------------------------------------------------------- 1 | package Client; 2 | 3 | 4 | import java.sql.Connection; 5 | import java.util.List; 6 | import java.util.Scanner; 7 | 8 | import com.util.Dbconnection; 9 | 10 | import dao.EmployeeDAO; 11 | import daoImpl.EmployeeDAOImpl; 12 | import model.Employee; 13 | public class Clienttest { 14 | 15 | public static void main(String[] args) { 16 | 17 | System.out.println("Welcome....................."); 18 | Scanner s = new Scanner(System.in); 19 | EmployeeDAOImpl employeeDAO = new EmployeeDAOImpl(); 20 | System.out.println("1.insert 2.update 3.delete 4.get one employee 5 getemployee list"); 21 | 22 | int c1=1; 23 | while(c1!=0) { 24 | System.out.println("enter the choice"); 25 | int c= s.nextInt(); 26 | switch(c) { 27 | case 1: 28 | System.out.println("insertion"); 29 | System.out.println("enter name"); 30 | String emp_name=s.nextLine(); 31 | s.nextLine(); 32 | System.out.println("enter id"); 33 | String email =s.nextLine(); 34 | System.out.println("enter salary"); 35 | Double salary=s.nextDouble(); 36 | s.nextLine(); 37 | System.out.println("enter doj"); 38 | String doj=s.nextLine(); 39 | System.out.println("enter bonous"); 40 | int bonus=s.nextInt(); 41 | Employee e = new Employee( emp_name, email, salary, doj, bonus); 42 | employeeDAO.createEmployee(e); 43 | 44 | break; 45 | 46 | case 2: 47 | System.out.println("update"); 48 | System.out.println("enter id to change"); 49 | int employeeId = s.nextInt(); 50 | System.out.println("enter the new email id"); 51 | String newEmail = s.nextLine(); 52 | employeeDAO.updateEmployeeEmailById(newEmail,employeeId); 53 | break; 54 | case 3: 55 | System.out.println("delete"); 56 | System.out.println("enter the condition to delete"); 57 | int employeeId1 = s.nextInt(); 58 | employeeDAO.deleteEmployeeById( employeeId1); 59 | 60 | case 4: 61 | System.out.println("get on employee detail"); 62 | System.out.println("enter the id to display"); 63 | int emp_id = s.nextInt(); 64 | System.out.println( employeeDAO.getEmploeeById(emp_id)); 65 | break; 66 | 67 | case 5: 68 | System.out.println("get on employees detail"); 69 | List empList = employeeDAO.getAllEmployeesInfo(); 70 | for(Employee emp1 : empList) 71 | { 72 | System.out.println(emp1); 73 | } 74 | 75 | break; 76 | 77 | } 78 | System.out.println("enter the 1 when you want to edit it or diplay it (or) enter 0 to exit"); 79 | c1= s.nextInt(); 80 | 81 | } 82 | } 83 | } 84 | 85 | -------------------------------------------------------------------------------- /employeedaoimple.java: -------------------------------------------------------------------------------- 1 | package daoImpl; 2 | 3 | import java.sql.Connection; 4 | import java.sql.Date; 5 | import java.sql.PreparedStatement; 6 | import java.sql.ResultSet; 7 | import java.sql.SQLException; 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | import com.util.Dbconnection; 12 | 13 | import dao.EmployeeDAO; 14 | import model.Employee; 15 | 16 | public class EmployeeDAOImpl implements EmployeeDAO { 17 | public void createEmployee(Employee employee){ 18 | Connection con = Dbconnection.connectDb(); 19 | String sql = "insert into employees(emp_name,email,salary,doj,bonous)values(?,?,?,?,?)"; 20 | try { 21 | PreparedStatement pst = con.prepareStatement(sql); 22 | pst.setString(1, employee.getEmp_name()); 23 | pst.setString(2, employee.getEmail()); 24 | pst.setDouble(3, employee.getSalary()); 25 | pst.setString(4,employee.getDoj()); 26 | pst.setInt(5, employee.getBonous()); 27 | int executeUpdate = pst.executeUpdate(); 28 | if(executeUpdate == 1) { 29 | System.out.println("created"); 30 | } 31 | 32 | } 33 | catch(SQLException e) { 34 | System.out.println(e); 35 | } 36 | } 37 | 38 | public Employee getEmploeeById(int employeeId){ 39 | Employee emp = new Employee(); 40 | String SQL = "SELECT * FROM employees WHERE emp_id=?"; 41 | try{ 42 | Connection connection = Dbconnection.connectDb(); 43 | 44 | PreparedStatement pst = connection.prepareStatement(SQL); 45 | 46 | pst.setInt(1, employeeId); 47 | ResultSet rs = pst.executeQuery(); 48 | while(rs.next()) { 49 | int empid = rs.getInt("emp_id"); 50 | String ename = rs.getString("emp_name"); 51 | String email = rs.getString("emp_name"); 52 | Double salary = rs.getDouble("salary"); 53 | int bonous = rs.getInt("bonous"); 54 | String doj = rs.getString("doj"); 55 | 56 | emp.setEmp_id(empid); 57 | emp.setEmp_name(ename); 58 | emp.setEmail(email); 59 | emp.setSalary(salary); 60 | emp.setBonous(bonous); 61 | emp.setDoj(doj); 62 | } 63 | } catch (Exception e) { 64 | System.out.println(e); 65 | } 66 | 67 | return emp; 68 | } 69 | public void updateEmployeeEmailById(String newEmail,int employeeId){ 70 | Connection con = Dbconnection.connectDb(); 71 | String sql="update employees set email=? where emp_id=?"; 72 | try { 73 | PreparedStatement pst = con.prepareStatement(sql); 74 | pst.setString(1,newEmail); 75 | pst.setInt(2, employeeId); 76 | int ans = pst.executeUpdate(); 77 | if(ans==1) 78 | System.out.println("update"); 79 | } 80 | catch(SQLException e){ 81 | System.out.println(e); 82 | } 83 | 84 | 85 | } 86 | public void deleteEmployeeById(int employeeId){ 87 | Connection con = Dbconnection.connectDb(); 88 | String sql="delete from employees where emp_id=?"; 89 | 90 | try { 91 | PreparedStatement pst = con.prepareStatement(sql); 92 | pst.setInt(1, employeeId); 93 | int ans = pst.executeUpdate(); 94 | if(ans==1) 95 | System.out.println("delete"); 96 | } 97 | catch(SQLException e){ 98 | System.out.println(e); 99 | } 100 | 101 | } 102 | public List getAllEmployeesInfo(){ 103 | List empList = new ArrayList<>(); 104 | 105 | String SQL = "SELECT *FROM employees"; 106 | try { 107 | Connection connection = Dbconnection.connectDb(); 108 | PreparedStatement ps = connection.prepareStatement(SQL); 109 | 110 | 111 | ResultSet rs = ps.executeQuery(); 112 | while (rs.next()) { 113 | Employee employee = new Employee(); 114 | int empId = rs.getInt("emp_id"); 115 | String eName = rs.getString("emp_name"); 116 | String email = rs.getString("email"); 117 | Double salary = rs.getDouble("salary"); 118 | int bonous = rs.getInt("bonous"); 119 | String date = rs.getString("doj"); 120 | 121 | 122 | employee.setEmp_name(eName); 123 | 124 | employee.setBonous(bonous); 125 | employee.setDoj(date); 126 | employee.setEmail(email); 127 | employee.setEmp_id(empId); 128 | employee.setSalary(salary); 129 | 130 | empList.add(employee); 131 | } 132 | 133 | } catch (Exception e) { 134 | System.out.println(e); 135 | } 136 | return empList; 137 | 138 | 139 | } 140 | 141 | } 142 | --------------------------------------------------------------------------------