java实现mysql增删改查的窗口弹出
时间: 2023-07-08 20:24:26 浏览: 108
java mysql实现增删改查
你可以使用Java GUI框架,如Swing或JavaFX,来创建一个窗口并在窗口中添加文本框、按钮和标签等组件来实现MySQL的增删改查功能。以下是一个简单的示例代码:
```java
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MySQLWindow extends JFrame implements ActionListener {
private JLabel label1, label2, label3;
private JTextField tf1, tf2, tf3;
private JButton btn1, btn2, btn3, btn4;
private Connection connection;
public MySQLWindow() {
// 设置窗口标题和大小
setTitle("MySQL Window");
setSize(400, 250);
// 创建标签和文本框
label1 = new JLabel("ID:");
tf1 = new JTextField(10);
label2 = new JLabel("Name:");
tf2 = new JTextField(10);
label3 = new JLabel("Age:");
tf3 = new JTextField(10);
// 创建按钮
btn1 = new JButton("Insert");
btn2 = new JButton("Update");
btn3 = new JButton("Delete");
btn4 = new JButton("Query");
// 添加组件到窗口中
JPanel panel1 = new JPanel(new GridLayout(3, 2));
panel1.add(label1);
panel1.add(tf1);
panel1.add(label2);
panel1.add(tf2);
panel1.add(label3);
panel1.add(tf3);
JPanel panel2 = new JPanel(new GridLayout(1, 4));
panel2.add(btn1);
panel2.add(btn2);
panel2.add(btn3);
panel2.add(btn4);
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(panel1, BorderLayout.CENTER);
container.add(panel2, BorderLayout.SOUTH);
// 添加按钮监听器
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
// 连接MySQL数据库
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
} catch (Exception e) {
e.printStackTrace();
}
// 显示窗口
setVisible(true);
}
// 按钮点击事件处理
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) { // Insert
String sql = "insert into student(id, name, age) values(?, ?, ?)";
try {
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1, Integer.parseInt(tf1.getText()));
ps.setString(2, tf2.getText());
ps.setInt(3, Integer.parseInt(tf3.getText()));
ps.executeUpdate();
JOptionPane.showMessageDialog(this, "Insert successfully!");
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Insert failed!");
}
} else if (e.getSource() == btn2) { // Update
String sql = "update student set name=?, age=? where id=?";
try {
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, tf2.getText());
ps.setInt(2, Integer.parseInt(tf3.getText()));
ps.setInt(3, Integer.parseInt(tf1.getText()));
ps.executeUpdate();
JOptionPane.showMessageDialog(this, "Update successfully!");
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Update failed!");
}
} else if (e.getSource() == btn3) { // Delete
String sql = "delete from student where id=?";
try {
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1, Integer.parseInt(tf1.getText()));
ps.executeUpdate();
JOptionPane.showMessageDialog(this, "Delete successfully!");
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Delete failed!");
}
} else if (e.getSource() == btn4) { // Query
String sql = "select * from student where id=?";
try {
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1, Integer.parseInt(tf1.getText()));
ResultSet rs = ps.executeQuery();
if (rs.next()) {
tf2.setText(rs.getString("name"));
tf3.setText(rs.getInt("age") + "");
} else {
JOptionPane.showMessageDialog(this, "No record found!");
}
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Query failed!");
}
}
}
public static void main(String[] args) {
new MySQLWindow();
}
}
```
在上述代码中,我们使用了JDBC API来连接MySQL数据库,并且为每个按钮添加了事件监听器,以便在用户单击按钮时执行相应的SQL语句。当用户单击Insert、Update、Delete、Query按钮后,程序会执行相应的SQL语句并显示结果对话框。这个示例代码仅供参考,你可以根据自己的需求进行修改和扩展。
阅读全文