java实现mysql增删改查的窗口弹出
时间: 2023-07-08 09:24:39 浏览: 106
java mysql实现增删改查
可以使用Java Swing库来实现MySQL增删改查的窗口弹出。以下是一个简单的示例:
1. 首先,需要在Java项目中导入MySQL连接器(JDBC)的驱动程序。可以从MySQL官网下载最新版本的驱动程序,然后将其添加到项目的依赖中。
2. 创建一个Java类,用于连接到MySQL数据库。可以使用以下代码:
```java
import java.sql.*;
public class MySQLConnector {
private Connection conn;
private Statement stmt;
public MySQLConnector() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
stmt = conn.createStatement();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public ResultSet executeQuery(String query) {
try {
return stmt.executeQuery(query);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public int executeUpdate(String query) {
try {
return stmt.executeUpdate(query);
} catch (Exception ex) {
ex.printStackTrace();
return -1;
}
}
public void close() {
try {
stmt.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
```
这个类包含了连接到MySQL数据库、执行查询和更新操作、关闭连接等方法。需要将“mydatabase”、“username”和“password”替换为实际的数据库名称、用户名和密码。
3. 创建一个Java窗口应用程序,用于实现MySQL增删改查的窗口弹出。可以使用以下代码:
```java
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class MySQLWindow extends JFrame {
private JTextField tfName, tfAge, tfEmail;
private JButton btnInsert, btnUpdate, btnDelete, btnSearch;
private JTextArea taResult;
private MySQLConnector connector;
public MySQLWindow() {
super("MySQL Window");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JPanel panelName = new JPanel();
panelName.add(new JLabel("Name"));
tfName = new JTextField(20);
panelName.add(tfName);
panel.add(panelName);
JPanel panelAge = new JPanel();
panelAge.add(new JLabel("Age"));
tfAge = new JTextField(20);
panelAge.add(tfAge);
panel.add(panelAge);
JPanel panelEmail = new JPanel();
panelEmail.add(new JLabel("Email"));
tfEmail = new JTextField(20);
panelEmail.add(tfEmail);
panel.add(panelEmail);
JPanel panelButtons = new JPanel();
btnInsert = new JButton("Insert");
btnInsert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = tfName.getText();
int age = Integer.parseInt(tfAge.getText());
String email = tfEmail.getText();
int result = connector.executeUpdate("INSERT INTO mytable (name, age, email) VALUES ('" + name + "', " + age + ", '" + email + "')");
if (result > 0) {
taResult.setText("Insert success");
} else {
taResult.setText("Insert failed");
}
}
});
panelButtons.add(btnInsert);
btnUpdate = new JButton("Update");
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = tfName.getText();
int age = Integer.parseInt(tfAge.getText());
String email = tfEmail.getText();
int result = connector.executeUpdate("UPDATE mytable SET age = " + age + ", email = '" + email + "' WHERE name = '" + name + "'");
if (result > 0) {
taResult.setText("Update success");
} else {
taResult.setText("Update failed");
}
}
});
panelButtons.add(btnUpdate);
btnDelete = new JButton("Delete");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = tfName.getText();
int result = connector.executeUpdate("DELETE FROM mytable WHERE name = '" + name + "'");
if (result > 0) {
taResult.setText("Delete success");
} else {
taResult.setText("Delete failed");
}
}
});
panelButtons.add(btnDelete);
btnSearch = new JButton("Search");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = tfName.getText();
ResultSet rs = connector.executeQuery("SELECT * FROM mytable WHERE name = '" + name + "'");
try {
if (rs.next()) {
taResult.setText("Name: " + rs.getString("name") + "\nAge: " + rs.getInt("age") + "\nEmail: " + rs.getString("email"));
} else {
taResult.setText("No results");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
panelButtons.add(btnSearch);
panel.add(panelButtons);
taResult = new JTextArea(10, 30);
taResult.setEditable(false);
JScrollPane scroll = new JScrollPane(taResult);
panel.add(scroll);
add(panel);
connector = new MySQLConnector();
}
public static void main(String[] args) {
MySQLWindow window = new MySQLWindow();
window.setVisible(true);
}
}
```
这个类包含了一个窗口,可以输入姓名、年龄、电子邮件地址,然后点击“Insert”、“Update”、“Delete”、“Search”按钮进行增删改查操作。需要将“mytable”替换为实际的表名。可以根据需要修改窗口布局、按钮事件处理程序等。需要注意的是,这个类中的SQL语句直接将用户输入的数据拼接到SQL语句中,存在SQL注入的风险,需要进行防范。可以使用PreparedStatement来代替普通的Statement,以防止SQL注入。
阅读全文