请用java语言写出来利用gui界面实现数据库数据的crud增查改删功能
时间: 2024-03-21 13:38:59 浏览: 75
好的,以下是一个简单的Java GUI程序,用于实现数据库数据的CRUD功能:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class DatabaseGui extends JFrame implements ActionListener {
private JLabel label1, label2, label3, label4, label5;
private JTextField text1, text2, text3, text4, text5;
private JButton button1, button2, button3, button4;
private JPanel panel1, panel2, panel3, panel4;
private Connection conn;
public DatabaseGui() {
super("Database GUI");
setLayout(new BorderLayout());
// 创建组件
label1 = new JLabel("ID:");
label2 = new JLabel("Name:");
label3 = new JLabel("Age:");
label4 = new JLabel("Gender:");
label5 = new JLabel("Phone:");
text1 = new JTextField(10);
text2 = new JTextField(10);
text3 = new JTextField(10);
text4 = new JTextField(10);
text5 = new JTextField(10);
button1 = new JButton("Add");
button2 = new JButton("Search");
button3 = new JButton("Update");
button4 = new JButton("Delete");
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
// 添加组件
panel1.setLayout(new GridLayout(5, 1));
panel1.add(label1);
panel1.add(label2);
panel1.add(label3);
panel1.add(label4);
panel1.add(label5);
panel2.setLayout(new GridLayout(5, 1));
panel2.add(text1);
panel2.add(text2);
panel2.add(text3);
panel2.add(text4);
panel2.add(text5);
panel3.setLayout(new GridLayout(1, 4));
panel3.add(button1);
panel3.add(button2);
panel3.add(button3);
panel3.add(button4);
panel4.setLayout(new BorderLayout());
panel4.add(panel1, BorderLayout.WEST);
panel4.add(panel2, BorderLayout.CENTER);
panel4.add(panel3, BorderLayout.SOUTH);
add(panel4, BorderLayout.CENTER);
// 添加监听器
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
} catch (Exception e) {
e.printStackTrace();
}
// 设置窗口属性
setSize(400, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) { // 添加数据
try {
String sql = "INSERT INTO student(id, name, age, gender, phone) VALUES(?, ?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, text1.getText());
ps.setString(2, text2.getText());
ps.setString(3, text3.getText());
ps.setString(4, text4.getText());
ps.setString(5, text5.getText());
int result = ps.executeUpdate();
if (result > 0) {
JOptionPane.showMessageDialog(this, "Data added successfully.");
} else {
JOptionPane.showMessageDialog(this, "Failed to add data.");
}
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == button2) { // 查询数据
try {
String sql = "SELECT * FROM student WHERE id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, text1.getText());
ResultSet rs = ps.executeQuery();
if (rs.next()) {
text2.setText(rs.getString("name"));
text3.setText(rs.getString("age"));
text4.setText(rs.getString("gender"));
text5.setText(rs.getString("phone"));
} else {
JOptionPane.showMessageDialog(this, "No data found.");
}
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == button3) { // 更新数据
try {
String sql = "UPDATE student SET name=?, age=?, gender=?, phone=? WHERE id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, text2.getText());
ps.setString(2, text3.getText());
ps.setString(3, text4.getText());
ps.setString(4, text5.getText());
ps.setString(5, text1.getText());
int result = ps.executeUpdate();
if (result > 0) {
JOptionPane.showMessageDialog(this, "Data updated successfully.");
} else {
JOptionPane.showMessageDialog(this, "Failed to update data.");
}
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == button4) { // 删除数据
try {
String sql = "DELETE FROM student WHERE id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, text1.getText());
int result = ps.executeUpdate();
if (result > 0) {
text2.setText("");
text3.setText("");
text4.setText("");
text5.setText("");
JOptionPane.showMessageDialog(this, "Data deleted successfully.");
} else {
JOptionPane.showMessageDialog(this, "Failed to delete data.");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new DatabaseGui();
}
}
```
在这个程序中,我们使用了Java Swing库来创建GUI界面,并使用JDBC驱动程序连接到MySQL数据库。我们创建了一个包含一些文本字段和按钮的面板,用于输入和显示数据。当用户单击按钮时,我们使用PreparedStatement对象执行SQL查询或更新,并显示结果或错误消息。
阅读全文