我已经连接上数据库,请你编写一个java gui实现学生信息管理系统的前端页面
时间: 2023-08-12 13:05:10 浏览: 122
学生信息管理系统-java-连接数据库.doc
好的,以下是一个简单的Java GUI实现学生信息管理系统的前端页面的示例代码,它使用了Swing库来创建GUI界面:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class StudentManagementSystem extends JFrame {
private static final String DB_URL = "jdbc:mysql://localhost:3306/student_db";
private static final String DB_USERNAME = "root";
private static final String DB_PASSWORD = "your_password";
private Connection conn;
private PreparedStatement stmt;
private ResultSet rs;
private JLabel nameLabel = new JLabel("Name:");
private JLabel ageLabel = new JLabel("Age:");
private JLabel genderLabel = new JLabel("Gender:");
private JTextField nameField = new JTextField(20);
private JTextField ageField = new JTextField(20);
private JRadioButton maleButton = new JRadioButton("Male");
private JRadioButton femaleButton = new JRadioButton("Female");
private ButtonGroup genderButtonGroup = new ButtonGroup();
private JButton addButton = new JButton("Add");
private JButton deleteButton = new JButton("Delete");
private JButton updateButton = new JButton("Update");
public StudentManagementSystem() {
setTitle("Student Management System");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel(new GridLayout(4, 2, 5, 5));
mainPanel.add(nameLabel);
mainPanel.add(nameField);
mainPanel.add(ageLabel);
mainPanel.add(ageField);
mainPanel.add(genderLabel);
genderButtonGroup.add(maleButton);
genderButtonGroup.add(femaleButton);
mainPanel.add(maleButton);
mainPanel.add(femaleButton);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
buttonPanel.add(updateButton);
add(mainPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addStudent();
}
});
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
deleteStudent();
}
});
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateStudent();
}
});
try {
// 连接数据库
conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
}
private void addStudent() {
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
String gender = maleButton.isSelected() ? "Male" : "Female";
try {
// 执行插入语句
stmt = conn.prepareStatement("INSERT INTO student(name, age, gender) VALUES(?, ?, ?)");
stmt.setString(1, name);
stmt.setInt(2, age);
stmt.setString(3, gender);
stmt.executeUpdate();
JOptionPane.showMessageDialog(this, "Student added successfully");
} catch (SQLException e) {
e.printStackTrace();
}
}
private void deleteStudent() {
String name = nameField.getText();
try {
// 执行删除语句
stmt = conn.prepareStatement("DELETE FROM student WHERE name = ?");
stmt.setString(1, name);
stmt.executeUpdate();
JOptionPane.showMessageDialog(this, "Student deleted successfully");
} catch (SQLException e) {
e.printStackTrace();
}
}
private void updateStudent() {
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
String gender = maleButton.isSelected() ? "Male" : "Female";
try {
// 执行更新语句
stmt = conn.prepareStatement("UPDATE student SET age = ?, gender = ? WHERE name = ?");
stmt.setInt(1, age);
stmt.setString(2, gender);
stmt.setString(3, name);
stmt.executeUpdate();
JOptionPane.showMessageDialog(this, "Student updated successfully");
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
StudentManagementSystem sms = new StudentManagementSystem();
sms.setVisible(true);
}
}
```
在上面的代码中,我们使用了JDBC来连接MySQL数据库,并在GUI界面中添加了三个按钮(添加、删除和更新)以及必要的文本框和单选按钮。当用户点击相应的按钮时,我们会执行相应的数据库操作,例如插入、删除或更新数据。请注意,这只是一个非常简单的示例,实际的学生信息管理系统可能需要更复杂和完整的功能。
阅读全文