基于javaweb设计一个mysql数据库学生信息管理系统,要数据库设计过程和源码
时间: 2023-12-28 17:06:20 浏览: 97
JavaWeb课程设计之学生信息管理系统+实验报告(源码+数据库).zip
5星 · 资源好评率100%
数据库设计过程:
1. 确定需求:学生信息管理系统需要存储的数据包括学生基本信息、课程信息和成绩信息。
2. 设计ER图:根据需求,设计ER图,包括学生、课程和成绩三个实体,以及它们之间的关系,如学生与课程之间的选课关系,课程与成绩之间的关系等。
3. 范式化:对ER图进行范式化,消除冗余数据,提高数据库的效率和安全性。
4. 设计数据库表:根据ER图和范式化结果,设计数据库表,并设置主键、外键等约束条件。
5. 编写SQL脚本:根据数据库表的设计,编写SQL脚本,创建数据库及表结构,并插入初始数据。
源码:
由于时间和篇幅有限,我提供一个基本的学生信息管理系统的源码框架,供参考。具体实现过程需要根据具体需求进行调整和完善。
1. 数据库连接类:
```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";
private static final String USER = "root";
private static final String PASSWORD = "password";
private static Connection conn = null;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
return conn;
}
}
```
2. 学生类:
```java
public class Student {
private int id;
private String name;
private String gender;
private int age;
public Student(int id, String name, String gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
// getter and setter methods
}
```
3. 课程类:
```java
public class Course {
private int id;
private String name;
private int credit;
public Course(int id, String name, int credit) {
this.id = id;
this.name = name;
this.credit = credit;
}
// getter and setter methods
}
```
4. 成绩类:
```java
public class Score {
private int id;
private int studentId;
private int courseId;
private int score;
public Score(int id, int studentId, int courseId, int score) {
this.id = id;
this.studentId = studentId;
this.courseId = courseId;
this.score = score;
}
// getter and setter methods
}
```
5. 数据库操作类:
```java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class DBUtil {
public static List<Student> getAllStudents() {
List<Student> students = new ArrayList<Student>();
Connection conn = DBConnection.getConnection();
String sql = "SELECT * FROM student";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String gender = rs.getString("gender");
int age = rs.getInt("age");
students.add(new Student(id, name, gender, age));
}
} catch (SQLException e) {
e.printStackTrace();
}
return students;
}
public static List<Course> getAllCourses() {
List<Course> courses = new ArrayList<Course>();
Connection conn = DBConnection.getConnection();
String sql = "SELECT * FROM course";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int credit = rs.getInt("credit");
courses.add(new Course(id, name, credit));
}
} catch (SQLException e) {
e.printStackTrace();
}
return courses;
}
public static List<Score> getAllScores() {
List<Score> scores = new ArrayList<Score>();
Connection conn = DBConnection.getConnection();
String sql = "SELECT * FROM score";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
int studentId = rs.getInt("student_id");
int courseId = rs.getInt("course_id");
int score = rs.getInt("score");
scores.add(new Score(id, studentId, courseId, score));
}
} catch (SQLException e) {
e.printStackTrace();
}
return scores;
}
public static void addStudent(Student student) {
Connection conn = DBConnection.getConnection();
String sql = "INSERT INTO student(id, name, gender, age) VALUES (?, ?, ?, ?)";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, student.getId());
ps.setString(2, student.getName());
ps.setString(3, student.getGender());
ps.setInt(4, student.getAge());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void addCourse(Course course) {
Connection conn = DBConnection.getConnection();
String sql = "INSERT INTO course(id, name, credit) VALUES (?, ?, ?)";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, course.getId());
ps.setString(2, course.getName());
ps.setInt(3, course.getCredit());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void addScore(Score score) {
Connection conn = DBConnection.getConnection();
String sql = "INSERT INTO score(id, student_id, course_id, score) VALUES (?, ?, ?, ?)";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, score.getId());
ps.setInt(2, score.getStudentId());
ps.setInt(3, score.getCourseId());
ps.setInt(4, score.getScore());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// other database operations, such as update, delete, search, etc.
}
```
6. 主程序:
```java
import java.util.List;
public class Main {
public static void main(String[] args) {
// add a new student
Student student = new Student(1, "Tom", "male", 20);
DBUtil.addStudent(student);
// get all students
List<Student> students = DBUtil.getAllStudents();
for (Student s : students) {
System.out.println(s.getName());
}
// add a new course
Course course = new Course(1, "Java", 3);
DBUtil.addCourse(course);
// get all courses
List<Course> courses = DBUtil.getAllCourses();
for (Course c : courses) {
System.out.println(c.getName());
}
// add a new score
Score score = new Score(1, 1, 1, 90);
DBUtil.addScore(score);
// get all scores
List<Score> scores = DBUtil.getAllScores();
for (Score s : scores) {
System.out.println(s.getScore());
}
}
}
```
阅读全文