可以给我用java和数据库写一个请假系统吗
时间: 2024-01-06 12:03:30 浏览: 146
当然可以,以下是一个简单的请假管理系统的代码示例,使用 Java 编程语言和 MySQL 数据库:
1. 创建请假请求表:
```sql
CREATE TABLE leave_request (
id INT NOT NULL AUTO_INCREMENT,
employee_id INT NOT NULL,
start_date DATE NOT NULL,
end_date DATE NOT NULL,
reason TEXT,
status VARCHAR(20) NOT NULL,
PRIMARY KEY (id)
);
```
2. 创建员工表:
```sql
CREATE TABLE employee (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
role VARCHAR(20) NOT NULL,
PRIMARY KEY (id)
);
```
3. 创建 Java 类:
请假请求实体类:
```java
public class LeaveRequest {
private int id;
private int employeeId;
private Date startDate;
private Date endDate;
private String reason;
private String status;
// 省略 getter 和 setter 方法
}
```
员工实体类:
```java
public class Employee {
private int id;
private String name;
private String email;
private String password;
private String role;
// 省略 getter 和 setter 方法
}
```
请假请求 DAO 类:
```java
public class LeaveRequestDao {
private Connection conn;
// 构造函数
public LeaveRequestDao(Connection conn) {
this.conn = conn;
}
// 插入请假请求
public void insert(LeaveRequest request) throws SQLException {
String sql = "INSERT INTO leave_request (employee_id, start_date, end_date, reason, status) VALUES (?, ?, ?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1, request.getEmployeeId());
stmt.setDate(2, new java.sql.Date(request.getStartDate().getTime()));
stmt.setDate(3, new java.sql.Date(request.getEndDate().getTime()));
stmt.setString(4, request.getReason());
stmt.setString(5, request.getStatus());
stmt.executeUpdate();
}
// 查询所有请假请求
public List<LeaveRequest> findAll() throws SQLException {
List<LeaveRequest> requests = new ArrayList<LeaveRequest>();
String sql = "SELECT * FROM leave_request";
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
LeaveRequest request = new LeaveRequest();
request.setId(rs.getInt("id"));
request.setEmployeeId(rs.getInt("employee_id"));
request.setStartDate(rs.getDate("start_date"));
request.setEndDate(rs.getDate("end_date"));
request.setReason(rs.getString("reason"));
request.setStatus(rs.getString("status"));
requests.add(request);
}
return requests;
}
// 根据 ID 查询请假请求
public LeaveRequest findById(int id) throws SQLException {
String sql = "SELECT * FROM leave_request WHERE id=?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
LeaveRequest request = new LeaveRequest();
request.setId(rs.getInt("id"));
request.setEmployeeId(rs.getInt("employee_id"));
request.setStartDate(rs.getDate("start_date"));
request.setEndDate(rs.getDate("end_date"));
request.setReason(rs.getString("reason"));
request.setStatus(rs.getString("status"));
return request;
} else {
return null;
}
}
}
```
员工 DAO 类:
```java
public class EmployeeDao {
private Connection conn;
// 构造函数
public EmployeeDao(Connection conn) {
this.conn = conn;
}
// 根据用户名和密码查询员工
public Employee findByEmailAndPassword(String email, String password) throws SQLException {
String sql = "SELECT * FROM employee WHERE email=? AND password=?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, email);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
Employee employee = new Employee();
employee.setId(rs.getInt("id"));
employee.setName(rs.getString("name"));
employee.setEmail(rs.getString("email"));
employee.setPassword(rs.getString("password"));
employee.setRole(rs.getString("role"));
return employee;
} else {
return null;
}
}
}
```
4. 创建 Web 应用程序:
您可以使用 Java Web 框架,例如 Spring MVC 或 Struts2,来创建 Web 应用程序并实现请假管理系统的功能。例如,您可以创建以下页面:
- 登录页面:员工可以使用他们的邮箱和密码登录系统。
- 请假请求页面:员工可以提交新的请假请求。
- 请假审批页面:主管可以查看并批准或拒绝请假请求。
- 请假记录页面:HR 管理员可以查看所有请假请求的记录和统计数据。
以上是一个简单的请假管理系统的示例,您可以根据实际需求进行修改和扩展。
阅读全文