import java.sql.*;
public class StudentScoreManagementSystem {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/student_score_db";
String username = "root";
String password = "password";
try {
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
while (rs.next()) {
String name = rs.getString("name");
int score = rs.getInt("score");
System.out.println("姓名:" + name + ",成绩:" + score);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
System.out.println("数据库连接失败:" + e.getMessage());
}
}
}
该代码使用JDBC连接MySQL数据库,并从名为"students"的表中检索所有学生的姓名和成绩。
在while循环中,代码遍历结果集并打印每个学生的姓名和成绩。
最后,关闭数据库连接。
请注意,此代码仅用于演示目的,实际应用中需要添加更多的功能和错误处理。