员管理系统 java_员工管理系统java版
时间: 2023-10-18 14:16:23 浏览: 106
员工管理系统是一个比较常见的管理软件,可以帮助企业和组织对员工进行管理。Java语言是一种非常流行的编程语言,也可以用来开发员工管理系统。
在Java中,可以使用Swing或JavaFX等GUI库来创建用户界面,使用JDBC或ORM框架来连接数据库,实现数据的存储和读取。以下是一个简单的员工管理系统的Java代码示例:
```java
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class EmployeeManagementSystem extends JFrame implements ActionListener {
private JLabel titleLabel, nameLabel, ageLabel, genderLabel, positionLabel;
private JTextField nameField, ageField, positionField;
private JRadioButton maleRadioButton, femaleRadioButton;
private ButtonGroup genderButtonGroup;
private JButton addButton, updateButton, deleteButton, clearButton;
private JList employeeList;
private DefaultListModel employeeListModel;
private Connection conn;
private Statement stmt;
private ResultSet rs;
public EmployeeManagementSystem() {
setTitle("Employee Management System");
// Initialize GUI components
titleLabel = new JLabel("Employee Management System");
nameLabel = new JLabel("Name:");
ageLabel = new JLabel("Age:");
genderLabel = new JLabel("Gender:");
positionLabel = new JLabel("Position:");
nameField = new JTextField(20);
ageField = new JTextField(3);
positionField = new JTextField(20);
maleRadioButton = new JRadioButton("Male", true);
femaleRadioButton = new JRadioButton("Female", false);
genderButtonGroup = new ButtonGroup();
genderButtonGroup.add(maleRadioButton);
genderButtonGroup.add(femaleRadioButton);
addButton = new JButton("Add");
updateButton = new JButton("Update");
deleteButton = new JButton("Delete");
clearButton = new JButton("Clear");
employeeListModel = new DefaultListModel();
employeeList = new JList(employeeListModel);
// Add GUI components to the frame
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(10, 0, 10, 0);
add(titleLabel, gbc);
gbc.gridy++;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets(5, 5, 5, 5);
add(nameLabel, gbc);
gbc.gridx++;
gbc.anchor = GridBagConstraints.WEST;
add(nameField, gbc);
gbc.gridy++;
gbc.gridx = 0;
gbc.anchor = GridBagConstraints.EAST;
add(ageLabel, gbc);
gbc.gridx++;
gbc.anchor = GridBagConstraints.WEST;
add(ageField, gbc);
gbc.gridy++;
gbc.gridx = 0;
gbc.anchor = GridBagConstraints.EAST;
add(genderLabel, gbc);
gbc.gridx++;
gbc.anchor = GridBagConstraints.WEST;
add(maleRadioButton, gbc);
gbc.gridx++;
add(femaleRadioButton, gbc);
gbc.gridy++;
gbc.gridx = 0;
gbc.anchor = GridBagConstraints.EAST;
add(positionLabel, gbc);
gbc.gridx++;
gbc.anchor = GridBagConstraints.WEST;
add(positionField, gbc);
gbc.gridy++;
gbc.gridx = 0;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(10, 0, 10, 0);
add(addButton, gbc);
gbc.gridx++;
add(updateButton, gbc);
gbc.gridy++;
gbc.gridx = 0;
add(deleteButton, gbc);
gbc.gridx++;
add(clearButton, gbc);
gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.insets = new Insets(10, 5, 5, 5);
add(new JScrollPane(employeeList), gbc);
// Add action listeners to the buttons
addButton.addActionListener(this);
updateButton.addActionListener(this);
deleteButton.addActionListener(this);
clearButton.addActionListener(this);
// Connect to the database
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/employee", "root", "");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM employees");
while (rs.next()) {
employeeListModel.addElement(rs.getString("name"));
}
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
setSize(500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
String gender = maleRadioButton.isSelected() ? "Male" : "Female";
String position = positionField.getText();
employeeListModel.addElement(name);
// Insert the new employee into the database
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/employee", "root", "");
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO employees (name, age, gender, position) VALUES (?, ?, ?, ?)");
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setString(3, gender);
pstmt.setString(4, position);
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == updateButton) {
int selectedIndex = employeeList.getSelectedIndex();
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
String gender = maleRadioButton.isSelected() ? "Male" : "Female";
String position = positionField.getText();
employeeListModel.setElementAt(name, selectedIndex);
// Update the employee in the database
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/employee", "root", "");
PreparedStatement pstmt = conn.prepareStatement("UPDATE employees SET name=?, age=?, gender=?, position=? WHERE name=?");
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setString(3, gender);
pstmt.setString(4, position);
pstmt.setString(5, (String) employeeListModel.getElementAt(selectedIndex));
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == deleteButton) {
int selectedIndex = employeeList.getSelectedIndex();
employeeListModel.removeElementAt(selectedIndex);
// Delete the employee from the database
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/employee", "root", "");
PreparedStatement pstmt = conn.prepareStatement("DELETE FROM employees WHERE name=?");
pstmt.setString(1, (String) employeeListModel.getElementAt(selectedIndex));
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == clearButton) {
nameField.setText("");
ageField.setText("");
maleRadioButton.setSelected(true);
positionField.setText("");
employeeList.clearSelection();
}
}
public static void main(String[] args) {
new EmployeeManagementSystem();
}
}
```
这个员工管理系统使用了MySQL数据库来存储员工的信息。运行程序后,可以添加、更新、删除和清空员工信息,同时也可以从数据库中读取已有的员工信息并显示在界面上。
阅读全文