学生成绩管理系统是一个用于管理学生考试成绩的软件系统。它通常包括学生信息管理、课程信息管理、成绩录入与查询等功能。数据库是该系统的核心部分,用于存储和管理所有相关数据。
以下是一个简单的Java实现学生成绩管理系统的示例代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection {
private static final String URL = "jdbc:mysql://localhost:3306/student_db";
private static final String USER = "root";
private static final String PASSWORD = "password";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class StudentDAO {
public void addStudent(Student student) throws SQLException {
String sql = "INSERT INTO Student (StudentID, Name, Gender, Grade, Class) VALUES (?, ?, ?, ?, ?)";
try (Connection conn = DBConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, student.getStudentID());
pstmt.setString(2, student.getName());
pstmt.setString(3, student.getGender());
pstmt.setString(4, student.getGrade());
pstmt.setString(5, student.getClassName());
pstmt.executeUpdate();
}
}
public Student getStudentById(String studentID) throws SQLException {
String sql = "SELECT * FROM Student WHERE StudentID = ?";
try (Connection conn = DBConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, studentID);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return new Student(
rs.getString("StudentID"),
rs.getString("Name"),
rs.getString("Gender"),
rs.getString("Grade"),
rs.getString("Class")
);
}
}
return null;
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class GradeDAO {
public void addGrade(Grade grade) throws SQLException {
String sql = "INSERT INTO Grade (StudentID, CourseID, Score) VALUES (?, ?, ?)";
try (Connection conn = DBConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, grade.getStudentID());
pstmt.setString(2, grade.getCourseID());
pstmt.setDouble(3, grade.getScore());
pstmt.executeUpdate();
}
}
public Grade getGradeByStudentAndCourse(String studentID, String courseID) throws SQLException {
String sql = "SELECT * FROM Grade WHERE StudentID = ? AND CourseID = ?";
try (Connection conn = DBConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, studentID);
pstmt.setString(2, courseID);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return new Grade(
rs.getString("StudentID"),
rs.getString("CourseID"),
rs.getDouble("Score")
);
}
}
return null;
}
}
通过以上设计和示例代码,可以实现一个简单的学生成绩管理系统数据库。根据实际需求,可以进一步扩展和优化系统功能。
领取专属 10元无门槛券
手把手带您无忧上云