模拟某单位的人事管理。人事信息主要包括:员工编号、所在部门、职务、薪资等级、薪资、联系方式等内容;部门信息主要包括:部门编号、部门名称等内容。完成以下操作:实现员工信息、部门信息的添加、修改、删除和查询。
时间: 2023-11-29 18:05:02 浏览: 117
好的,这是一个比较复杂的问题,我将从以下几个方面来回答你的问题:
1. 数据库设计
2. 数据库操作
3. 界面设计
首先,我们需要设计一个数据库来存储人事信息和部门信息。可以使用关系型数据库,例如MySQL、Oracle等。以下是一个简单的数据库设计:
**员工表**
| 字段名 | 类型 | 说明 |
| --- | --- | --- |
| id | int | 员工编号 |
| dept_id | int | 所在部门编号 |
| position | varchar(50) | 职务 |
| salary_grade | int | 薪资等级 |
| salary | float | 薪资 |
| phone | varchar(50) | 联系方式 |
**部门表**
| 字段名 | 类型 | 说明 |
| --- | --- | --- |
| id | int | 部门编号 |
| name | varchar(50) | 部门名称 |
接下来,我们需要实现数据库操作。可以使用Java的JDBC连接数据库,进行增删改查操作。以下是一个简单的Java代码示例:
```java
import java.sql.*;
public class HRManagement {
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost:3306/hr_management";
private static final String USER = "root";
private static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// 注册 JDBC 驱动器
Class.forName(JDBC_DRIVER);
// 打开连接
System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// 执行查询
System.out.println("实例化Statement对象...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, dept_id, position, salary_grade, salary, phone FROM employee";
ResultSet rs = stmt.executeQuery(sql);
// 处理结果集
while (rs.next()) {
// 通过字段检索
int id = rs.getInt("id");
int dept_id = rs.getInt("dept_id");
String position = rs.getString("position");
int salary_grade = rs.getInt("salary_grade");
float salary = rs.getFloat("salary");
String phone = rs.getString("phone");
// 输出数据
System.out.print("ID: " + id);
System.out.print(", Dept ID: " + dept_id);
System.out.print(", Position: " + position);
System.out.print(", Salary Grade: " + salary_grade);
System.out.print(", Salary: " + salary);
System.out.println(", Phone: " + phone);
}
// 清理环境
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
// 处理 JDBC 错误
se.printStackTrace();
} catch (Exception e) {
// 处理 Class.forName 错误
e.printStackTrace();
} finally {
// 关闭资源
try {
if (stmt != null) stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
```
以上代码演示了如何查询员工表中的数据。其他增删改操作也可以通过类似的方式实现。
最后,我们需要实现一个界面来让用户进行操作。可以使用Java Swing或JavaFX等图形界面库来实现。以下是一个简单的JavaFX界面示例:
```java
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class HRManagementUI extends Application {
private TextField employeeIdField;
private TextField employeeDeptIdField;
private TextField employeePositionField;
private TextField employeeSalaryGradeField;
private TextField employeeSalaryField;
private TextField employeePhoneField;
private TextField deptIdField;
private TextField deptNameField;
@Override
public void start(Stage primaryStage) {
// 创建控件
Button addEmployeeButton = new Button("添加员工");
Button updateEmployeeButton = new Button("修改员工");
Button deleteEmployeeButton = new Button("删除员工");
Button searchEmployeeButton = new Button("查询员工");
Button addDeptButton = new Button("添加部门");
Button updateDeptButton = new Button("修改部门");
Button deleteDeptButton = new Button("删除部门");
Button searchDeptButton = new Button("查询部门");
employeeIdField = new TextField();
employeeDeptIdField = new TextField();
employeePositionField = new TextField();
employeeSalaryGradeField = new TextField();
employeeSalaryField = new TextField();
employeePhoneField = new TextField();
deptIdField = new TextField();
deptNameField = new TextField();
// 设置控件样式
employeeIdField.setPromptText("员工编号");
employeeDeptIdField.setPromptText("所在部门编号");
employeePositionField.setPromptText("职务");
employeeSalaryGradeField.setPromptText("薪资等级");
employeeSalaryField.setPromptText("薪资");
employeePhoneField.setPromptText("联系方式");
deptIdField.setPromptText("部门编号");
deptNameField.setPromptText("部门名称");
addEmployeeButton.setPrefWidth(100);
updateEmployeeButton.setPrefWidth(100);
deleteEmployeeButton.setPrefWidth(100);
searchEmployeeButton.setPrefWidth(100);
addDeptButton.setPrefWidth(100);
updateDeptButton.setPrefWidth(100);
deleteDeptButton.setPrefWidth(100);
searchDeptButton.setPrefWidth(100);
// 添加事件处理器
addEmployeeButton.setOnAction(event -> {
// 添加员工
});
updateEmployeeButton.setOnAction(event -> {
// 修改员工
});
deleteEmployeeButton.setOnAction(event -> {
// 删除员工
});
searchEmployeeButton.setOnAction(event -> {
// 查询员工
});
addDeptButton.setOnAction(event -> {
// 添加部门
});
updateDeptButton.setOnAction(event -> {
// 修改部门
});
deleteDeptButton.setOnAction(event -> {
// 删除部门
});
searchDeptButton.setOnAction(event -> {
// 查询部门
});
// 创建布局
GridPane employeeGrid = new GridPane();
employeeGrid.setAlignment(Pos.CENTER);
employeeGrid.setHgap(10);
employeeGrid.setVgap(10);
employeeGrid.setPadding(new Insets(25, 25, 25, 25));
employeeGrid.add(new Label("员工信息"), 0, 0, 2, 1);
employeeGrid.add(new Label("员工编号:"), 0, 1);
employeeGrid.add(employeeIdField, 1, 1);
employeeGrid.add(new Label("所在部门编号:"), 0, 2);
employeeGrid.add(employeeDeptIdField, 1, 2);
employeeGrid.add(new Label("职务:"), 0, 3);
employeeGrid.add(employeePositionField, 1, 3);
employeeGrid.add(new Label("薪资等级:"), 0, 4);
employeeGrid.add(employeeSalaryGradeField, 1, 4);
employeeGrid.add(new Label("薪资:"), 0, 5);
employeeGrid.add(employeeSalaryField, 1, 5);
employeeGrid.add(new Label("联系方式:"), 0, 6);
employeeGrid.add(employeePhoneField, 1, 6);
HBox employeeButtonBox = new HBox(10);
employeeButtonBox.setAlignment(Pos.CENTER);
employeeButtonBox.getChildren().addAll(addEmployeeButton, updateEmployeeButton, deleteEmployeeButton, searchEmployeeButton);
employeeGrid.add(employeeButtonBox, 0, 7, 2, 1);
GridPane deptGrid = new GridPane();
deptGrid.setAlignment(Pos.CENTER);
deptGrid.setHgap(10);
deptGrid.setVgap(10);
deptGrid.setPadding(new Insets(25, 25, 25, 25));
deptGrid.add(new Label("部门信息"), 0, 0, 2, 1);
deptGrid.add(new Label("部门编号:"), 0, 1);
deptGrid.add(deptIdField, 1, 1);
deptGrid.add(new Label("部门名称:"), 0, 2);
deptGrid.add(deptNameField, 1, 2);
HBox deptButtonBox = new HBox(10);
deptButtonBox.setAlignment(Pos.CENTER);
deptButtonBox.getChildren().addAll(addDeptButton, updateDeptButton, deleteDeptButton, searchDeptButton);
deptGrid.add(deptButtonBox, 0, 3, 2, 1);
VBox root = new VBox(20);
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(employeeGrid, deptGrid);
// 创建场景
Scene scene = new Scene(root, 600, 400);
// 设置舞台
primaryStage.setTitle("人事管理");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
以上代码演示了如何创建一个简单的JavaFX界面,并添加员工和部门的增删改查功能。其他操作也可以通过类似的方式实现。
希望这些代码对你有帮助!
阅读全文