1. 查询部门员工

public List getEmployeesByDepartment(String department) { List employees = new ArrayList<>(); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; String sql = "SELECT * FROM employee WHERE dname = ?"; try { conn = DBUtil.getConnection(); ps = conn.prepareStatement(sql); ps.setString(1, department); rs = ps.executeQuery(); while (rs.next()) { Employee employee = new Employee(); employee.setEno(rs.getInt("eno")); employee.setEname(rs.getString("ename")); employee.setSalary(rs.getFloat("salary")); employee.setDname(rs.getString("dname")); employee.setHiredate(rs.getString("hiredate")); employees.add(employee); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(conn, ps, rs); } return employees; }

  1. 办理员工入职

public boolean addEmployee(Employee employee) { Connection conn = null; PreparedStatement ps = null; String sql = "INSERT INTO employee (eno, ename, salary, dname, hiredate) VALUES (?, ?, ?, ?, ?)"; try { conn = DBUtil.getConnection(); ps = conn.prepareStatement(sql); ps.setInt(1, employee.getEno()); ps.setString(2, employee.getEname()); ps.setFloat(3, employee.getSalary()); ps.setString(4, employee.getDname()); ps.setString(5, employee.getHiredate()); int result = ps.executeUpdate(); if (result > 0) { return true; } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(conn, ps); } return false; }

  1. 调整员工薪资

public boolean updateEmployeeSalary(int eno, float salary) { Connection conn = null; PreparedStatement ps = null; String sql = "UPDATE employee SET salary = ? WHERE eno = ?"; try { conn = DBUtil.getConnection(); ps = conn.prepareStatement(sql); ps.setFloat(1, salary); ps.setInt(2, eno); int result = ps.executeUpdate(); if (result > 0) { return true; } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(conn, ps); } return false; }

  1. 员工离职

public boolean deleteEmployee(int eno) { Connection conn = null; PreparedStatement ps = null; String sql = "DELETE FROM employee WHERE eno = ?"; try { conn = DBUtil.getConnection(); ps = conn.prepareStatement(sql); ps.setInt(1, eno); int result = ps.executeUpdate(); if (result > 0) { return true; } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(conn, ps); } return false; }

  1. 分页查询员工

public List getEmployeesByPage(int pageNum, int pageSize) { List employees = new ArrayList<>(); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; String sql = "SELECT * FROM employee LIMIT ?, ?"; try { conn = DBUtil.getConnection(); ps = conn.prepareStatement(sql); ps.setInt(1, (pageNum - 1) * pageSize); ps.setInt(2, pageSize); rs = ps.executeQuery(); while (rs.next()) { Employee employee = new Employee(); employee.setEno(rs.getInt("eno")); employee.setEname(rs.getString("ename")); employee.setSalary(rs.getFloat("salary")); employee.setDname(rs.getString("dname")); employee.setHiredate(rs.getString("hiredate")); employees.add(employee); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(conn, ps, rs); } return employees;


原文地址: https://www.cveoy.top/t/topic/gJca 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录