构建在线考试系统:数据库与 Java 代码示例
构建在线考试系统:数据库与 Java 代码示例
概述
本文将指导您使用数据库和 Java 创建一个简单的在线考试系统。我们将介绍数据库设计、学生和教师功能以及代码示例,帮助您了解构建此类系统的基本原理。
数据库设计
我们将使用以下表格来存储考试系统的数据:
- students: 存储学生信息 -
student_idINT PRIMARY KEY -nameVARCHAR(50) -passwordVARCHAR(50)- courses: 存储课程信息 -course_idINT PRIMARY KEY -course_nameVARCHAR(100)- questions: 存储考试题目 -question_idINT PRIMARY KEY -course_idINT, -question_textVARCHAR(500) -option_aVARCHAR(100) -option_bVARCHAR(100) -option_cVARCHAR(100) -option_dVARCHAR(100) -correct_answerVARCHAR(1) - FOREIGN KEY (course_id) REFERENCEScourses(course_id)- exam_results: 存储考试结果 -student_idINT -course_idINT -scoreINT - PRIMARY KEY (student_id,course_id) - FOREIGN KEY (student_id) REFERENCESstudents(student_id) - FOREIGN KEY (course_id) REFERENCEScourses(course_id)- teachers: 存储教师信息 -teacher_idINT PRIMARY KEY -nameVARCHAR(50) -passwordVARCHAR(50)- teacher_courses: 存储教师与课程的关联关系 -teacher_idINT -course_idINT - PRIMARY KEY (teacher_id,course_id) - FOREIGN KEY (teacher_id) REFERENCESteachers(teacher_id) - FOREIGN KEY (course_id) REFERENCEScourses(course_id)
数据库创建脚本
以下是创建数据库和表格的 SQL 脚本:sqlCREATE DATABASE exam;USE exam;
CREATE TABLE students ( student_id INT PRIMARY KEY, name VARCHAR(50), password VARCHAR(50));
CREATE TABLE courses ( course_id INT PRIMARY KEY, course_name VARCHAR(100));
CREATE TABLE questions ( question_id INT PRIMARY KEY, course_id INT, question_text VARCHAR(500), option_a VARCHAR(100), option_b VARCHAR(100), option_c VARCHAR(100), option_d VARCHAR(100), correct_answer VARCHAR(1), FOREIGN KEY (course_id) REFERENCES courses(course_id));
CREATE TABLE exam_results ( student_id INT, course_id INT, score INT, PRIMARY KEY (student_id, course_id), FOREIGN KEY (student_id) REFERENCES students(student_id), FOREIGN KEY (course_id) REFERENCES courses(course_id));
CREATE TABLE teachers ( teacher_id INT PRIMARY KEY, name VARCHAR(50), password VARCHAR(50));
CREATE TABLE teacher_courses ( teacher_id INT, course_id INT, PRIMARY KEY (teacher_id, course_id), FOREIGN KEY (teacher_id) REFERENCES teachers(teacher_id), FOREIGN KEY (course_id) REFERENCES courses(course_id));
-- 创建新用户并授予权限CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'password';GRANT ALL PRIVILEGES ON exam.* TO 'new_user'@'localhost';
Java 代码实现
以下是使用 Java 实现考试系统基本功能的示例代码:javapackage exam;
import java.util.Scanner;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;
public class ExamManagementSystem {
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println('Select Language:'); System.out.println('1. 中文'); System.out.println('2. English'); int langChoice = scanner.nextInt();
switch (langChoice) { case 1: // 中文界面逻辑 break; case 2: // 英文界面逻辑 break; default: System.out.println('Invalid option!'); break; } } // 学生注册 public static void studentRegistration() { Scanner scanner = new Scanner(System.in); System.out.println('请输入学号:'); int studentId = scanner.nextInt(); System.out.println('请输入姓名:'); scanner.nextLine(); String name = scanner.nextLine(); System.out.println('请输入密码:'); String password = scanner.nextLine(); System.out.println('请再次输入密码:'); String confirmPassword = scanner.nextLine(); if (!password.equals(confirmPassword)) { System.out.println('两次输入的密码不一致,请重新注册!'); return; } // 将学生信息插入到数据库中 try (Connection conn = DatabaseConnector.getConnection()) { String sql = 'INSERT INTO students (student_id, name, password) VALUES (?, ?, ?)'; PreparedStatement statement = conn.prepareStatement(sql); statement.setInt(1, studentId); statement.setString(2, name); statement.setString(3, password); statement.executeUpdate(); System.out.println('注册成功!'); } catch (SQLException e) { e.printStackTrace(); System.out.println('注册失败,请重新尝试!'); } } // 学生登录 public static void studentLogin() { Scanner scanner = new Scanner(System.in); System.out.println('请输入学号:'); int studentId = scanner.nextInt(); System.out.println('请输入密码:'); String password = scanner.next(); // 查询数据库验证学生登录信息 try (Connection conn = DatabaseConnector.getConnection()) { String sql = 'SELECT * FROM students WHERE student_id = ? AND password = ?'; PreparedStatement statement = conn.prepareStatement(sql); statement.setInt(1, studentId); statement.setString(2, password); ResultSet result = statement.executeQuery(); if (result.next()) { System.out.println('登录成功!'); // 登录成功后的逻辑 } else { System.out.println('学号或密码错误,请重新登录!'); studentLogin(); } } catch (SQLException e) { e.printStackTrace(); System.out.println('登录失败,请重新尝试!'); } }
// 学生开始考试 public static void studentStartExam() { Scanner scanner = new Scanner(System.in); System.out.println('请输入您的学号:'); int studentId = scanner.nextInt(); System.out.println('请选择您要考试的学科:'); System.out.println('1. Mathematics'); System.out.println('2. English'); System.out.println('3. Science'); int subjectChoice = scanner.nextInt();
String subject = ''; int totalQuestions = 0; switch (subjectChoice) { case 1: subject = 'Mathematics'; totalQuestions = 2; // 假设每个科目只有2道题 break; case 2: subject = 'English'; totalQuestions = 2; break; case 3: subject = 'Science'; totalQuestions = 2; break; default: System.out.println('无效的选项!'); return; }
int score = 0; for (int i = 1; i <= totalQuestions; i++) { System.out.println('问题 ' + i + ':'); // 从数据库中获取题目 try (Connection conn = DatabaseConnector.getConnection()) { String sql = 'SELECT * FROM questions WHERE course_name = ? ORDER BY RAND() LIMIT 1'; PreparedStatement statement = conn.prepareStatement(sql); statement.setString(1, subject); ResultSet result = statement.executeQuery(); if (result.next()) { String question = result.getString('question_text'); String optionA = result.getString('option_a'); String optionB = result.getString('option_b'); String optionC = result.getString('option_c'); String optionD = result.getString('option_d'); String answer = result.getString('correct_answer');
System.out.println(question); System.out.println('A. ' + optionA); System.out.println('B. ' + optionB); System.out.println('C. ' + optionC); System.out.println('D. ' + optionD); System.out.println('请选择您的答案:'); scanner.nextLine(); String studentAnswer = scanner.nextLine();
if (studentAnswer.equalsIgnoreCase(answer)) { score += 10; System.out.println('回答正确!'); } else { System.out.println('回答错误!'); } } } catch (SQLException e) { e.printStackTrace(); System.out.println('获取题目失败!'); }
if (i < totalQuestions) { System.out.println('请选择您要进行的操作:'); System.out.println('1. 下一题'); System.out.println('2. 完成答题'); int answerChoice = scanner.nextInt();
if (answerChoice == 2) { break; } } }
System.out.println('您的得分是:' + score);
// 将学生的分数保存到数据库中 try (Connection conn = DatabaseConnector.getConnection()) { String sql = 'INSERT INTO exam_results (student_id, course_id, score) VALUES (?, ?, ?)'; PreparedStatement statement = conn.prepareStatement(sql); statement.setInt(1, studentId); statement.setInt(2, subjectChoice); // 使用 subjectChoice 作为 course_id statement.setInt(3, score); statement.executeUpdate(); System.out.println('分数已保存!'); } catch (SQLException e) { e.printStackTrace(); System.out.println('保存分数失败!'); } }
// 教师登录 public static void teacherLogin() { // 教师登录逻辑
原文地址: https://www.cveoy.top/t/topic/f0Af 著作权归作者所有。请勿转载和采集!