Java+学生信息管理系统+GUI+MySQL
时间: 2024-01-17 14:02:42 浏览: 104
以下是一个使用Java编写的学生信息管理系统的示例,包括GUI界面和MySQL数据库的连接和操作:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class StudentManagementSystemGUI extends JFrame {
private JTextField tfName, tfGender, tfIdClass, tfStunum;
private JButton btnAdd, btnDelete, btnUpdate, btnSearch;
private JTextArea taResult;
private Connection conn;
public StudentManagementSystemGUI() {
setTitle("学生信息管理系统");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 2));
panel.add(new JLabel("姓名:"));
tfName = new JTextField();
panel.add(tfName);
panel.add(new JLabel("性别:"));
tfGender = new JTextField();
panel.add(tfGender);
panel.add(new JLabel("班级:"));
tfIdClass = new JTextField();
panel.add(tfIdClass);
panel.add(new JLabel("学号:"));
tfStunum = new JTextField();
panel.add(tfStunum);
btnAdd = new JButton("添加");
btnDelete = new JButton("删除");
btnUpdate = new JButton("更新");
btnSearch = new JButton("查询");
panel.add(btnAdd);
panel.add(btnDelete);
panel.add(btnUpdate);
panel.add(btnSearch);
taResult = new JTextArea();
taResult.setEditable(false);
add(panel, BorderLayout.NORTH);
add(new JScrollPane(taResult), BorderLayout.CENTER);
btnAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addStudent();
}
});
btnDelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
deleteStudent();
}
});
btnUpdate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateStudent();
}
});
btnSearch.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
searchStudent();
}
});
connectToDatabase(); }
private void connectToDatabase() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/student_db";
String username = "root";
String password = "123456";
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
}
private void addStudent() {
String name = tfName.getText();
String gender = tfGender.getText();
String idClass = tfIdClass.getText();
String stunum = tfStunum.getText();
try {
String sql = "INSERT INTO students (name, gender, id_class, stunum) VALUES (?, ?, ?, ?)";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, name);
statement.setString(2, gender);
statement.setString(3, idClass);
statement.setString(4, stunum);
statement.executeUpdate();
statement.close();
clearFields();
showMessage("学生信息添加成功!");
} catch (SQLException e) {
e.printStackTrace();
showMessage("学生信息添加失败!");
}
}
private void deleteStudent() {
String stunum = tfStunum.getText();
try {
String sql = "DELETE FROM students WHERE stunum = ?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, stunum);
int rows = statement.executeUpdate();
statement.close();
clearFields();
if (rows > 0) {
showMessage("学生信息删除成功!");
} else {
showMessage("学生信息不存在!");
}
} catch (SQLException e) {
e.printStackTrace();
showMessage("学生信息删除失败!");
}
}
private void updateStudent() {
String name = tfName.getText();
String gender = tfGender.getText();
String idClass = tfIdClass.getText();
String stunum = tfStunum.getText();
try {
String sql = "UPDATE students SET name = ?, gender = ?, id_class = ? WHERE stunum = ?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, name);
statement.setString(2, gender);
statement.setString(3, idClass);
statement.setString(4, stunum);
int rows = statement.executeUpdate();
statement.close();
clearFields();
if (rows > 0) {
showMessage("学生信息更新成功!");
} else {
showMessage("学生信息不存在!");
}
} catch (SQLException e) {
e.printStackTrace();
showMessage("学生信息更新失败!");
}
}
private void searchStudent() {
String stunum = tfStunum.getText();
try {
String sql = "SELECT * FROM students WHERE stunum = ?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, stunum);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
String name = resultSet.getString("name");
String gender = resultSet.getString("gender");
String idClass = resultSet.getString("id_class");
showMessage("姓名:" + name + "\n性别:" + gender + "\n班级:" + idClass);
} else {
showMessage("学生信息不存在!");
}
resultSet.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
showMessage("学生信息查询失败!");
}
}
private void clearFields() {
tfName.setText("");
tfGender.setText("");
tfIdClass.setText("");
tfStunum.setText("");
}
private void showMessage(String message) {
taResult.setText(message);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new StudentManagementSystemGUI().setVisible(true);
}
});
}
}
```
请注意,上述代码仅为示例,实际的学生信息管理系统可能需要根据具体需求进行修改和完善。
阅读全文