用java写一个人员信息修改里面包含编号,姓名,性别,部门,职位,其他以上都需带一个文本框进行信息输入,拥有人员编号查询,修改,清空和退出的功能按钮,并实现其功能。其中人员编号查询需要弹窗出显示输入人员编号进行查询,查询需要查询数据库表。修改也需要查询数据库表。其中查询数据库表表名为personnel_info,代码帮我整合在一起,。
时间: 2023-11-19 19:05:27 浏览: 38
好的,以下是整合好的Java代码,包括GUI界面和数据库操作。请注意,本代码使用了Java Swing库和MySQL数据库。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class PersonnelInfoModify implements ActionListener {
private JFrame frame;
private JTextField numField, nameField, genderField, deptField, posField, otherField;
private JButton searchBtn, modifyBtn, clearBtn, exitBtn;
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
public PersonnelInfoModify() {
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
// 创建GUI界面
frame = new JFrame("Personnel Info Modify");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(7, 2, 10, 10));
JLabel numLabel = new JLabel("Number:");
numField = new JTextField(10);
mainPanel.add(numLabel);
mainPanel.add(numField);
JLabel nameLabel = new JLabel("Name:");
nameField = new JTextField(10);
mainPanel.add(nameLabel);
mainPanel.add(nameField);
JLabel genderLabel = new JLabel("Gender:");
genderField = new JTextField(10);
mainPanel.add(genderLabel);
mainPanel.add(genderField);
JLabel deptLabel = new JLabel("Department:");
deptField = new JTextField(10);
mainPanel.add(deptLabel);
mainPanel.add(deptField);
JLabel posLabel = new JLabel("Position:");
posField = new JTextField(10);
mainPanel.add(posLabel);
mainPanel.add(posField);
JLabel otherLabel = new JLabel("Other:");
otherField = new JTextField(10);
mainPanel.add(otherLabel);
mainPanel.add(otherField);
searchBtn = new JButton("Search");
searchBtn.addActionListener(this);
mainPanel.add(searchBtn);
modifyBtn = new JButton("Modify");
modifyBtn.addActionListener(this);
mainPanel.add(modifyBtn);
clearBtn = new JButton("Clear");
clearBtn.addActionListener(this);
mainPanel.add(clearBtn);
exitBtn = new JButton("Exit");
exitBtn.addActionListener(this);
mainPanel.add(exitBtn);
frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == searchBtn) {
search();
} else if (e.getSource() == modifyBtn) {
modify();
} else if (e.getSource() == clearBtn) {
clear();
} else if (e.getSource() == exitBtn) {
exit();
}
}
private void search() {
String num = numField.getText();
try {
pstmt = conn.prepareStatement("SELECT * FROM personnel_info WHERE num=?");
pstmt.setString(1, num);
rs = pstmt.executeQuery();
if (rs.next()) {
nameField.setText(rs.getString("name"));
genderField.setText(rs.getString("gender"));
deptField.setText(rs.getString("dept"));
posField.setText(rs.getString("pos"));
otherField.setText(rs.getString("other"));
} else {
JOptionPane.showMessageDialog(frame, "No personnel found with the given number.");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void modify() {
String num = numField.getText();
String name = nameField.getText();
String gender = genderField.getText();
String dept = deptField.getText();
String pos = posField.getText();
String other = otherField.getText();
try {
pstmt = conn.prepareStatement("UPDATE personnel_info SET name=?, gender=?, dept=?, pos=?, other=? WHERE num=?");
pstmt.setString(1, name);
pstmt.setString(2, gender);
pstmt.setString(3, dept);
pstmt.setString(4, pos);
pstmt.setString(5, other);
pstmt.setString(6, num);
int rows = pstmt.executeUpdate();
if (rows > 0) {
JOptionPane.showMessageDialog(frame, "Personnel information has been updated successfully.");
} else {
JOptionPane.showMessageDialog(frame, "Failed to update personnel information.");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void clear() {
numField.setText("");
nameField.setText("");
genderField.setText("");
deptField.setText("");
posField.setText("");
otherField.setText("");
}
private void exit() {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
System.exit(0);
}
public static void main(String[] args) {
new PersonnelInfoModify();
}
}
```
请注意,在运行此代码之前,您需要先创建一个名为"personnel_info"的MySQL数据库表,并添加以下列:
- num(人员编号)
- name(姓名)
- gender(性别)
- dept(部门)
- pos(职位)
- other(其他)
此外,您需要将MySQL连接字符串中的用户名和密码更改为您自己的用户名和密码。
如果您在运行代码时遇到任何问题,请告诉我。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)