java 编写学生信息成绩管理系统的代码
时间: 2024-03-18 17:43:06 浏览: 15
好,下面提供一个简单的学生信息成绩管理系统的Java代码示例,包括学生信息、课程信息和成绩信息的增删改查操作。
1. 创建数据库表
首先需要在MySQL中创建三张表:student、course和score,用于存储学生信息、课程信息和成绩信息等。
```
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`gender` varchar(4) NOT NULL,
`age` int(11) NOT NULL,
`clazz` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `course` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`number` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `score` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
`score` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
2. 创建JavaBean
创建JavaBean来存储学生、课程和成绩等信息,如下:
```
public class Student {
private int id;
private String name;
private String gender;
private int age;
private String clazz;
// getter和setter方法省略
}
public class Course {
private int id;
private String name;
private String number;
// getter和setter方法省略
}
public class Score {
private int id;
private int studentId;
private int courseId;
private int score;
// getter和setter方法省略
}
```
3. 创建数据库操作类
创建一个数据库操作类,用于实现对数据库的增删改查操作,如下:
```
public class StudentDao {
// 添加学生信息
public void addStudent(Student student) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
String sql = "INSERT INTO student(name, gender, age, clazz) VALUES(?,?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getGender());
pstmt.setInt(3, student.getAge());
pstmt.setString(4, student.getClazz());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt);
}
}
// 删除学生信息
public void deleteStudent(int id) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
String sql = "DELETE FROM student WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt);
}
}
// 修改学生信息
public void updateStudent(Student student) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
String sql = "UPDATE student SET name=?, gender=?, age=?, clazz=? WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getGender());
pstmt.setInt(3, student.getAge());
pstmt.setString(4, student.getClazz());
pstmt.setInt(5, student.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt);
}
}
// 查询学生信息
public Student findStudent(int id) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Student student = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM student WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setGender(rs.getString("gender"));
student.setAge(rs.getInt("age"));
student.setClazz(rs.getString("clazz"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt, rs);
}
return student;
}
// 查询所有学生信息
public List<Student> findAllStudents() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Student> students = new ArrayList<Student>();
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM student";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setGender(rs.getString("gender"));
student.setAge(rs.getInt("age"));
student.setClazz(rs.getString("clazz"));
students.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt, rs);
}
return students;
}
}
public class CourseDao {
// 添加课程信息
public void addCourse(Course course) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
String sql = "INSERT INTO course(name, number) VALUES(?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, course.getName());
pstmt.setString(2, course.getNumber());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt);
}
}
// 删除课程信息
public void deleteCourse(int id) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
String sql = "DELETE FROM course WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt);
}
}
// 修改课程信息
public void updateCourse(Course course) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
String sql = "UPDATE course SET name=?, number=? WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, course.getName());
pstmt.setString(2, course.getNumber());
pstmt.setInt(3, course.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt);
}
}
// 查询课程信息
public Course findCourse(int id) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Course course = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM course WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
course = new Course();
course.setId(rs.getInt("id"));
course.setName(rs.getString("name"));
course.setNumber(rs.getString("number"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt, rs);
}
return course;
}
// 查询所有课程信息
public List<Course> findAllCourses() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Course> courses = new ArrayList<Course>();
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM course";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
Course course = new Course();
course.setId(rs.getInt("id"));
course.setName(rs.getString("name"));
course.setNumber(rs.getString("number"));
courses.add(course);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt, rs);
}
return courses;
}
}
public class ScoreDao {
// 添加成绩信息
public void addScore(Score score) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
String sql = "INSERT INTO score(student_id, course_id, score) VALUES(?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, score.getStudentId());
pstmt.setInt(2, score.getCourseId());
pstmt.setInt(3, score.getScore());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt);
}
}
// 删除成绩信息
public void deleteScore(int id) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
String sql = "DELETE FROM score WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt);
}
}
// 修改成绩信息
public void updateScore(Score score) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
String sql = "UPDATE score SET student_id=?, course_id=?, score=? WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, score.getStudentId());
pstmt.setInt(2, score.getCourseId());
pstmt.setInt(3, score.getScore());
pstmt.setInt(4, score.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt);
}
}
// 查询成绩信息
public Score findScore(int id) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Score score = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM score WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
score = new Score();
score.setId(rs.getInt("id"));
score.setStudentId(rs.getInt("student_id"));
score.setCourseId(rs.getInt("course_id"));
score.setScore(rs.getInt("score"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt, rs);
}
return score;
}
// 查询所有成绩信息
public List<Score> findAllScores() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Score> scores = new ArrayList<Score>();
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM score";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
Score score = new Score();
score.setId(rs.getInt("id"));
score.setStudentId(rs.getInt("student_id"));
score.setCourseId(rs.getInt("course_id"));
score.setScore(rs.getInt("score"));
scores.add(score);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt, rs);
}
return scores;
}
}
```
4. 创建Servlet
创建Servlet来处理前端页面的请求,可以使用JSP或者Thymeleaf等模板引擎来实现动态页面,如下:
```
public class StudentServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理GET请求的代码
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理POST请求的代码
}
}
public class CourseServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理GET请求的代码
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理POST请求的代码
}
}
public class ScoreServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理GET请求的代码
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理POST请求的代码
}
}
```
5. 创建数据库连接池
创建一个数据库连接池类,用于