公司员工考勤管理系统java代码实现
时间: 2023-09-20 08:11:46 浏览: 130
基于java公司员工考勤管理系统设计与实现.docx
5星 · 资源好评率100%
公司员工考勤管理系统的Java实现包括以下步骤:
1. 定义员工类和考勤类,员工类包含员工的基本信息,考勤类包含员工的考勤记录。
```java
public class Employee {
private int id;
private String name;
private String department;
private String position;
// 省略getter和setter方法
}
public class Attendance {
private int id;
private Date date;
private int employeeId;
private String status;
// 省略getter和setter方法
}
```
2. 定义数据库连接类,实现与数据库的连接和操作。
```java
public class DBConnection {
private static final String URL = "jdbc:mysql://localhost:3306/attendance";
private static final String USER = "root";
private static final String PASSWORD = "root";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
3. 定义员工管理类和考勤管理类,实现员工和考勤的增删改查等操作。
```java
public class EmployeeManager {
public void addEmployee(Employee employee) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBConnection.getConnection();
String sql = "INSERT INTO employee (id, name, department, position) VALUES (?, ?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, employee.getId());
stmt.setString(2, employee.getName());
stmt.setString(3, employee.getDepartment());
stmt.setString(4, employee.getPosition());
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBConnection.close(conn, stmt, null);
}
}
public void deleteEmployee(int id) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBConnection.getConnection();
String sql = "DELETE FROM employee WHERE id=?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBConnection.close(conn, stmt, null);
}
}
public void updateEmployee(Employee employee) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBConnection.getConnection();
String sql = "UPDATE employee SET name=?, department=?, position=? WHERE id=?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, employee.getName());
stmt.setString(2, employee.getDepartment());
stmt.setString(3, employee.getPosition());
stmt.setInt(4, employee.getId());
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBConnection.close(conn, stmt, null);
}
}
public List<Employee> getEmployees() {
List<Employee> employees = new ArrayList<>();
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DBConnection.getConnection();
String sql = "SELECT * FROM employee";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
Employee employee = new Employee();
employee.setId(rs.getInt("id"));
employee.setName(rs.getString("name"));
employee.setDepartment(rs.getString("department"));
employee.setPosition(rs.getString("position"));
employees.add(employee);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBConnection.close(conn, stmt, rs);
}
return employees;
}
}
public class AttendanceManager {
public void addAttendance(Attendance attendance) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBConnection.getConnection();
String sql = "INSERT INTO attendance (id, date, employee_id, status) VALUES (?, ?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, attendance.getId());
stmt.setDate(2, new java.sql.Date(attendance.getDate().getTime()));
stmt.setInt(3, attendance.getEmployeeId());
stmt.setString(4, attendance.getStatus());
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBConnection.close(conn, stmt, null);
}
}
public void deleteAttendance(int id) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBConnection.getConnection();
String sql = "DELETE FROM attendance WHERE id=?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBConnection.close(conn, stmt, null);
}
}
public void updateAttendance(Attendance attendance) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBConnection.getConnection();
String sql = "UPDATE attendance SET date=?, employee_id=?, status=? WHERE id=?";
stmt = conn.prepareStatement(sql);
stmt.setDate(1, new java.sql.Date(attendance.getDate().getTime()));
stmt.setInt(2, attendance.getEmployeeId());
stmt.setString(3, attendance.getStatus());
stmt.setInt(4, attendance.getId());
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBConnection.close(conn, stmt, null);
}
}
public List<Attendance> getAttendances() {
List<Attendance> attendances = new ArrayList<>();
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DBConnection.getConnection();
String sql = "SELECT * FROM attendance";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
Attendance attendance = new Attendance();
attendance.setId(rs.getInt("id"));
attendance.setDate(rs.getDate("date"));
attendance.setEmployeeId(rs.getInt("employee_id"));
attendance.setStatus(rs.getString("status"));
attendances.add(attendance);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBConnection.close(conn, stmt, rs);
}
return attendances;
}
}
```
4. 编写测试类,测试员工和考勤的增删改查等操作。
```java
public class Test {
public static void main(String[] args) {
EmployeeManager employeeManager = new EmployeeManager();
AttendanceManager attendanceManager = new AttendanceManager();
// 添加员工
Employee employee1 = new Employee(1, "张三", "研发部", "工程师");
employeeManager.addEmployee(employee1);
Employee employee2 = new Employee(2, "李四", "市场部", "销售");
employeeManager.addEmployee(employee2);
// 添加考勤记录
Attendance attendance1 = new Attendance(1, new Date(), 1, "正常");
attendanceManager.addAttendance(attendance1);
Attendance attendance2 = new Attendance(2, new Date(), 2, "迟到");
attendanceManager.addAttendance(attendance2);
// 查询员工列表
List<Employee> employees = employeeManager.getEmployees();
for (Employee employee : employees) {
System.out.println(employee.getId() + " " + employee.getName() + " " + employee.getDepartment() + " " + employee.getPosition());
}
// 查询考勤记录列表
List<Attendance> attendances = attendanceManager.getAttendances();
for (Attendance attendance : attendances) {
System.out.println(attendance.getId() + " " + attendance.getDate() + " " + attendance.getEmployeeId() + " " + attendance.getStatus());
}
// 更新员工信息
Employee employee3 = new Employee(2, "王五", "市场部", "销售经理");
employeeManager.updateEmployee(employee3);
// 删除考勤记录
attendanceManager.deleteAttendance(2);
// 删除员工
employeeManager.deleteEmployee(1);
}
}
```
以上是一个简单的公司员工考勤管理系统的Java实现方案,其中还有很多细节需要处理,例如数据库连接池、异常处理、日期格式化等。
阅读全文