用java Swing设计一个有增删改查的页面,数据库随便
时间: 2024-03-18 08:39:53 浏览: 62
使用Java Swing界面实现数据库基本操作
4星 · 用户满意度95%
好的,下面是一个简单的Java Swing界面,其中包括增加、删除、修改和查询数据的功能。数据库使用的是SQLite。
首先,需要在项目中引入SQLite JDBC驱动,然后创建一个SQLite数据库。在本例中,我们创建一个名为"example.db"的数据库,其中包含一个名为"users"的表格,包含三个字段:id、name和age。
下面是完整的Java代码:
```java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class DatabaseGUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
// 数据库连接相关
private final String DB_URL = "jdbc:sqlite:example.db";
private Connection conn;
// GUI组件
private JPanel panelTop, panelBottom;
private JLabel labelName, labelAge;
private JTextField textName, textAge;
private JButton buttonAdd, buttonDelete, buttonUpdate, buttonQuery;
private JTable table;
private DefaultTableModel tableModel;
public DatabaseGUI() {
// 初始化界面
setTitle("Database GUI");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// 连接数据库
try {
conn = DriverManager.getConnection(DB_URL);
System.out.println("Database connected.");
} catch (SQLException e) {
System.err.println("Database connection failed: " + e.getMessage());
}
// 创建GUI组件
panelTop = new JPanel();
labelName = new JLabel("Name:");
textName = new JTextField(20);
labelAge = new JLabel("Age:");
textAge = new JTextField(10);
buttonAdd = new JButton("Add");
buttonDelete = new JButton("Delete");
buttonUpdate = new JButton("Update");
buttonQuery = new JButton("Query");
panelTop.add(labelName);
panelTop.add(textName);
panelTop.add(labelAge);
panelTop.add(textAge);
panelTop.add(buttonAdd);
panelTop.add(buttonDelete);
panelTop.add(buttonUpdate);
panelTop.add(buttonQuery);
add(panelTop, BorderLayout.NORTH);
panelBottom = new JPanel();
tableModel = new DefaultTableModel();
tableModel.addColumn("ID");
tableModel.addColumn("Name");
tableModel.addColumn("Age");
table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
panelBottom.add(scrollPane);
add(panelBottom, BorderLayout.CENTER);
// 注册事件监听器
buttonAdd.addActionListener(this);
buttonDelete.addActionListener(this);
buttonUpdate.addActionListener(this);
buttonQuery.addActionListener(this);
// 显示界面
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonAdd) {
// 添加数据
String name = textName.getText();
int age = Integer.parseInt(textAge.getText());
try {
PreparedStatement stmt = conn.prepareStatement("INSERT INTO users(name, age) VALUES(?, ?)");
stmt.setString(1, name);
stmt.setInt(2, age);
stmt.executeUpdate();
stmt.close();
textName.setText("");
textAge.setText("");
JOptionPane.showMessageDialog(this, "Added successfully.");
} catch (SQLException ex) {
System.err.println("SQL error: " + ex.getMessage());
}
} else if (e.getSource() == buttonDelete) {
// 删除数据
int row = table.getSelectedRow();
if (row != -1) {
int id = (int) tableModel.getValueAt(row, 0);
try {
PreparedStatement stmt = conn.prepareStatement("DELETE FROM users WHERE id = ?");
stmt.setInt(1, id);
stmt.executeUpdate();
stmt.close();
tableModel.removeRow(row);
JOptionPane.showMessageDialog(this, "Deleted successfully.");
} catch (SQLException ex) {
System.err.println("SQL error: " + ex.getMessage());
}
} else {
JOptionPane.showMessageDialog(this, "Please select a row to delete.");
}
} else if (e.getSource() == buttonUpdate) {
// 修改数据
int row = table.getSelectedRow();
if (row != -1) {
int id = (int) tableModel.getValueAt(row, 0);
String name = (String) tableModel.getValueAt(row, 1);
int age = (int) tableModel.getValueAt(row, 2);
name = JOptionPane.showInputDialog(this, "Name:", name);
age = Integer.parseInt(JOptionPane.showInputDialog(this, "Age:", age));
try {
PreparedStatement stmt = conn.prepareStatement("UPDATE users SET name = ?, age = ? WHERE id = ?");
stmt.setString(1, name);
stmt.setInt(2, age);
stmt.setInt(3, id);
stmt.executeUpdate();
stmt.close();
tableModel.setValueAt(name, row, 1);
tableModel.setValueAt(age, row, 2);
JOptionPane.showMessageDialog(this, "Updated successfully.");
} catch (SQLException ex) {
System.err.println("SQL error: " + ex.getMessage());
}
} else {
JOptionPane.showMessageDialog(this, "Please select a row to update.");
}
} else if (e.getSource() == buttonQuery) {
// 查询数据
try {
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM users");
ResultSet rs = stmt.executeQuery();
tableModel.setRowCount(0);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
Object[] row = { id, name, age };
tableModel.addRow(row);
}
rs.close();
stmt.close();
} catch (SQLException ex) {
System.err.println("SQL error: " + ex.getMessage());
}
}
}
public static void main(String[] args) {
new DatabaseGUI();
}
}
```
运行程序后,可以看到一个界面,其中包括一个表格和一些按钮。在"Name"和"Age"文本框中输入新的数据,然后点击"Add"按钮可以将数据添加到数据库中。选中表格中的一行数据,然后点击"Delete"按钮可以将该行数据从数据库中删除。同样地,选中表格中的一行数据,然后点击"Update"按钮可以修改该行数据。最后,点击"Query"按钮可以查询数据库中的所有数据并在表格中显示。
请注意,本示例程序的代码只是为了演示用途,实际开发中可能需要进行更多的错误处理和输入验证。
阅读全文