Eclipse+Java+Swing+Mysql implements employee salary management system

Table of contents

1. System introduction

1. Operating environment

2.Technical description

3. System functions

4.Database 

2. System implementation

1. Log in to the system

2. Administrator interface

3.Employee module

4. Department module

​Edit 

5. Salary module 

6. Ordinary employees 

3. Part of the code

DepartmentDao.java

DepartmentDaoImpl.java 

EmployeeDao.java 

EmployeeDaoImpl.java 


1. System introduction

1. Operating environment

Development tools: Eclipse/IDEA

JDK version: jdk1.8

Mysql version: 8.0.13

2.Technical description

Java+Swing+Mysql

3. System functions

Function: Two roles: administrator and employee
Ordinary employees do not need to log in: query a certain monthly salary;
Administrators can manage employees and departments , salary and other basic information.
Employee module: add, delete, modify, view
Department module: add, delete, modify, view
Administrator module : Add, delete, modify, view
Salary module: enter salary, query salary, issue salary, salary list

4.Database 

/*
Navicat MySQL Data Transfer

Source Server         : localhost_3306
Source Server Version : 80025
Source Host           : 127.0.0.1:3306
Source Database       : db_salary

Target Server Type    : MYSQL
Target Server Version : 80025
File Encoding         : 65001

Date: 2021-07-21 22:01:24
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for department
-- ----------------------------
DROP TABLE IF EXISTS `department`;
CREATE TABLE `department` (
  `id` varchar(255) NOT NULL,
  `name` varchar(255) NOT NULL,
  `director` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

-- ----------------------------
-- Records of department
-- ----------------------------
INSERT INTO `department` VALUES ('DE001', '财务部', '小李');
INSERT INTO `department` VALUES ('DE002', '经理部', '小黑');

-- ----------------------------
-- Table structure for employee
-- ----------------------------
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
  `id` varchar(255) NOT NULL,
  `name` varchar(255) NOT NULL,
  `sex` varchar(255) NOT NULL,
  `age` int NOT NULL,
  `department_id` varchar(255) NOT NULL,
  `position` varchar(255) NOT NULL,
  `phone` varchar(255) NOT NULL,
  `time` date NOT NULL,
  `address` varchar(255) NOT NULL,
  PRIMARY KEY (`id`,`department_id`),
  KEY `ed_dept_id` (`department_id`),
  KEY `id` (`id`),
  CONSTRAINT `ed_dept_id` FOREIGN KEY (`department_id`) REFERENCES `department` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

-- ----------------------------
-- Records of employee
-- ----------------------------
INSERT INTO `employee` VALUES ('1001', '王一', '男', '25', 'DE002', '会计', '18474588569', '2021-07-20', '山东曹县');
INSERT INTO `employee` VALUES ('1002', '王二3', '女', '36', 'DE002', '工程师', '15478453654', '2021-08-23', '山东日照');
INSERT INTO `employee` VALUES ('1003', '1', '1', '1', 'DE002', '1', '1', '2021-06-12', '1');

-- ----------------------------
-- Table structure for salary
-- ----------------------------
DROP TABLE IF EXISTS `salary`;
CREATE TABLE `salary` (
  `id` int NOT NULL AUTO_INCREMENT,
  `emp_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `basepay` int DEFAULT NULL,
  `welfare` int DEFAULT NULL,
  `reward` int DEFAULT NULL,
  `insurance` int DEFAULT NULL,
  `funds` int DEFAULT NULL,
  `month` varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `remark` varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`,`emp_id`),
  KEY `se_name` (`emp_id`),
  CONSTRAINT `se_name` FOREIGN KEY (`emp_id`) REFERENCES `employee` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8mb3 COLLATE=utf8_unicode_ci;

-- ----------------------------
-- Records of salary
-- ----------------------------
INSERT INTO `salary` VALUES ('1', '1001', '3000', '100', '100', '100', '500', '1月', '已发放');
INSERT INTO `salary` VALUES ('13', '1002', '3000', '200', '100', '100', '400', '1月', '未结算');
INSERT INTO `salary` VALUES ('15', '1003', '2500', '100', '200', '100', '300', '1月', '未结算');

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `status` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '管理员',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb3;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'admin', '123456', '管理员');
INSERT INTO `user` VALUES ('3', 'test', '123456', '管理员');

2. System implementation

1. Log in to the system

2. Administrator interface

 

 

3.Employee module

 

 

 

 

4. Department module


 

 

 

 

5. Salary module 

 

6. Ordinary employees 

 

 

 

3. Part of the code

DepartmentDao.java

package com.system.dao;

import java.util.List;

import com.system.entity.Department;

public interface DepartmentDao {

	/**
	 * 查询所有员工
	 * @return
	 */
	List<Department> selectAll();

	/**
	 * 添加部门
	 * 
	 * @param id
	 * @param name
	 * @param director
	 * @return
	 */
	int addDepartment(String id, String name, String director);

	/**
	 * 根据部门编号修改
	 * 
	 * @param id
	 * @param name
	 * @param director
	 * @return
	 */
	int updateDepartment(String id, String name, String director);

	/**
	 * 根据部门编号删除
	 * 
	 * @param id
	 * @return
	 */
	int delete(String id);

	/**
	 * 根据id查询部门
	 * 
	 * @param id
	 * @return
	 */
	Department selectDepartmentOneById(String id);

	/**
	 * 根据name查询id
	 * 
	 * @param name
	 * @return
	 */
	Department selectDepartmentOneByName(String name);

}

DepartmentDaoImpl.java 

package com.system.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.system.entity.Department;
import com.system.utils.DB;

public class DepartmentDaoImpl implements DepartmentDao {

	private DB db;
	private Connection connection;
	private PreparedStatement ps;
	private ResultSet rs;

	@Override
	public List<Department> selectAll() {
		String sql = "select * from department";
		connection = new DB().getConnection();
		List<Department> list = new ArrayList();
		try {
			ps = connection.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				Department department = new Department();
				department.setId(rs.getString("id"));
				department.setName(rs.getString("name"));
				department.setDirector(rs.getString("director"));
				list.add(department);
			}
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Department selectDepartmentOneById(String id) {
		String sql = "select * from department where id = ?";
		connection = new DB().getConnection();
		Department department = null;
		try {
			ps = connection.prepareStatement(sql);
			ps.setString(1, id);
			rs = ps.executeQuery();
			while (rs.next()) {
				department = new Department();
				department.setId(rs.getString("id"));
				department.setName(rs.getString("name"));
				department.setDirector(rs.getString("director"));
			}
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return department;
	}

	@Override
	public int addDepartment(String id, String name, String director) {
		String sql = "insert into department(id,name,director) values(?,?,?)";
		connection = new DB().getConnection();
		int flag = 0;
		try {
			ps = connection.prepareStatement(sql);
			ps.setString(1, id);
			ps.setString(2, name);
			ps.setString(3, director);
			flag = ps.executeUpdate();
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public int updateDepartment(String id, String name, String director) {
		String sql = "update department set name = ?,director = ? where id = ?";
		connection = new DB().getConnection();
		int flag = 0;
		try {
			ps = connection.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, director);
			ps.setString(3, id);
			flag = ps.executeUpdate();
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public int delete(String id) {
		String sql = "delete from department where id = ?";
		connection = new DB().getConnection();
		int flag = 0;
		try {
			ps = connection.prepareStatement(sql);
			ps.setString(1, id);
			flag = ps.executeUpdate();
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public Department selectDepartmentOneByName(String name) {
		String sql = "select * from department where name = ?";
		connection = new DB().getConnection();
		Department department = null;
		try {
			ps = connection.prepareStatement(sql);
			ps.setString(1, name);
			rs = ps.executeQuery();
			while (rs.next()) {
				department = new Department();
				department.setId(rs.getString("id"));
				department.setName(rs.getString("name"));
				department.setDirector(rs.getString("director"));
			}
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return department;
	}

}

EmployeeDao.java 

package com.system.dao;

import java.util.List;

import com.system.entity.Employee;

public interface EmployeeDao {

	/**
	 * 查询所有员工
	 * 
	 * @return
	 */
	List<Employee> selectAll();

	/**
	 * id 查询 员工
	 * 
	 * @param id
	 * @return
	 */
	Employee selectOneById(String id);

	/**
	 * 添加员工
	 * 
	 * @param id
	 * @return
	 */
	int addEmployee(Employee employee);

	/**
	 * id 修改员工
	 * 
	 * @param id
	 * @return
	 */
	int updateEmployeeById(Employee employee);

	/**
	 * 删除员工
	 * 
	 * @param id
	 * @return
	 */
	int deleteEmployeeById(String id);

}

EmployeeDaoImpl.java 

package com.system.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.system.entity.Employee;
import com.system.utils.DB;

public class EmployeeDaoImpl implements EmployeeDao {

	private DB db;
	private Connection connection;
	private PreparedStatement ps;
	private ResultSet rs;

	@Override
	public List<Employee> selectAll() {
		String sql = "select e.id,e.name,e.sex,e.age,d.name as department,e.position,e.phone,e.time,e.address "
				+ "from employee e,department d where e.department_id = d.id";
		connection = new DB().getConnection();
		List<Employee> list = new ArrayList();
		try {
			ps = connection.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				Employee employee = new Employee();
				employee.setId(rs.getString("e.id"));
				employee.setName(rs.getString("e.name"));
				employee.setSex(rs.getString("e.sex"));
				employee.setAge(rs.getInt("e.age"));
				employee.setDepartmentName(rs.getString("department"));
				employee.setPosition(rs.getString("e.position"));
				employee.setPhone(rs.getString("e.phone"));
				employee.setTime(rs.getString("e.time"));
				employee.setAddress(rs.getString("e.address"));
				list.add(employee);
			}
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Employee selectOneById(String id) {
		String sql = "select e.id,e.name,e.sex,e.age,d.name as department,e.position,e.phone,e.time,e.address "
				+ "from employee e,department d where e.department_id = d.id and e.id = " + id;
		connection = new DB().getConnection();
		Employee employee = null;
		try {
			ps = connection.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				employee = new Employee();
				employee.setId(rs.getString("e.id"));
				employee.setName(rs.getString("e.name"));
				employee.setSex(rs.getString("e.sex"));
				employee.setAge(rs.getInt("e.age"));
				employee.setDepartmentName(rs.getString("department"));
				employee.setPosition(rs.getString("e.position"));
				employee.setPhone(rs.getString("e.phone"));
				employee.setTime(rs.getString("e.time"));
				employee.setAddress(rs.getString("e.address"));
			}
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return employee;
	}

	@Override
	public int addEmployee(Employee employee) {
		String sql = "insert into employee(id,name,sex,age,department_id,position,phone,time,address) values(?,?,?,?,?,?,?,?,?)";
		 connection = new DB().getConnection();
		 int flag = 0;
		 try {
			 ps = connection.prepareStatement(sql);
			 ps.setString(1, employee.getId());
			 ps.setString(2, employee.getName());
			 ps.setString(3, employee.getSex());
			 ps.setInt(4, employee.getAge());
			 ps.setString(5, employee.getDepartmentName());
			 ps.setString(6, employee.getPosition());
			 ps.setString(7, employee.getPhone());
			 ps.setString(8, employee.getTime());
			 ps.setString(9, employee.getAddress());
			flag = ps.executeUpdate();
			connection.close();
		 } catch (SQLException e) {
			e.printStackTrace();
		 }
		return flag;
	}

	@Override
	public int updateEmployeeById(Employee employee) {
		String sql = "update employee set name = ?,sex = ?, age = ?,department_id = ?,position=?,phone=?,time=?,address=?  where id= ?";
		connection = new DB().getConnection();
		int flag = 0;
		try {
			ps = connection.prepareStatement(sql);
			ps.setString(1, employee.getName());
			ps.setString(2, employee.getSex());
			ps.setInt(3, employee.getAge());
			ps.setString(4, employee.getDepartmentName());
			ps.setString(5, employee.getPosition());
			ps.setString(6, employee.getPhone());
			ps.setString(7, employee.getTime());
			ps.setString(8, employee.getAddress());
			ps.setString(9, employee.getId());
			flag = ps.executeUpdate();
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public int deleteEmployeeById(String id) {
		String sql = "delete from employee where id= ?";
		connection = new DB().getConnection();
		int flag = 0;
		try {
			ps = connection.prepareStatement(sql);
			ps.setString(1, id);
			flag = ps.executeUpdate();
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

}

 

Guess you like

Origin blog.csdn.net/qq_61827376/article/details/125313870#comments_30383201