mysql + javaSE -- 员工管理系统3.0

废话不多说,上代码
如果有需要源代码,可以到本人的上传资源下载,一切免费。
employeesystem3.0

main包

Main类

package com.situ.main;

import java.util.Scanner;

/**
 * @author : 一只小海猪
 * @date : 16:23 2021/11/12
 *  缺陷:1.当部门下没人的时候,更新部门人数可能会出错!
 *         2.暂时还没解决查询出的空信息,当查询员工或部门时,可能不报错,也不输出,为空字符串
 *          3.暂时没有将同一个部门员工放到同一个部门下,暂时在员工表内检索
 */
public class Main {
    public static void main(String[] args) {
        DepartmentMain dm = new DepartmentMain();
        EmployeeMain em = new EmployeeMain();
        while(true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("============================");
            System.out.println("==1.员工系统 2.部门系统 3.退出==");
            System.out.println("============================");
            System.out.println("请选择系统代号:");
                String choice = sc.next();
                if("1".equals(choice)){
                    em.Employeemain();
                    break;
                }else if ("2".equals(choice)){
                    dm.Departmentmain();
                    break;
                }else if ("3".equals(choice)){
                    System.out.println("谢谢使用!");
                    break;
                }else{
                    System.out.println("输入的代号错误,请重新输入!");
                }
        }
    }

}

EmployeeMain类

package com.situ.main;

import com.situ.controller.EmployeeController;

/**
 * @author : 一只小海猪
 * @date : 15:51 2021/11/12
 */
public class EmployeeMain {
    public void Employeemain(){
        EmployeeController c = new EmployeeController();
        c.start();
    }
}

DepartmentMain类

package com.situ.main;

import com.situ.controller.DepartmentController;

/**
 * @author : 一只小海猪
 * @date : 16:23 2021/11/12
 */
public class DepartmentMain {
    public void Departmentmain() {
        DepartmentController dc = new DepartmentController();
        dc.start();
    }
}

model包

Employee类

package com.situ.model;

import java.util.Date;

/**
 * @author : 一只小海猪
 * @date : 17:17 2021/11/12
 */
public class Employee {
    private int id;
    private String name;
    private String sex;
    private String birthday;
    private String hiredate;
    private int department_id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getHiredate() {
        return hiredate;
    }

    public void setHiredate(String hiredate) {
        this.hiredate = hiredate;
    }

    public int getDepartment_id() {
        return department_id;
    }

    public void setDepartment_id(int department_id) {
        this.department_id = department_id;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", birthday=" + birthday +
                ", hiredate=" + hiredate +
                ", department_id=" + department_id +
                '}';
    }
}

Department类

package com.situ.model;

/**
 * @author : 一只小海猪
 * @date : 12:02 2021/11/14
 */
public class Department {
    private int id;
    private String name;
    private int countemployee;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCountemployee() {
        return countemployee;
    }

    public void setCountemployee(int countemployee) {
        this.countemployee = countemployee;
    }

    @Override
    public String toString() {
        return "Department{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", countemployee=" + countemployee +
                '}';
    }
}

Count类

package com.situ.model;

/**
 * @author : 一只小海猪
 * @date : 15:42 2021/11/14
 */
public class Count {
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

controller包

EmployeeController类

package com.situ.controller;

import com.situ.dao.EmployeeDao;
import com.situ.model.Employee;
import com.situ.view.EmployeeView;

/**
 * @author : 一只小海猪
 * @date : 15:53 2021/11/12
 */
public class EmployeeController {
    //创建私有对象,方便控制使用各种类内的方法
    private EmployeeView view = new EmployeeView();
    private EmployeeDao dao = new EmployeeDao();

    public void start() {
        while (true) {
            int choice = view.showMenu();
            if (choice == 1) {
                view.showfindMenu();
            } else if (choice == 2) {
                view.showeditMenu();
            } else if (choice == 3) {
                view.showdeleteMenu();
            } else if (choice == 4) {
                view.showaddMenu();
            } else if (choice == 5) {
                System.out.println("谢谢使用!");
                break;
            }else{
                System.out.println("操作符不正确!请重新输入!");
            }
        }
    }

    public void findAll() {
        view.showAll(dao.queryAll());
    }

    public void findAsId(int id) {
        view.showAsId(dao.queryAsId(id));
    }

    public void addemployee(Employee e) {
        view.showAddEmployee(dao.addEmployee(e));
    }

    public void deleteEmployee(int id) {
         view.showDeleteEmployee(dao.deleteEmployee(id));
    }

    public void editNameAsId() {
        view.showeditName(dao.editNameAsId(view.getId() , view.getName()));
    }

    public void editSexAsId() {
        view.showeditSex(dao.editSexAsId(view.getId() , view.getSex()));
    }

    public void editBirthdayAsId() {
        view.showeditBirthday(dao.editBirthdayAsId(view.getId() , view.getBirthday()));
    }

    public void editHiredateAsId() {
        view.showeditHiredate(dao.editHiredateAsId(view.getId() , view.getHiredate()));
    }

    public void editDepartment_idAsId() {
        view.showeditDepartment_id(dao.editDepartment_idAsId(view.getId() , view.getDepartment_id()));
    }
}

DepartmentController类

package com.situ.controller;

import com.situ.dao.DepartmentDao;
import com.situ.jdbcutils.JdbcUtils;
import com.situ.view.Departmentview;
import com.situ.view.EmployeeView;

/**
 * @author : 一只小海猪
 * @date : 12:07 2021/11/14
 */
public class DepartmentController {
    private EmployeeView view1 = new EmployeeView();
    private Departmentview view = new Departmentview();
    private DepartmentDao dao = new DepartmentDao();
    public void start() {
        while (true) {
            int choice = view.showMenu();
            if (choice == 1) {
                view.showfindMenu();
            } else if (choice == 2) {
                view.showeditMenu();
            } else if (choice == 4) {
                view.showdeleteMenu();
            } else if (choice == 3) {
                view.showaddMenu();
            } else if (choice == 5) {
               view.countMenu();
            }else if (choice == 6){
                System.out.println("谢谢使用!");
                break;
            }else{
                System.out.println("操作符不正确!请重新输入!");
            }
        }
    }

    public void findDepartment() {
        while (true) {
            String choice = view.getChoice();
            if ("1".equals(choice)) {
                findAll();
            } else if ("2".equals(choice)) {
                findAsId();
            } else if ("3".equals(choice)) {
                break;
            }else {
                System.out.println("操作符输入错误,请重新输入:");
            }
        }
    }

    private void findAsId() {
        view.printDepartment(dao.getAsId(dao.findDepartmentAsId(view.getDepartment_id())));
    }

    private void findAll() {
        view.printDepartment(dao.getAll(dao.findAllDepartment()));
    }

    public void addDepartment(String name) {
        view.print(dao.addDepartment(JdbcUtils.getConnection(),name));
    }

    public void deleteDepartment(String id) {
        view.print(dao.deleteDepartment(JdbcUtils.getConnection(),id));
    }

    public void editDepartmentName(String id, String name) {
        view.print(dao.editDepartmentName(JdbcUtils.getConnection() ,id,name));
    }

    public void departmentCount() {
        while (true) {
            String choice = view.getChoice();
            if ("1".equals(choice)) {
                dao.countEmployeeofDepartment();
            } else if ("2".equals(choice)) {
                view.printEmployeeofDepartment();
            } else if ("3".equals(choice)) {
                break;
            }else {
                System.out.println("操作符输入错误,请重新输入!格式:数字");
            }
        }
    }

    public void findAllASDepartment_id(String id) {
        view1.showAll(dao.AsArrayLIst(dao.findAllEmployeeAsid(JdbcUtils.getConnection(),id)));
    }
}

view包

EmployeeView类

package com.situ.view;

import com.situ.controller.EmployeeController;
import com.situ.model.Employee;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author : 一只小海猪
 * @date : 15:54 2021/11/12
 */
public class EmployeeView {

    //开始界面
    public int showMenu() {
        System.out.println("===========================");
        System.out.println("=====欢迎来到员工管理系统======");
        System.out.println("===========================");
        while(true) {
            Scanner sc = new Scanner(System.in);
        System.out.println("1.查询员工");
        System.out.println("2.编辑员工");
        System.out.println("3.删除员工");
        System.out.println("4.添加员工");
        System.out.println("5.退出");
        System.out.println("===========================");
        System.out.println("请输入操作符");
        int choice;
            try {
               choice = sc.nextInt();
                return choice;
            } catch (Exception e) {
                System.out.println("操作符错误,请重新输入!");
            }
        }
    }

    public void showfindMenu() {
        EmployeeController controller = new EmployeeController();
        System.out.println("===========员工查询==============");
        System.out.println("1.查询全部员工 2.按员工号查询 3.退出");
        System.out.println("===============================");
        while(true){
            Scanner sc  =new Scanner(System.in);
           String choice = sc.next();
           if ("1".equals(choice)){
                controller.findAll();
                break;
           }else if ("2".equals(choice)) {
               while(true) {
                   Scanner scanner = new Scanner(System.in);
                   System.out.println("请输入员工号:");
                   try {
                       int id = scanner.nextInt();
                       controller.findAsId(id);
                       break;
                   }catch (Exception e){
                       System.out.println("请重新输入正确员工号!");
                   }
               }
               break;
           }else if ("3".equals(choice)){
               break;
           }else{
               System.out.println("输入代号错误!请重新输入!");
           }
        }
    }

    //打印全部员工
    public void showAll(ArrayList<Employee> employees) {
        show(employees);
    }
    private void show(ArrayList<Employee> employees) {
        if(employees == null){
            System.out.println("没有员工信息!");
        }else {
            for (Employee e : employees) {
                System.out.println(e);
            }
        }
    }

    public void showAsId(ArrayList<Employee> queryAsId) {
        show(queryAsId);
    }

    public void showaddMenu() {
        EmployeeController ec = new EmployeeController();
        Employee e = new Employee();
        while (true){
            Scanner scanner = new Scanner(System.in);
            try {
                System.out.println("请输入姓名:格式:xxxxx");
                    e.setName(scanner.next());
                while (true) {
                    System.out.println("请输入性别:格式:男/女");
                    String sex = scanner.next();
                    if ("女".equals(sex) || "男".equals(sex)) {
                        e.setSex(sex);
                        break;
                    }
                }
                System.out.println("请输入出生日期:格式:yyyy-MM-dd");
                String birthday = scanner.next();
                    e.setBirthday(birthday);
                System.out.println("请输入入职日期:格式:yyyy-MM-dd");
                String hiredate = scanner.next();
                    e.setHiredate(hiredate);
                System.out.println("请输入部门编号:格式:数字 ");
                    e.setDepartment_id(scanner.nextInt());
                    ec.addemployee(e);
//                System.out.println(e);
                break;
            }catch (Exception e1){
                System.out.println("员工信息格式不正确!请重新输入!");
            }
        }
    }

    public void showAddEmployee(int addEmployee) {
        if (addEmployee>0){
            System.out.println("添加员工成功!");
        }else{
            System.out.println("添加员工失败!");
        }
    }

    public void showdeleteMenu() {
        EmployeeController es  =new EmployeeController();
        while (true){
            try {
                Scanner scanner = new Scanner(System.in);
                System.out.println("请输入需要删除的员工号:");
                int id = scanner.nextInt();
                es.deleteEmployee(id);
                break;
            }catch (Exception e){
                System.out.println("请输入重新正确的员工号:");
            }
        }
    }

    public void showDeleteEmployee(int deleteEmployee) {
        if (deleteEmployee>0){
            System.out.println("删除员工成功!");
        }else{
            System.out.println("删除员工失败!");
        }
    }

    public void showeditMenu() {
        EmployeeController employeeController = new EmployeeController();
        while (true) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("1.修改员工姓名 2.修改员工性别 3.修改员工出生日期 4.修改员工入职时间 5.修改员工所在部门号 6.退出");
            System.out.println("请输入操作符:格式:数字");
            String choice = scanner.next();
            if ("1".equals(choice)) {
                    employeeController.editNameAsId();
            } else if ("2".equals(choice)) {
                    employeeController.editSexAsId();
            } else if ("3".equals(choice)) {
                    employeeController.editBirthdayAsId();
            } else if ("4".equals(choice)) {
                    employeeController.editHiredateAsId();
            } else if ("5".equals(choice)) {
                    employeeController.editDepartment_idAsId();
            } else if ("6".equals(choice)) {
                    break;
            }else{
                System.out.println("操作符不正确!请重新输入:格式:数字");
            }
        }
    }

    public int getId() {
        while(true){
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入员工号:格式:数字");
            try{
                int id = scanner.nextInt();
                return id;
            }catch (Exception e){
                System.out.println("员工号错误!请重新输入正确的员工号:格式:数字");
            }
        }
    }

    public String getName() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入修改后员工的姓名:格式:xxx");
            String name = scanner.next();
            return name;
    }

    public void showeditName(int editNameAsId) {
        if(editNameAsId>0){
            System.out.println("修改员工姓名信息成功!");
        }else{
            System.out.println("修改员工姓名信息失败!");
        }
    }

    public String getSex() {
        while (true) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入修改后员工的性别:格式:男/女");
            String sex = scanner.next();
            if ("男".equals(sex)||"女".equals(sex)) {
                return sex;
            }else{
                System.out.println("请输入正确的性别:格式:男/女");
            }
        }
    }

    public void showeditSex(int editSexAsId) {
        if (editSexAsId>0){
            System.out.println("修改员工性别信息成功!");
        }else{
            System.out.println("修改员工性别信息失败!");
        }
    }

    public String getBirthday() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入修改后员工的生日:格式:2000-1-1");
        String birthday = scanner.next();
        return birthday;
    }

    public void showeditBirthday(int editBirthdayAsId) {
        if (editBirthdayAsId>0){
            System.out.println("修改员工生日信息成功!");
        }else{
            System.out.println("修改员工生日信息失败!");
        }
    }

    public String getHiredate() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入修改后员工的入职时间:格式:2000-1-1");
        String hiredate = scanner.next();
        return hiredate;
    }

    public void showeditHiredate(int editHiredateAsId) {
        if (editHiredateAsId>0){
            System.out.println("修改员工入职时间信息成功!");
        }else{
            System.out.println("修改员工入职时间信息失败!");
        }
    }

    public int getDepartment_id() {
        while(true){
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入员工部门号:格式:数字");
            try{
                int department_id = scanner.nextInt();
                return department_id;
            }catch (Exception e){
                System.out.println("员工部门号错误!请重新输入正确的员工部门号:格式:数字");
            }
        }
    }

    public void showeditDepartment_id(int editDepartment_idAsId) {
        if (editDepartment_idAsId>0){
            System.out.println("修改员工部门信息成功!");
        }else{
            System.out.println("修改员工部门信息失败!");
        }
    }
}

Departmentview类

package com.situ.view;

import com.situ.controller.DepartmentController;
import com.situ.model.Department;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author : 一只小海猪
 * @date : 12:10 2021/11/14
 */
public class Departmentview {


    public int showMenu() {
        while(true) {
            Scanner scanner = new Scanner(System.in);
            int choice;
            System.out.println("1.查询部门");
            System.out.println("2.修改部门");
            System.out.println("3.添加部门");
            System.out.println("4.删除部门");
            System.out.println("5.统计部门人数");
            System.out.println("6.退出");
            try {
                choice = scanner.nextInt();
                return choice;
            }catch (Exception e){
               System.out.println("请输入重新正确操作符!格式:数字");
            }
        }
    }

    public void showfindMenu() {
        DepartmentController departmentController = new DepartmentController();
        System.out.println("1.查询全部部门 2.按部门号查询 3.退出");
        departmentController.findDepartment();
    }

    public String getChoice() {
        System.out.println("请输入操作符:");
        Scanner scanner = new Scanner(System.in);
        return scanner.next();
    }

    public void printDepartment(ArrayList<Department> all) {
        for(Department d : all){
            System.out.println(d);
        }
    }

    public String getDepartment_id() {
        System.out.println("请输入部门号:");
        Scanner scanner = new Scanner(System.in);
        return scanner.next();
    }

    public void showaddMenu() {
        DepartmentController departmentController= new DepartmentController();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入部门名:");
        String name = scanner.next();
        departmentController.addDepartment(name);
    }

    public void print(int addDepartment) {
        if (addDepartment>0){
            System.out.println("操作成功!");
        }else{
            System.out.println("操作失败!");
        }
    }

    public void showdeleteMenu() {
        DepartmentController departmentController= new DepartmentController();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入部门编号:");
        String id = scanner.next();
        departmentController.deleteDepartment(id);
    }

    public void showeditMenu() {
        DepartmentController departmentController = new DepartmentController();
        System.out.println("只支持修改部门名称!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入部门编号");
        String id = scanner.next();
        System.out.println("请输入修改后的部门名:");
        String name  = scanner.next();
        departmentController.editDepartmentName(id, name);
    }

    public void countMenu() {
        DepartmentController departmentController = new DepartmentController();
        System.out.println("1.更新各部门员工数 2.显示部门员工信息 3.退出");
        departmentController.departmentCount();
    }

    public void printEmployeeofDepartment() {
        DepartmentController departmentController = new DepartmentController();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要查看的部门编号:");
        String id  = scanner.next();
        departmentController.findAllASDepartment_id(id);
    }
}

dao包

EmployeeDao类

package com.situ.dao;

import com.situ.jdbcutils.JdbcUtils;
import com.situ.model.Employee;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @author : 一只小海猪
 * @date : 15:55 2021/11/12
 */
public class EmployeeDao {
    public ArrayList<Employee> queryAll() {
        ArrayList<Employee> employees= new ArrayList<>();
        ResultSet rs = JdbcUtils.quryAllEmployee(JdbcUtils.getConnection());

            try {
                while (rs.next()){
                Employee e  =new Employee();
                e.setId(rs.getInt("id"));
                e.setName(rs.getString("name"));
                e.setSex(rs.getString("sex"));
                e.setBirthday(rs.getDate("birthday").toString());
                e.setHiredate(rs.getDate("hiredate").toString());
                e.setDepartment_id(rs.getInt("department_id"));
                employees.add(e);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        return employees;
        }

    public ArrayList<Employee> queryAsId(int id) {
        ArrayList<Employee> employee= new ArrayList<>();
        ResultSet rs = JdbcUtils.queryAsId(JdbcUtils.getConnection(),id);
        try {
            while (rs.next()){
                Employee e  =new Employee();
                e.setId(rs.getInt("id"));
                e.setName(rs.getString("name"));
                e.setSex(rs.getString("sex"));
                e.setBirthday(rs.getDate("birthday").toString());
                e.setHiredate(rs.getDate("hiredate").toString());
                e.setDepartment_id(rs.getInt("department_id"));
                employee.add(e);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return employee;
    }

    public int addEmployee(Employee e) {
        return JdbcUtils.AddEmployee(JdbcUtils.getConnection(),e);
    }

    public int deleteEmployee(int id) {
        return JdbcUtils.DeleteEmployee(JdbcUtils.getConnection(),id);
    }

    public int editNameAsId(int id, String name) {
        return JdbcUtils.UpdateNameAsId(JdbcUtils.getConnection(),id,name);
    }

    public int editSexAsId(int id, String sex) {
        return JdbcUtils.UpdateSexAsId(JdbcUtils.getConnection(),id,sex);
    }

    public int editBirthdayAsId(int id, String birthday) {
        return JdbcUtils.UpdateBirthdayAsId(JdbcUtils.getConnection(),id,birthday);
    }

    public int editHiredateAsId(int id, String hiredate) {
        return JdbcUtils.UpdateHiredateAsId(JdbcUtils.getConnection(),id,hiredate);
    }

    public int editDepartment_idAsId(int id, int department_id) {
        return JdbcUtils.UpdateDepartment_idAsId(JdbcUtils.getConnection(),id,department_id);
    }
}

DepartmentDao类

package com.situ.dao;

import com.situ.jdbcutils.JdbcUtils;
import com.situ.model.Count;
import com.situ.model.Department;
import com.situ.model.Employee;
import com.situ.view.Departmentview;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;

/**
 * @author : 一只小海猪
 * @date : 12:12 2021/11/14
 */
public class DepartmentDao {
    public ResultSet findAllDepartment() {
        return JdbcUtils.queryAllDepartment(JdbcUtils.getConnection());
    }

    public ArrayList<Department> getAll(ResultSet allDepartment) {
        ArrayList<Department> departments = new ArrayList<>();
        try {
            while (allDepartment.next()) {
                Department d = new Department();
                d.setId(allDepartment.getInt("department_id"));
                d.setName(allDepartment.getString("name"));
                d.setCountemployee(allDepartment.getInt("countemployee"));
                departments.add(d);
            }
            return departments;
        }catch (Exception e){
            System.out.println("查询部门信息失败!");
        }
        throw new RuntimeException("查询部门信息失败!");
    }

    public ResultSet findDepartmentAsId(String departmentId) {
        return JdbcUtils.queryDepartmentAsId(JdbcUtils.getConnection(),departmentId);
    }

    public ArrayList<Department> getAsId(ResultSet departmentAsId) {
        return getAll(departmentAsId);
    }

    public int addDepartment(Connection connection, String name) {
        return JdbcUtils.addDepartment(connection,name);
    }

    public int deleteDepartment(Connection connection, String id) {
        return JdbcUtils.deleteDepartment(connection,id);
    }

    public int editDepartmentName(Connection connection, String id, String name) {
        return JdbcUtils.editDepartmentName(connection,id,name);
    }

    public ResultSet findAllEmployeeAsid(Connection connection, String id) {
        return JdbcUtils.getAllEmployeeAsDid(connection,id);
    }


    public ArrayList<Employee> AsArrayLIst(ResultSet allEmployeeAsid) {
        ArrayList<Employee> employees = new ArrayList<>();
        try{
            while(allEmployeeAsid.next()){
                Employee e = new Employee();
                e.setId(allEmployeeAsid.getInt("id"));
                e.setName(allEmployeeAsid.getString("name"));
                e.setSex(allEmployeeAsid.getString("sex"));
                e.setBirthday(allEmployeeAsid.getDate("birthday").toString());
                e.setHiredate(allEmployeeAsid.getDate("hiredate").toString());
                e.setDepartment_id(allEmployeeAsid.getInt("department_id"));
                employees.add(e);
            }
            return employees;
        }catch (Exception e1){
            System.out.println("查询部门下员工信息失败!");
        }
        throw new RuntimeException("查询部门下员工信息失败!");
    }

    public void countEmployeeofDepartment() {
        Departmentview view = new Departmentview();
        ArrayList<Count> count = countEmployee();
        int departmrnt_id[] = countdepartment(count.size());
        int i = 0;
        for (Count c : count){
            view.print(JdbcUtils.setCountEmployee(JdbcUtils.getConnection(),c.getId(),departmrnt_id[i]));
            i++;
        }
    }

    private int[] countdepartment(int size) {
        int[] a = new int[size];
        ResultSet rs = JdbcUtils.countDepartment(JdbcUtils.getConnection());
        try{
            int i = 0;
            while(rs.next()){
                a[i] = rs.getInt("department_id");
                i++;
            }
            return a;
        }catch(Exception e){
            e.printStackTrace();
        }
        throw new RuntimeException("统计部门编号出错!");
    }


    private ArrayList<Count> countEmployee() {
        ArrayList<Count> counts = new ArrayList<>();
        ResultSet rs = JdbcUtils.countEmployee(JdbcUtils.getConnection());
        try {
            while (rs.next()){
                Count c = new Count();
              c.setId (rs.getInt("count(*)"));
              counts.add(c);
            }
            return counts;
        }catch(Exception e){
            e.printStackTrace();
        }
        throw new RuntimeException("统计部门人数出错!");
    }
}

jdbcutils包

JdbcUtils类

package com.situ.jdbcutils;

import com.situ.model.Employee;

import java.sql.*;

/**
 * @author : 一只小海猪
 * @date : 16:55 2021/11/12
 */
public class JdbcUtils {
    public static final String driver = "com.mysql.cj.jdbc.Driver";
    private static final String url = "jdbc:mysql://localhost:3306/employee?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
    private static final String user = "root";
    private static final String password = "123456";

    //获取数据库连接
    public static final Connection getConnection(){
        Connection conn = null;
        try {
            //加载驱动
            Class.forName(driver);

            conn = DriverManager.getConnection(url,user,password);

            return conn;
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("获取数据库连接失败!");
    }

    //查询全部员工
    public static ResultSet quryAllEmployee(Connection connection) {
        String sql = "select id ,`name`,sex,birthday,hiredate,department_id from t_employee";
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            return ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("查询全部员工信息失败");
    }

    public static ResultSet queryAsId(Connection connection, int id) {
        String sql = "select id ,`name`,sex,birthday,hiredate,department_id from t_employee where `id` = "+id;
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            return ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("查询员工信息失败");
    }

    public static int AddEmployee(Connection connection, Employee e) {
        String sql = "insert into t_employee(`name`,sex,birthday,hiredate,department_id) " +
                "values(" +"'"+e.getName()+"'"+
                ","+"'" +e.getSex()+"'"+
                "," +"'"+e.getBirthday()+"'"+
                ","+"'" +e.getHiredate()+"'"+
                "," +e.getDepartment_id()+
                ")";
//        System.out.println(sql);
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            return ps.executeUpdate();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        throw new RuntimeException("添加员工信息失败");
    }

    public static int DeleteEmployee(Connection connection, int id) {
        String sql = "delete from t_employee where t_employee.id = "+id;
//        System.out.println(sql);
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("删除员工信息失败");
    }

    public static int UpdateNameAsId(Connection connection, int id, String name) {
        String sql = "update t_employee set t_employee.`name` = "+"'"+name+"'"+" where t_employee.id = "+id;
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("修改员工姓名信息失败");
    }

    public static int UpdateSexAsId(Connection connection, int id, String sex) {
        String sql = "update t_employee set t_employee.sex = "+"'"+sex+"'"+" where t_employee.id = "+id;
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("修改员工性别信息失败");
    }

    public static int UpdateBirthdayAsId(Connection connection, int id, String birthday) {
        String sql = "update t_employee set t_employee.birthday = "+"'"+birthday+"'"+" where t_employee.id = "+id;
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("修改员工生日信息失败");
    }

    public static int UpdateHiredateAsId(Connection connection, int id, String hiredate) {
        String sql = "update t_employee set t_employee.hiredate = "+"'"+hiredate+"'"+" where t_employee.id = "+id;
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("修改员工入职时间信息失败");
    }

    public static int UpdateDepartment_idAsId(Connection connection, int id, int department_id) {
        String sql = "update t_employee set t_employee.department_id = "+department_id+ " where t_employee.id = "+id;
//        System.out.println(sql);
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("修改员工部门信息失败");
    }

    public static ResultSet queryAllDepartment(Connection connection) {
        String sql = "select department_id ,`name`,countemployee from t_department";
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            return ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("查询全部部门信息失败");
    }

    public static ResultSet queryDepartmentAsId(Connection connection, String departmentId) {
        String sql = "select department_id ,`name`,countemployee from t_department where department_id="+departmentId;
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            return ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("查询部门信息失败");
    }

    public static int addDepartment(Connection connection, String name) {
        String sql = "insert into t_department(`name`) values ("+"'"+name+"'"+")";
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("添加部门信息失败");
    }

    public static int deleteDepartment(Connection connection, String id) {
        String sql ="delete from t_department where department_id = "+id+" and countemployee = 0 ";
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            return ps.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }
        throw new RuntimeException("删除部门信息失败,请检查部门下是否还有成员!只支持删除成员为0的部门!");
    }

    public static int editDepartmentName(Connection connection, String id, String name) {
        String sql ="update t_department set `name`= "+"'"+name+"'"+" where department_id = "+id;
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            return ps.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }
        throw new RuntimeException("修改部门信息失败!");
    }

    public static ResultSet getAllEmployeeAsDid(Connection connection, String id) {
        String sql = "select id ,`name`,sex,birthday,hiredate,department_id from t_employee where  department_id = "+id;
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            return ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("查询部门下员工信息失败");
    }

    public static ResultSet countEmployee(Connection connection) {
        String sql = "select count(*) from t_employee GROUP BY department_id ORDER BY department_id  ";
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            return ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("查询部门下员工信息失败");
    }

    public static ResultSet countDepartment(Connection connection) {
        String sql = "select department_id from t_department";
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            return ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("查询全部部门编号失败");
    }

    public static int setCountEmployee(Connection connection, int id, int i) {
        String sql = "update t_department set countemployee = "+i+" where department_id = "+id ;
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("统计各部门员工失败!");
    }
}

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A little sea pig

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值