设计一个基于Java swing和MySQL的分类查询功能代码
时间: 2024-02-25 20:52:24 浏览: 67
基于Java+Swing+mysql实现垃圾分类管理系统
5星 · 资源好评率100%
好的,以下是一个简单的Java Swing和MySQL分类查询代码示例:
```java
import java.sql.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CategoryQuery extends JFrame {
private JPanel contentPane;
private JTextField searchField;
private JComboBox<String> categoryComboBox;
private JTextArea resultArea;
private Connection conn;
public CategoryQuery() {
// 设置窗口标题
setTitle("分类查询");
try {
// 加载MySQL驱动
Class.forName("com.mysql.jdbc.Driver");
// 连接MySQL数据库
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
// 设置窗口大小
setSize(500, 400);
// 居中显示窗口
setLocationRelativeTo(null);
// 关闭窗口时退出程序
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建内容面板
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
// 创建查询面板
JPanel queryPanel = new JPanel();
contentPane.add(queryPanel, BorderLayout.NORTH);
JLabel categoryLabel = new JLabel("分类:");
queryPanel.add(categoryLabel);
// 创建分类下拉框
categoryComboBox = new JComboBox<String>();
queryPanel.add(categoryComboBox);
try {
// 查询分类数据
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT DISTINCT category FROM products");
while (rs.next()) {
categoryComboBox.addItem(rs.getString("category"));
}
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
JLabel searchLabel = new JLabel("关键字:");
queryPanel.add(searchLabel);
// 创建搜索框
searchField = new JTextField();
queryPanel.add(searchField);
searchField.setColumns(20);
// 创建查询按钮
JButton queryButton = new JButton("查询");
queryButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 获取用户选择的分类和搜索关键字
String category = (String) categoryComboBox.getSelectedItem();
String keyword = searchField.getText().trim();
try {
// 查询数据
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT name, price FROM products WHERE category = '" + category + "' AND name LIKE '%" + keyword + "%'");
StringBuilder sb = new StringBuilder();
while (rs.next()) {
sb.append(rs.getString("name")).append("\t").append(rs.getDouble("price")).append("\n");
}
rs.close();
stmt.close();
// 显示查询结果
resultArea.setText(sb.toString());
} catch (SQLException ex) {
ex.printStackTrace();
}
}
});
queryPanel.add(queryButton);
// 创建结果面板
JPanel resultPanel = new JPanel();
contentPane.add(resultPanel, BorderLayout.CENTER);
resultPanel.setLayout(new BorderLayout(0, 0));
JLabel resultLabel = new JLabel("查询结果:");
resultPanel.add(resultLabel, BorderLayout.NORTH);
// 创建结果文本框
resultArea = new JTextArea();
resultPanel.add(resultArea, BorderLayout.CENTER);
}
public static void main(String[] args) {
CategoryQuery frame = new CategoryQuery();
frame.setVisible(true);
}
}
```
这个程序包括一个查询面板和一个结果面板。查询面板中包括一个分类下拉框、一个搜索框和一个查询按钮,用户可以选择分类并输入关键字进行查询。结果面板中显示查询结果。程序使用了MySQL数据库,需要事先在本地安装和配置好MySQL。
阅读全文