这篇文章将为大家详细讲解有关如何利用MySQL和Java开发一个简单的人事管理系统,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
核心技术: MySQL、Java
开发步骤:
1. 设定数据库架构
- 创建一个名为"hr_system"的数据库。
- 定义以下数据表:
- employee (id, name, email, job_title, department, salary)
- department (id, name)
2. Java代码实现
2.1. 连接数据库
import java.sql.*;
public class EmployeeManager {
private Connection connection;
public EmployeeManager() throws SQLException {
String url = "jdbc:mysql://localhost:3306/hr_system";
String user = "root";
String password = "password";
connection = DriverManager.getConnection(url, user, password);
}
}
2.2. 操作员工记录
public void addEmployee(Employee employee) throws SQLException {
String sql = "INSERT INTO employee (name, email, job_title, department, salary) VALUES (?, ?, ?, ?, ?)";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setString(1, employee.getName());
statement.setString(2, employee.getEmail());
statement.setString(3, employee.getJobTitle());
statement.setString(4, employee.getDepartment());
statement.setDouble(5, employee.getSalary());
statement.executeUpdate();
}
}
public List<Employee> getAllEmployees() throws SQLException {
String sql = "SELECT * FROM employee";
List<Employee> employees = new ArrayList<>();
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql)) {
while (resultSet.next()) {
employees.add(new Employee(resultSet.getInt("id"), resultSet.getString("name"), resultSet.getString("email"), resultSet.getString("job_title"), resultSet.getString("department"), resultSet.getDouble("salary")));
}
}
return employees;
}
public void updateEmployee(Employee employee) throws SQLException {
String sql = "UPDATE employee SET name = ?, email = ?, job_title = ?, department = ?, salary = ? WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setString(1, employee.getName());
statement.setString(2, employee.getEmail());
statement.setString(3, employee.getJobTitle());
statement.setString(4, employee.getDepartment());
statement.setDouble(5, employee.getSalary());
statement.setInt(6, employee.getId());
statement.executeUpdate();
}
}
public void deleteEmployee(int id) throws SQLException {
String sql = "DELETE FROM employee WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setInt(1, id);
statement.executeUpdate();
}
}
2.3. 操作部门记录
public void addDepartment(Department department) throws SQLException {
String sql = "INSERT INTO department (name) VALUES (?)";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setString(1, department.getName());
statement.executeUpdate();
}
}
public List<Department> getAllDepartments() throws SQLException {
String sql = "SELECT * FROM department";
List<Department> departments = new ArrayList<>();
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql)) {
while (resultSet.next()) {
departments.add(new Department(resultSet.getInt("id"), resultSet.getString("name")));
}
}
return departments;
}
public void updateDepartment(Department department) throws SQLException {
String sql = "UPDATE department SET name = ? WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setString(1, department.getName());
statement.setInt(2, department.getId());
statement.executeUpdate();
}
}
public void deleteDepartment(int id) throws SQLException {
String sql = "DELETE FROM department WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setInt(1, id);
statement.executeUpdate();
}
}
3. 关闭连接
public void closeConnection() throws SQLException {
if (connection != null) {
connection.close();
}
}
4. 使用示例
EmployeeManager employeeManager = new EmployeeManager();
employeeManager.addEmployee(new Employee("John Doe", "john.doe@example.com", "Software Engineer", "IT", 60000));
List<Employee> employees = employeeManager.getAllEmployees();
employeeManager.closeConnection();
以上就是如何利用MySQL和Java开发一个简单的人事管理系统的详细内容,更多请关注编程学习网其它相关文章!