请用java联系数据库test中的表employee,编号对应数据表中id锁定编号文本框,姓名对应数据表中的name,性别对应数据表中的gender,部门对应数据表中的deoartment,职位对应数据表中的position,点击查询按钮可以查询表中的所有数据,再点击某个数据点击删除按钮可以进行删除这个数据
时间: 2024-03-23 11:38:03 浏览: 59
好的,以下是一个简单的Java Swing界面程序,可以连接MySQL数据库中的test库中的employee表,并实现查询和删除功能。
在使用前,需要先修改代码中的数据库连接地址、用户名和密码,以及MySQL JDBC驱动的路径。同时,需要在MySQL中创建test数据库和employee表。
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
public class EmployeeManagementSystem extends JFrame implements ActionListener {
private JTextField idField, nameField, genderField, departmentField, positionField;
private JButton queryButton, deleteButton;
private JTable table;
private Connection conn;
public EmployeeManagementSystem() {
super("员工管理系统");
setSize(600, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 初始化界面组件
idField = new JTextField(10);
nameField = new JTextField(10);
genderField = new JTextField(10);
departmentField = new JTextField(10);
positionField = new JTextField(10);
queryButton = new JButton("查询");
deleteButton = new JButton("删除");
table = new JTable(new DefaultTableModel());
// 设置界面布局
JPanel inputPanel = new JPanel(new GridLayout(5, 2, 10, 10));
inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
inputPanel.add(new JLabel("编号:"));
inputPanel.add(idField);
inputPanel.add(new JLabel("姓名:"));
inputPanel.add(nameField);
inputPanel.add(new JLabel("性别:"));
inputPanel.add(genderField);
inputPanel.add(new JLabel("部门:"));
inputPanel.add(departmentField);
inputPanel.add(new JLabel("职位:"));
inputPanel.add(positionField);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
buttonPanel.add(queryButton);
buttonPanel.add(deleteButton);
JScrollPane tableScrollPane = new JScrollPane(table);
setLayout(new BorderLayout());
add(inputPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
add(tableScrollPane, BorderLayout.SOUTH);
// 添加事件监听器
queryButton.addActionListener(this);
deleteButton.addActionListener(this);
// 连接数据库
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost/test?serverTimezone=UTC";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "无法连接数据库:" + e.getMessage());
System.exit(0);
}
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == queryButton) {
// 查询数据
String sql = "SELECT * FROM employee";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
DefaultTableModel model = new DefaultTableModel();
model.addColumn("编号");
model.addColumn("姓名");
model.addColumn("性别");
model.addColumn("部门");
model.addColumn("职位");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String gender = rs.getString("gender");
String department = rs.getString("department");
String position = rs.getString("position");
Object[] row = new Object[] { id, name, gender, department, position };
model.addRow(row);
}
table.setModel(model);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "查询数据出错:" + e.getMessage());
}
} else if (event.getSource() == deleteButton) {
// 删除数据
int row = table.getSelectedRow();
if (row < 0) {
JOptionPane.showMessageDialog(this, "请选择要删除的数据");
return;
}
int id = (int) table.getValueAt(row, 0);
String sql = "DELETE FROM employee WHERE id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(this, "删除成功");
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "删除数据出错:" + e.getMessage());
}
}
}
public static void main(String[] args) {
new EmployeeManagementSystem().setVisible(true);
}
}
```
运行程序后,界面上会显示一个文本框和一个查询按钮,点击查询按钮可以查询表中的所有数据,并显示在界面下方的表格中。同时,每一行数据后面都会有一个删除按钮,点击删除按钮可以删除对应的数据。
阅读全文