用java写前端,MySQL写数据库做一个系统,内容包括:(1)教职工基本信息管理:教职工基本信息的增加、修 改与删除; (2)教职工签到管理:教职工输入编号后,签到,系统 自动记录其签到的时间,并注明是否迟到。 (3)教职工签到情况的查询与统计:按不同的条件对 工签到情况进行查询与统计 。java的后端代码具体怎么实现?不需要写前端。
时间: 2024-02-19 21:57:55 浏览: 131
好的,以下是Java后端代码的实现:
1. 建立教职工基本信息表和签到记录表的实体类:
```java
public class Employee {
private int id;
private String name;
private String gender;
private Date birthDate;
private String phone;
// 省略getters和setters
}
public class CheckInRecord {
private int id;
private int employeeId;
private Date checkInTime;
private boolean isLate;
// 省略getters和setters
}
```
2. 建立DAO类,实现与数据库的交互:
```java
public class EmployeeDao {
private Connection conn;
// 构造方法中初始化数据库连接
public EmployeeDao() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/employee", "root", "password");
} catch (Exception e) {
e.printStackTrace();
}
}
// 添加教职工基本信息到数据库中
public void addEmployee(Employee employee) throws SQLException {
PreparedStatement stmt = conn.prepareStatement("INSERT INTO employee(name, gender, birth_date, phone) VALUES (?, ?, ?, ?)");
stmt.setString(1, employee.getName());
stmt.setString(2, employee.getGender());
stmt.setDate(3, new java.sql.Date(employee.getBirthDate().getTime()));
stmt.setString(4, employee.getPhone());
stmt.executeUpdate();
}
// 修改教职工基本信息
public void updateEmployee(Employee employee) throws SQLException {
PreparedStatement stmt = conn.prepareStatement("UPDATE employee SET name=?, gender=?, birth_date=?, phone=? WHERE id=?");
stmt.setString(1, employee.getName());
stmt.setString(2, employee.getGender());
stmt.setDate(3, new java.sql.Date(employee.getBirthDate().getTime()));
stmt.setString(4, employee.getPhone());
stmt.setInt(5, employee.getId());
stmt.executeUpdate();
}
// 删除教职工基本信息
public void deleteEmployee(int id) throws SQLException {
PreparedStatement stmt = conn.prepareStatement("DELETE FROM employee WHERE id=?");
stmt.setInt(1, id);
stmt.executeUpdate();
}
// 根据教职工编号查询基本信息
public Employee getEmployeeById(int id) throws SQLException {
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM employee WHERE id=?");
stmt.setInt(1, id);
ResultSet rs = stmt.executeQuery();
Employee employee = null;
if (rs.next()) {
employee = new Employee();
employee.setId(rs.getInt("id"));
employee.setName(rs.getString("name"));
employee.setGender(rs.getString("gender"));
employee.setBirthDate(rs.getDate("birth_date"));
employee.setPhone(rs.getString("phone"));
}
return employee;
}
}
public class CheckInRecordDao {
private Connection conn;
// 构造方法中初始化数据库连接
public CheckInRecordDao() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/employee", "root", "password");
} catch (Exception e) {
e.printStackTrace();
}
}
// 添加签到记录到数据库中
public void addCheckInRecord(CheckInRecord checkInRecord) throws SQLException {
PreparedStatement stmt = conn.prepareStatement("INSERT INTO check_in_record(employee_id, check_in_time, is_late) VALUES (?, ?, ?)");
stmt.setInt(1, checkInRecord.getEmployeeId());
stmt.setTimestamp(2, new Timestamp(checkInRecord.getCheckInTime().getTime()));
stmt.setBoolean(3, checkInRecord.isLate());
stmt.executeUpdate();
}
// 根据教职工编号查询签到记录
public List<CheckInRecord> getCheckInRecordByEmployeeId(int employeeId) throws SQLException {
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM check_in_record WHERE employee_id=?");
stmt.setInt(1, employeeId);
ResultSet rs = stmt.executeQuery();
List<CheckInRecord> checkInRecords = new ArrayList<CheckInRecord>();
while (rs.next()) {
CheckInRecord checkInRecord = new CheckInRecord();
checkInRecord.setId(rs.getInt("id"));
checkInRecord.setEmployeeId(rs.getInt("employee_id"));
checkInRecord.setCheckInTime(rs.getTimestamp("check_in_time"));
checkInRecord.setLate(rs.getBoolean("is_late"));
checkInRecords.add(checkInRecord);
}
return checkInRecords;
}
// 根据时间段查询签到记录
public List<CheckInRecord> getCheckInRecordByTimeRange(Date start, Date end) throws SQLException {
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM check_in_record WHERE check_in_time BETWEEN ? AND ?");
stmt.setTimestamp(1, new Timestamp(start.getTime()));
stmt.setTimestamp(2, new Timestamp(end.getTime()));
ResultSet rs = stmt.executeQuery();
List<CheckInRecord> checkInRecords = new ArrayList<CheckInRecord>();
while (rs.next()) {
CheckInRecord checkInRecord = new CheckInRecord();
checkInRecord.setId(rs.getInt("id"));
checkInRecord.setEmployeeId(rs.getInt("employee_id"));
checkInRecord.setCheckInTime(rs.getTimestamp("check_in_time"));
checkInRecord.setLate(rs.getBoolean("is_late"));
checkInRecords.add(checkInRecord);
}
return checkInRecords;
}
}
```
3. 实现签到功能:
```java
public class CheckInService {
private CheckInRecordDao checkInRecordDao;
public CheckInService() {
checkInRecordDao = new CheckInRecordDao();
}
// 教职工签到
public void checkIn(int employeeId) throws SQLException {
EmployeeDao employeeDao = new EmployeeDao();
Employee employee = employeeDao.getEmployeeById(employeeId);
if (employee == null) {
throw new RuntimeException("教职工不存在");
}
CheckInRecord checkInRecord = new CheckInRecord();
checkInRecord.setEmployeeId(employeeId);
checkInRecord.setCheckInTime(new Date());
checkInRecord.setLate(checkIfLate());
checkInRecordDao.addCheckInRecord(checkInRecord);
}
// 判断教职工是否迟到
private boolean checkIfLate() {
// 实现判断逻辑
}
}
```
4. 实现查询和统计功能:
```java
public class CheckInRecordService {
private CheckInRecordDao checkInRecordDao;
public CheckInRecordService() {
checkInRecordDao = new CheckInRecordDao();
}
// 根据教职工编号查询签到记录
public List<CheckInRecord> getCheckInRecordByEmployeeId(int employeeId) throws SQLException {
return checkInRecordDao.getCheckInRecordByEmployeeId(employeeId);
}
// 根据时间段查询签到记录
public List<CheckInRecord> getCheckInRecordByTimeRange(Date start, Date end) throws SQLException {
return checkInRecordDao.getCheckInRecordByTimeRange(start, end);
}
// 统计教职工迟到次数
public int countLateByEmployeeId(int employeeId) throws SQLException {
List<CheckInRecord> checkInRecords = checkInRecordDao.getCheckInRecordByEmployeeId(employeeId);
int count = 0;
for (CheckInRecord checkInRecord : checkInRecords) {
if (checkInRecord.isLate()) {
count++;
}
}
return count;
}
// 统计所有教职工迟到次数
public int countLateAll() throws SQLException {
List<CheckInRecord> checkInRecords = checkInRecordDao.getCheckInRecordByTimeRange(new Date(0), new Date());
int count = 0;
for (CheckInRecord checkInRecord : checkInRecords) {
if (checkInRecord.isLate()) {
count++;
}
}
return count;
}
}
```
以上就是Java后端代码的实现。其中,DAO类实现了对数据库的增删改查操作,Service类实现了业务逻辑,如签到、查询和统计等。当然,这只是一个简单的示例,实际应用中还需要考虑更多的细节和安全性问题。
阅读全文