java jtable 显示数据库数据,点击查询后更新数据显示
时间: 2024-03-27 07:35:02 浏览: 35
实现这个功能的大致思路如下:
1. 创建一个 JTable 对象用来显示数据库中的数据;
2. 创建一个 JButton 对象用来触发查询操作;
3. 在 JButton 的 ActionListener 中编写查询数据库并更新 JTable 的代码。
下面是一份示例代码,可以作为参考:
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class JTableDemo extends JFrame implements ActionListener {
private JButton queryButton;
private JTable table;
private DefaultTableModel tableModel;
private Connection conn;
private PreparedStatement stmt;
public JTableDemo() {
super("JTable Demo");
// 初始化按钮
queryButton = new JButton("查询");
queryButton.addActionListener(this);
// 初始化表格
tableModel = new DefaultTableModel();
table = new JTable(tableModel);
// 添加组件
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.add(queryButton, BorderLayout.NORTH);
contentPane.add(new JScrollPane(table), BorderLayout.CENTER);
setContentPane(contentPane);
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
} catch (Exception e) {
e.printStackTrace();
}
// 显示窗口
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == queryButton) {
// 查询数据库
try {
stmt = conn.prepareStatement("SELECT * FROM student");
ResultSet rs = stmt.executeQuery();
// 清空表格数据
tableModel.setRowCount(0);
// 更新表格数据
while (rs.next()) {
Object[] row = new Object[3];
row[0] = rs.getString("id");
row[1] = rs.getString("name");
row[2] = rs.getString("age");
tableModel.addRow(row);
}
rs.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new JTableDemo();
}
}
```
这份代码中,我们在 actionPerformed 方法中编写了查询数据库并更新 JTable 的代码。当用户点击查询按钮时,程序会执行 SQL 语句获取数据库中的数据,并将数据显示在 JTable 中。需要注意的是,每次查询之前需要清空表格数据,否则会导致重复显示。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)