用java和mysql写一个学生成绩管理系统
时间: 2024-03-06 13:49:57 浏览: 76
好的,下面是一个基本的学生成绩管理系统的设计:
## 数据库设计
### 学生表
| 字段名 | 类型 | 描述 | 是否为主键 |
| ------ | ------- | ---------- | ---------- |
| id | int | 学生编号 | 是 |
| name | varchar | 学生姓名 | 否 |
| gender | varchar | 学生性别 | 否 |
| age | int | 学生年龄 | 否 |
| class | varchar | 学生班级 | 否 |
### 课程表
| 字段名 | 类型 | 描述 | 是否为主键 |
| ------ | ------- | ---------- | ---------- |
| id | int | 课程编号 | 是 |
| name | varchar | 课程名称 | 否 |
### 成绩表
| 字段名 | 类型 | 描述 | 是否为主键 |
| --------- | ------- | ---------- | ---------- |
| id | int | 成绩编号 | 是 |
| studentId | int | 学生编号 | 否 |
| courseId | int | 课程编号 | 否 |
| score | float | 成绩分数 | 否 |
| examDate | date | 考试日期 | 否 |
| remark | varchar | 成绩备注 | 否 |
## Java代码实现
### 学生类
```java
public class Student {
private int id;
private String name;
private String gender;
private int age;
private String className;
public Student(int id, String name, String gender, int age, String className) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.className = className;
}
// getter and setter methods
}
```
### 课程类
```java
public class Course {
private int id;
private String name;
public Course(int id, String name) {
this.id = id;
this.name = name;
}
// getter and setter methods
}
```
### 成绩类
```java
public class Score {
private int id;
private int studentId;
private int courseId;
private float score;
private Date examDate;
private String remark;
public Score(int id, int studentId, int courseId, float score, Date examDate, String remark) {
this.id = id;
this.studentId = studentId;
this.courseId = courseId;
this.score = score;
this.examDate = examDate;
this.remark = remark;
}
// getter and setter methods
}
```
### 数据库操作类
```java
import java.sql.*;
public class Database {
private Connection conn;
public Database(String url, String username, String password) {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
}
public void addStudent(Student student) {
try {
PreparedStatement pstmt = conn.prepareStatement(
"INSERT INTO student(id, name, gender, age, class) VALUES (?, ?, ?, ?, ?)");
pstmt.setInt(1, student.getId());
pstmt.setString(2, student.getName());
pstmt.setString(3, student.getGender());
pstmt.setInt(4, student.getAge());
pstmt.setString(5, student.getClassName());
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void addCourse(Course course) {
try {
PreparedStatement pstmt = conn.prepareStatement(
"INSERT INTO course(id, name) VALUES (?, ?)");
pstmt.setInt(1, course.getId());
pstmt.setString(2, course.getName());
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void addScore(Score score) {
try {
PreparedStatement pstmt = conn.prepareStatement(
"INSERT INTO score(id, studentId, courseId, score, examDate, remark) VALUES (?, ?, ?, ?, ?, ?)");
pstmt.setInt(1, score.getId());
pstmt.setInt(2, score.getStudentId());
pstmt.setInt(3, score.getCourseId());
pstmt.setFloat(4, score.getScore());
pstmt.setDate(5, new java.sql.Date(score.getExamDate().getTime()));
pstmt.setString(6, score.getRemark());
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
### 测试代码
```java
public class Test {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/score";
String username = "root";
String password = "123456";
Database db = new Database(url, username, password);
Student student = new Student(1, "张三", "男", 18, "计算机科学与技术");
db.addStudent(student);
Course course = new Course(1, "Java程序设计");
db.addCourse(course);
Score score = new Score(1, 1, 1, 90.0f, new Date(System.currentTimeMillis()), "优秀");
db.addScore(score);
db.close();
}
}
```
以上是一个简单的学生成绩管理系统的设计和实现,可以根据实际需求进行扩展和优化。
阅读全文