Java连接数据库并用GUI界面设计学生管理系统的代码
时间: 2023-07-28 13:07:22 浏览: 89
以下是一个简单的Java代码示例,演示如何连接MySQL数据库并使用GUI界面设计学生管理系统:
```java
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StudentManagementSystem extends JFrame implements ActionListener {
// 数据库连接信息
private String url = "jdbc:mysql://localhost:3306/student_db";
private String user = "root";
private String password = "123456";
// GUI组件
private JLabel nameLabel = new JLabel("姓名:");
private JTextField nameField = new JTextField();
private JLabel idLabel = new JLabel("学号:");
private JTextField idField = new JTextField();
private JButton addButton = new JButton("添加");
private JButton deleteButton = new JButton("删除");
private JButton updateButton = new JButton("更新");
private JButton queryButton = new JButton("查询");
private JTextArea resultArea = new JTextArea();
// 数据库连接对象
private Connection conn;
// 数据库查询结果集对象
private ResultSet rs;
public StudentManagementSystem() {
// 初始化GUI界面
this.setTitle("学生管理系统");
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
panel.add(nameLabel);
panel.add(nameField);
panel.add(idLabel);
panel.add(idField);
panel.add(addButton);
panel.add(deleteButton);
panel.add(updateButton);
panel.add(queryButton);
this.add(panel, BorderLayout.NORTH);
this.add(resultArea, BorderLayout.CENTER);
// 添加事件监听器
addButton.addActionListener(this);
deleteButton.addActionListener(this);
updateButton.addActionListener(this);
queryButton.addActionListener(this);
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
// 添加学生信息
String name = nameField.getText();
String id = idField.getText();
String sql = "INSERT INTO student(name, id) VALUES('" + name + "', '" + id + "')";
try {
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
resultArea.setText("添加成功");
} catch (SQLException ex) {
ex.printStackTrace();
resultArea.setText("添加失败");
}
} else if (e.getSource() == deleteButton) {
// 删除学生信息
String id = idField.getText();
String sql = "DELETE FROM student WHERE id = '" + id + "'";
try {
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
resultArea.setText("删除成功");
} catch (SQLException ex) {
ex.printStackTrace();
resultArea.setText("删除失败");
}
} else if (e.getSource() == updateButton) {
// 更新学生信息
String name = nameField.getText();
String id = idField.getText();
String sql = "UPDATE student SET name = '" + name + "' WHERE id = '" + id + "'";
try {
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
resultArea.setText("更新成功");
} catch (SQLException ex) {
ex.printStackTrace();
resultArea.setText("更新失败");
}
} else if (e.getSource() == queryButton) {
// 查询学生信息
String id = idField.getText();
String sql = "SELECT * FROM student WHERE id = '" + id + "'";
try {
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
String name = rs.getString("name");
resultArea.setText("姓名:" + name);
} else {
resultArea.setText("未找到该学生");
}
} catch (SQLException ex) {
ex.printStackTrace();
resultArea.setText("查询失败");
}
}
}
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
system.setVisible(true);
}
}
```
请注意,这段代码仅作为示例,实际应用中需要根据具体情况进行修改和优化。
阅读全文