java写一个连接数据库中商品库存管理中查询功能的页面(中文注释)
时间: 2024-03-25 17:37:23 浏览: 21
下面是一个简单的Java商品库存管理系统的查询功能页面的示例代码,包括连接数据库和中文注释:
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class ProductSearch extends JFrame implements ActionListener {
// 定义GUI组件
JLabel nameLabel, categoryLabel;
JTextField nameField, categoryField;
JButton searchButton, resetButton;
JTable resultTable;
// 定义数据库连接相关变量
Connection conn;
Statement stmt;
ResultSet rs;
public ProductSearch() {
// 初始化GUI组件
nameLabel = new JLabel("商品名称:");
nameField = new JTextField(20);
categoryLabel = new JLabel("商品类别:");
categoryField = new JTextField(20);
searchButton = new JButton("查询");
resetButton = new JButton("重置");
resultTable = new JTable();
// 设置GUI布局
JPanel inputPanel = new JPanel(new GridLayout(2, 2));
inputPanel.add(nameLabel);
inputPanel.add(nameField);
inputPanel.add(categoryLabel);
inputPanel.add(categoryField);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(searchButton);
buttonPanel.add(resetButton);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(inputPanel, BorderLayout.NORTH);
mainPanel.add(buttonPanel, BorderLayout.CENTER);
mainPanel.add(new JScrollPane(resultTable), BorderLayout.SOUTH);
// 添加事件监听器
searchButton.addActionListener(this);
resetButton.addActionListener(this);
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/inventory", "root", "password");
stmt = conn.createStatement();
} catch (Exception ex) {
ex.printStackTrace();
}
// 设置窗口属性
setTitle("商品查询");
setContentPane(mainPanel);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
// 处理按钮点击事件
public void actionPerformed(ActionEvent e) {
if (e.getSource() == searchButton) {
// 执行查询操作
String name = nameField.getText();
String category = categoryField.getText();
try {
String sql = "SELECT * FROM products WHERE name LIKE '%" + name + "%' AND category LIKE '%" + category + "%'";
rs = stmt.executeQuery(sql);
resultTable.setModel(buildTableModel(rs));
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == resetButton) {
// 重置查询条件
nameField.setText("");
categoryField.setText("");
resultTable.setModel(new DefaultTableModel());
}
}
// 将ResultSet转换成TableModel
public static DefaultTableModel buildTableModel(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// 获取列数
int columnCount = metaData.getColumnCount();
// 获取列名
String[] columnNames = new String[columnCount];
for (int i = 1; i <= columnCount; i++) {
columnNames[i - 1] = metaData.getColumnName(i);
}
// 获取行数据
Object[][] data = new Object[100][columnCount];
int rowCount = 0;
while (rs.next()) {
Object[] row = new Object[columnCount];
for (int i = 1; i <= columnCount; i++) {
row[i - 1] = rs.getObject(i);
}
data[rowCount++] = row;
}
return new DefaultTableModel(data, columnNames);
}
public static void main(String[] args) {
new ProductSearch();
}
}
```
这段代码在前面的示例基础上增加了连接数据库的功能。首先在构造函数中连接数据库,然后在查询按钮的事件处理方法中执行查询操作,将查询结果转换成TableModel并显示在表格中。注意,这里的查询操作需要根据实际的数据库表结构和查询条件进行调整。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)