Java(JavaSwing,GUI)学生信息成绩管理系统(IDEA+MYSQL)
由于提问中包含了完整的学生成绩管理系统的代码,这将是一个非常庞大且复杂的问题。我将提供一个简化版本的代码示例,展示如何使用Java Swing和MySQL创建一个简单的学生成绩管理系统。
import javax.swing.*;
import java.sql.*;
public class StudentScoreManager {
// 连接数据库的方法
public static Connection connectDatabase() {
String url = "jdbc:mysql://localhost:3306/student_db";
String user = "root";
String password = "password";
try {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
// 添加学生成绩的方法
public static void addScore(String name, int score) {
String sql = "INSERT INTO students (name, score) VALUES (?, ?)";
try (Connection conn = connectDatabase();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, name);
pstmt.setInt(2, score);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 查询所有学生成绩的方法
public static void viewScores() {
String sql = "SELECT * FROM students";
try (Connection conn = connectDatabase();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
System.out.println("Name: " + rs.getString("name") + ", Score: " + rs.getInt("score"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// 添加成绩按钮的监听器
addButton.addActionListener(e -> {
String name = nameField.getText();
int score = Integer.parseInt(scoreField.getText());
addScore(name, score);
JOptionPane.showMessageDialog(null, "Score Added Successfully!");
});
// 查询成绩按钮的监听器
viewButton.addActionListener(e -> {
viewScores();
JOptionPane.showMessageDialog(null, "Scores Viewed Successfully!");
});
// 初始化界面
JFrame frame = new
评论已关闭