请用Java编程:利用GUI界面实现数据库数据的CRUD增查改删功能
时间: 2024-03-20 12:44:37 浏览: 48
好的,以下是一个简单的Java程序,可以利用GUI界面实现数据库数据的CRUD增查改删功能。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class CRUD_GUI extends JFrame implements ActionListener {
private JTextField tfID, tfName, tfAge;
private JButton btnAdd, btnSearch, btnUpdate, btnDelete;
private JTextArea taOutput;
// 数据库连接信息
private String url = "jdbc:mysql://localhost:3306/test";
private String user = "root";
private String password = "123456";
private Connection conn;
public CRUD_GUI() {
super("CRUD GUI");
// 创建GUI界面元素
tfID = new JTextField(10);
tfName = new JTextField(10);
tfAge = new JTextField(10);
btnAdd = new JButton("Add");
btnSearch = new JButton("Search");
btnUpdate = new JButton("Update");
btnDelete = new JButton("Delete");
taOutput = new JTextArea(10, 30);
// 添加事件监听器
btnAdd.addActionListener(this);
btnSearch.addActionListener(this);
btnUpdate.addActionListener(this);
btnDelete.addActionListener(this);
// 创建面板
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 2));
panel.add(new JLabel("ID:"));
panel.add(tfID);
panel.add(new JLabel("Name:"));
panel.add(tfName);
panel.add(new JLabel("Age:"));
panel.add(tfAge);
panel.add(btnAdd);
panel.add(btnSearch);
panel.add(btnUpdate);
panel.add(btnDelete);
// 添加面板和文本区域到窗口中
Container contentPane = getContentPane();
contentPane.add(panel, BorderLayout.NORTH);
contentPane.add(new JScrollPane(taOutput), BorderLayout.CENTER);
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
// 处理按钮事件
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnAdd) {
addRecord();
} else if (e.getSource() == btnSearch) {
searchRecord();
} else if (e.getSource() == btnUpdate) {
updateRecord();
} else if (e.getSource() == btnDelete) {
deleteRecord();
}
}
// 添加记录到数据库中
private void addRecord() {
String id = tfID.getText().trim();
String name = tfName.getText().trim();
String age = tfAge.getText().trim();
try {
Statement stmt = conn.createStatement();
String sql = "INSERT INTO person (id, name, age) VALUES ('" + id + "', '" + name + "', '" + age + "')";
stmt.executeUpdate(sql);
taOutput.setText("Record added successfully.");
} catch (Exception e) {
e.printStackTrace();
taOutput.setText("Error adding record.");
}
}
// 根据ID查询记录
private void searchRecord() {
String id = tfID.getText().trim();
try {
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM person WHERE id='" + id + "'";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
String name = rs.getString("name");
String age = rs.getString("age");
tfName.setText(name);
tfAge.setText(age);
taOutput.setText("Record found.");
} else {
taOutput.setText("Record not found.");
}
} catch (Exception e) {
e.printStackTrace();
taOutput.setText("Error searching record.");
}
}
// 根据ID更新记录
private void updateRecord() {
String id = tfID.getText().trim();
String name = tfName.getText().trim();
String age = tfAge.getText().trim();
try {
Statement stmt = conn.createStatement();
String sql = "UPDATE person SET name='" + name + "', age='" + age + "' WHERE id='" + id + "'";
int rows = stmt.executeUpdate(sql);
if (rows == 1) {
taOutput.setText("Record updated successfully.");
} else {
taOutput.setText("Record not found.");
}
} catch (Exception e) {
e.printStackTrace();
taOutput.setText("Error updating record.");
}
}
// 根据ID删除记录
private void deleteRecord() {
String id = tfID.getText().trim();
try {
Statement stmt = conn.createStatement();
String sql = "DELETE FROM person WHERE id='" + id + "'";
int rows = stmt.executeUpdate(sql);
if (rows == 1) {
tfName.setText("");
tfAge.setText("");
taOutput.setText("Record deleted successfully.");
} else {
taOutput.setText("Record not found.");
}
} catch (Exception e) {
e.printStackTrace();
taOutput.setText("Error deleting record.");
}
}
public static void main(String[] args) {
CRUD_GUI frame = new CRUD_GUI();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
```
在这个程序中,我们使用了Java的GUI界面工具包Swing来创建界面元素,并使用MySQL数据库来存储数据。程序中包含了四个按钮,分别用于添加记录、查询记录、更新记录和删除记录。当用户点击这些按钮时,程序会执行相应的操作并将结果输出到文本区域中。
需要注意的是,这个程序仅仅是一个演示,没有考虑到实际应用场景中的安全性和稳定性问题。在实际开发中,需要更加严谨地处理用户输入和异常情况,以确保系统的安全和稳定。
阅读全文