利用swing语句连接数据库,弹出对话框对数据库进行增删改查
时间: 2023-07-20 17:08:11 浏览: 133
以下是一个基本的Java Swing应用程序,它使用JDBC连接到数据库并执行增删改查操作:
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class DatabaseDemo extends JFrame implements ActionListener {
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost:3306/mydatabase";
private static final String USER = "root";
private static final String PASS = "password";
private JLabel nameLabel, ageLabel, genderLabel;
private JTextField nameTextField, ageTextField, genderTextField;
private JButton addButton, deleteButton, updateButton, searchButton;
public DatabaseDemo() {
super("Database Demo");
nameLabel = new JLabel("Name:");
ageLabel = new JLabel("Age:");
genderLabel = new JLabel("Gender:");
nameTextField = new JTextField(10);
ageTextField = new JTextField(10);
genderTextField = new JTextField(10);
addButton = new JButton("Add");
deleteButton = new JButton("Delete");
updateButton = new JButton("Update");
searchButton = new JButton("Search");
addButton.addActionListener(this);
deleteButton.addActionListener(this);
updateButton.addActionListener(this);
searchButton.addActionListener(this);
JPanel panel = new JPanel(new GridLayout(4, 2));
panel.add(nameLabel);
panel.add(nameTextField);
panel.add(ageLabel);
panel.add(ageTextField);
panel.add(genderLabel);
panel.add(genderTextField);
panel.add(addButton);
panel.add(deleteButton);
panel.add(updateButton);
panel.add(searchButton);
add(panel);
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String name = nameTextField.getText();
int age = Integer.parseInt(ageTextField.getText());
String gender = genderTextField.getText();
Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "INSERT INTO persons (name, age, gender) VALUES('" + name + "'," + age + ",'" + gender + "')";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(this, "Record added successfully.");
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se2) {
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}
}
} else if (e.getSource() == deleteButton) {
String name = nameTextField.getText();
Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "DELETE FROM persons WHERE name='" + name + "'";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(this, "Record deleted successfully.");
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se2) {
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}
}
} else if (e.getSource() == updateButton) {
String name = nameTextField.getText();
int age = Integer.parseInt(ageTextField.getText());
String gender = genderTextField.getText();
Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "UPDATE persons SET age=" + age + ", gender='" + gender + "' WHERE name='" + name + "'";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(this, "Record updated successfully.");
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se2) {
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}
}
} else if (e.getSource() == searchButton) {
String name = nameTextField.getText();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "SELECT * FROM persons WHERE name='" + name + "'";
rs = stmt.executeQuery(sql);
if (rs.next()) {
String gender = rs.getString("gender");
int age = rs.getInt("age");
genderTextField.setText(gender);
ageTextField.setText(Integer.toString(age));
JOptionPane.showMessageDialog(this, "Record found.");
} else {
JOptionPane.showMessageDialog(this, "Record not found.");
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException se1) {
}
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se2) {
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new DatabaseDemo();
}
}
```
此示例使用MySQL数据库,但您可以轻松地将其更改为其他数据库。您需要将`DB_URL`,`USER`和`PASS`变量更改为适合您的数据库。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)