java swing 查询数据库显示在面板里代码示例
时间: 2024-03-23 21:38:22 浏览: 60
java swing实例
4星 · 用户满意度95%
以下是一个简单的 Java Swing 应用程序,它可以查询数据库并将结果显示在面板中:
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.border.*;
public class DatabaseViewer extends JFrame implements ActionListener {
private static final String DB_URL = "jdbc:mysql://localhost:3306/mydatabase";
private static final String DB_USER = "root";
private static final String DB_PASS = "password";
private JLabel lblName, lblAge, lblAddress;
private JTextField tfName, tfAge, tfAddress;
private JButton btnSearch;
private JTextArea taResult;
public DatabaseViewer() {
super("Database Viewer");
// Create UI components
lblName = new JLabel("Name:");
lblAge = new JLabel("Age:");
lblAddress = new JLabel("Address:");
tfName = new JTextField(20);
tfAge = new JTextField(20);
tfAddress = new JTextField(20);
btnSearch = new JButton("Search");
btnSearch.addActionListener(this);
taResult = new JTextArea(10, 30);
taResult.setEditable(false);
taResult.setBorder(new LineBorder(Color.GRAY));
// Create layout
JPanel pInput = new JPanel(new GridLayout(3, 2, 5, 5));
pInput.add(lblName);
pInput.add(tfName);
pInput.add(lblAge);
pInput.add(tfAge);
pInput.add(lblAddress);
pInput.add(tfAddress);
JPanel pButton = new JPanel();
pButton.add(btnSearch);
JPanel pResult = new JPanel(new BorderLayout());
pResult.add(new JScrollPane(taResult), BorderLayout.CENTER);
pResult.setBorder(new TitledBorder("Search Result"));
JPanel pMain = new JPanel(new BorderLayout());
pMain.add(pInput, BorderLayout.NORTH);
pMain.add(pButton, BorderLayout.CENTER);
pMain.add(pResult, BorderLayout.SOUTH);
// Set frame properties
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setContentPane(pMain);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnSearch) {
String name = tfName.getText().trim();
String age = tfAge.getText().trim();
String address = tfAddress.getText().trim();
searchDatabase(name, age, address);
}
}
private void searchDatabase(String name, String age, String address) {
try {
// Load the driver class
Class.forName("com.mysql.jdbc.Driver");
// Establish the connection
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
// Create the SQL statement
String sql = "SELECT * FROM people WHERE 1=1";
if (!name.isEmpty()) {
sql += " AND name LIKE '%" + name + "%'";
}
if (!age.isEmpty()) {
sql += " AND age = " + age;
}
if (!address.isEmpty()) {
sql += " AND address LIKE '%" + address + "%'";
}
// Execute the query
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
// Process the results
taResult.setText("");
while (rs.next()) {
String result = "Name: " + rs.getString("name") + "\n" +
"Age: " + rs.getInt("age") + "\n" +
"Address: " + rs.getString("address") + "\n\n";
taResult.append(result);
}
// Close the resources
rs.close();
stmt.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
new DatabaseViewer();
}
}
```
这个应用程序使用了 MySQL 数据库,你需要将 `DB_URL`、`DB_USER` 和 `DB_PASS` 替换为你自己的数据库连接信息。在 `searchDatabase` 方法中,我们使用 JDBC API 执行 SQL 查询,并处理结果集以在文本区域中显示查询结果。注意,我们在 SQL 语句中使用了 `LIKE` 操作符来模糊搜索名字和地址。
阅读全文