用Java语言,写出利用gui界面实现数据库数据的增删查改功能的示例代码
时间: 2024-03-20 11:44:13 浏览: 112
好的,以下是一个简单的使用Java GUI界面实现数据库数据增删查改的示例代码:
```java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class DatabaseGUI extends JFrame {
private JPanel contentPane;
private JTextField textFieldId;
private JTextField textFieldName;
private JTextField textFieldAge;
private JLabel lblId;
private JLabel lblName;
private JLabel lblAge;
private JButton btnAdd;
private JButton btnDelete;
private JButton btnUpdate;
private JButton btnSearch;
private JTextArea textArea;
// 数据库连接信息
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
static final String USER = "root";
static final String PASS = "123456";
// 数据库连接对象
Connection conn = null;
Statement stmt = null;
/**
* 创建GUI应用程序
*/
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
try {
DatabaseGUI frame = new DatabaseGUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
});
}
/**
* 创建应用程序框架
*/
public DatabaseGUI() {
setTitle("Database GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
lblId = new JLabel("ID:");
lblId.setBounds(30, 20, 30, 20);
contentPane.add(lblId);
textFieldId = new JTextField();
textFieldId.setBounds(70, 20, 80, 20);
contentPane.add(textFieldId);
textFieldId.setColumns(10);
lblName = new JLabel("Name:");
lblName.setBounds(30, 60, 50, 20);
contentPane.add(lblName);
textFieldName = new JTextField();
textFieldName.setBounds(70, 60, 80, 20);
contentPane.add(textFieldName);
textFieldName.setColumns(10);
lblAge = new JLabel("Age:");
lblAge.setBounds(30, 100, 50, 20);
contentPane.add(lblAge);
textFieldAge = new JTextField();
textFieldAge.setBounds(70, 100, 80, 20);
contentPane.add(textFieldAge);
textFieldAge.setColumns(10);
btnAdd = new JButton("Add");
btnAdd.setBounds(30, 140, 80, 30);
contentPane.add(btnAdd);
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addData();
}
});
btnDelete = new JButton("Delete");
btnDelete.setBounds(140, 140, 80, 30);
contentPane.add(btnDelete);
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteData();
}
});
btnUpdate = new JButton("Update");
btnUpdate.setBounds(250, 140, 80, 30);
contentPane.add(btnUpdate);
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateData();
}
});
btnSearch = new JButton("Search");
btnSearch.setBounds(360, 140, 80, 30);
contentPane.add(btnSearch);
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchData();
}
});
textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setBounds(30, 180, 410, 70);
contentPane.add(scrollPane);
// 初始化连接数据库
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 添加数据
*/
private void addData() {
String id = textFieldId.getText();
String name = textFieldName.getText();
String age = textFieldAge.getText();
try {
String sql = "INSERT INTO student (id, name, age) VALUES ('" + id + "', '" + name + "', '" + age + "')";
stmt.executeUpdate(sql);
textArea.setText("Add data success!");
} catch (SQLException e) {
e.printStackTrace();
textArea.setText("Add data failed!");
}
}
/**
* 删除数据
*/
private void deleteData() {
String id = textFieldId.getText();
try {
String sql = "DELETE FROM student WHERE id='" + id + "'";
stmt.executeUpdate(sql);
textArea.setText("Delete data success!");
} catch (SQLException e) {
e.printStackTrace();
textArea.setText("Delete data failed!");
}
}
/**
* 更新数据
*/
private void updateData() {
String id = textFieldId.getText();
String name = textFieldName.getText();
String age = textFieldAge.getText();
try {
String sql = "UPDATE student SET name='" + name + "', age='" + age + "' WHERE id='" + id + "'";
stmt.executeUpdate(sql);
textArea.setText("Update data success!");
} catch (SQLException e) {
e.printStackTrace();
textArea.setText("Update data failed!");
}
}
/**
* 查找数据
*/
private void searchData() {
String id = textFieldId.getText();
try {
String sql = "SELECT * FROM student WHERE id='" + id + "'";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
String name = rs.getString("name");
String age = rs.getString("age");
textArea.setText("ID: " + id + "\nName: " + name + "\nAge: " + age);
} else {
textArea.setText("Search data failed!");
}
} catch (SQLException e) {
e.printStackTrace();
textArea.setText("Search data failed!");
}
}
}
```
上述代码中,我们通过调用MySQL数据库的Java驱动程序连接到数据库。然后,我们使用GUI组件创建了一个简单的界面,其中包括文本字段、标签和按钮。对于每个按钮,我们添加了一个事件监听器来处理单击事件并执行相应的操作(添加、删除、更新和查找数据)。最后,我们将文本区域添加到界面中,以显示操作结果。
阅读全文