基于Java + Swing + MySQL的学生选课及成绩管理系统(Java课程设计)
由于篇幅限制,我将提供一个简化版的学生信息查询模块的核心代码。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class StudentSearchFrame extends JFrame {
private JTextField studentIdField;
private JButton searchButton;
private JTextArea resultTextArea;
public StudentSearchFrame() {
initComponents();
}
private void initComponents() {
setLayout(new BorderLayout());
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
studentIdField = new JTextField(10);
searchButton = new JButton("查询");
resultTextArea = new JTextArea(10, 20);
panel.add(new JLabel("学生ID:"));
panel.add(studentIdField);
panel.add(searchButton);
add(panel, BorderLayout.NORTH);
add(new JScrollPane(resultTextArea), BorderLayout.CENTER);
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String studentId = studentIdField.getText().trim();
String query = "SELECT * FROM student_info WHERE student_id = ?";
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/your_database", "username", "password");
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1, studentId);
ResultSet rs = pstmt.executeQuery();
resultTextArea.setText(""); // 清空显示区域
while (rs.next()) {
String name = rs.getString("student_name");
String id = rs.getString("student_id");
String major = rs.getString("major");
String grade = rs.getString("grade");
// 将查询结果添加到文本区域
resultTextArea.append("姓名: " + name + "\nID: " + id + "\n专业: " + major + "\n年级: " + grade + "\n\n");
}
rs.close();
pstmt.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
resultTextArea.
评论已关闭