用java写一个人员信息修改里面包含编号,姓名,性别,部门,职位,其他以上都需带一个文本框进行信息输入,拥有人员编号查询,修改,清空和退出的功能按钮,并实现其功能。其中人员编号查询需要弹窗出显示输入人员编号进行查询,查询需要查询数据库表。修改也需要查询数据库表。代码帮我整合在一起,并且帮我添加一下连接数据库的代码
时间: 2023-11-28 10:05:49 浏览: 50
好的,以下是整合了连接数据库代码的Java人员信息修改程序:
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class PersonInfo extends JFrame implements ActionListener {
JLabel label1, label2, label3, label4, label5, label6;
JTextField text1, text2, text3, text4, text5, text6;
JButton btn1, btn2, btn3, btn4;
// 数据库连接相关变量
Connection conn;
Statement stmt;
ResultSet rs;
public PersonInfo() {
// 初始化界面
label1 = new JLabel("编号:");
label2 = new JLabel("姓名:");
label3 = new JLabel("性别:");
label4 = new JLabel("部门:");
label5 = new JLabel("职位:");
label6 = new JLabel("其他:");
text1 = new JTextField(10);
text2 = new JTextField(10);
text3 = new JTextField(10);
text4 = new JTextField(10);
text5 = new JTextField(10);
text6 = new JTextField(10);
btn1 = new JButton("查询");
btn2 = new JButton("修改");
btn3 = new JButton("清空");
btn4 = new JButton("退出");
JPanel panel1 = new JPanel(new GridLayout(6, 2));
panel1.add(label1);
panel1.add(text1);
panel1.add(label2);
panel1.add(text2);
panel1.add(label3);
panel1.add(text3);
panel1.add(label4);
panel1.add(text4);
panel1.add(label5);
panel1.add(text5);
panel1.add(label6);
panel1.add(text6);
JPanel panel2 = new JPanel(new FlowLayout());
panel2.add(btn1);
panel2.add(btn2);
panel2.add(btn3);
panel2.add(btn4);
setLayout(new BorderLayout());
add(panel1, BorderLayout.CENTER);
add(panel2, BorderLayout.SOUTH);
// 添加按钮监听器
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
stmt = conn.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
setTitle("人员信息修改");
setSize(300, 250);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("查询")) {
String id = text1.getText();
try {
rs = stmt.executeQuery("SELECT * FROM person WHERE id='" + id + "'");
if (rs.next()) {
text2.setText(rs.getString("name"));
text3.setText(rs.getString("sex"));
text4.setText(rs.getString("dept"));
text5.setText(rs.getString("title"));
text6.setText(rs.getString("others"));
} else {
JOptionPane.showMessageDialog(null, "未找到编号为" + id + "的人员信息");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (cmd.equals("修改")) {
String id = text1.getText();
String name = text2.getText();
String sex = text3.getText();
String dept = text4.getText();
String title = text5.getText();
String others = text6.getText();
try {
int rows = stmt.executeUpdate("UPDATE person SET name='" + name + "',sex='" + sex + "',dept='" + dept + "',title='" + title + "',others='" + others + "' WHERE id='" + id + "'");
if (rows > 0) {
JOptionPane.showMessageDialog(null, "修改成功");
} else {
JOptionPane.showMessageDialog(null, "未找到编号为" + id + "的人员信息");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (cmd.equals("清空")) {
text1.setText("");
text2.setText("");
text3.setText("");
text4.setText("");
text5.setText("");
text6.setText("");
} else if (cmd.equals("退出")) {
try {
stmt.close();
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
System.exit(0);
}
}
public static void main(String[] args) {
new PersonInfo();
}
}
```
其中,连接MySQL数据库的代码如下:
```java
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
stmt = conn.createStatement();
```
其中,`com.mysql.jdbc.Driver` 是 MySQL JDBC 驱动程序的类名,`localhost:3306` 是 MySQL 服务器的地址和端口,`test` 是数据库名,`root` 和 `password` 分别是数据库的用户名和密码。请根据自己的实际情况修改这些参数。
阅读全文