12.2
package com.attendance.dao;
import com.attendance.bean.Employee;
import com.attendance.util.DBUtil;
import java.sql.*;
public class EmployeeDao {
public boolean addEmployee(Employee emp) {
String sql = "INSERT INTO employee (job_id, name, sex, birthday, department_id, role, password) VALUES (?, ?, ?, ?, ?, ?, ?)";
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, emp.getJobId());
pstmt.setString(2, emp.getName());
pstmt.setString(3, emp.getSex());
pstmt.setString(4, emp.getBirthday());
pstmt.setString(5, emp.getDepartmentId());
pstmt.setString(6, emp.getRole());
pstmt.setString(7, emp.getPassword());
return pstmt.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt, null);
}
return false;
}
// 生成工号:20190001, 20190002...
public String generateJobId() {
String sql = "SELECT MAX(job_id) FROM employee WHERE job_id LIKE '2019%'";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
String maxId = rs.getString(1);
if (maxId == null) return "20190001";
int num = Integer.parseInt(maxId.substring(4)) + 1;
return "2019" + String.format("%04d", num);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, rs);
}
return "20190001";
}
}

浙公网安备 33010602011771号