Home >Java >javaTutorial >Test question editing and management functions of online examination system written in Java

Test question editing and management functions of online examination system written in Java

PHPz
PHPzOriginal
2023-09-25 10:13:07780browse

Test question editing and management functions of online examination system written in Java

Java is a high-level programming language widely used in programming development and software design, with the advantages of simplicity, robustness, and portability. This article will introduce how to use Java to write the test question editing and management functions of the online examination system, and provide specific code examples.

  1. System Requirements Analysis
    The test question editing and management functions of the online examination system mainly include the following aspects:
  2. Creation and editing of test questions: including questions, options, and correct answers Input and modify content.
  3. Categories and labels of test questions: Manage test questions according to different categories and labels to facilitate search and filtering.
  4. Storage and persistence of test questions: Save test question data to the database for subsequent query and use.
  5. Database design
    First of all, you need to design the database table structure to store the relevant information of the test questions. The following is a simple database table structure example:
CREATE TABLE tb_question (
    id int PRIMARY KEY AUTO_INCREMENT,
    content VARCHAR(255) NOT NULL,
    option_a VARCHAR(100),
    option_b VARCHAR(100),
    option_c VARCHAR(100),
    option_d VARCHAR(100),
    answer VARCHAR(10),
    category_id int
);

CREATE TABLE tb_category (
    id int PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL
);
  1. Java code example
    Next, we use Java to write code examples for test question editing and management functions. First, we create a Question class to represent the test questions:
public class Question {
    private int id;
    private String content;
    private String optionA;
    private String optionB;
    private String optionC;
    private String optionD;
    private String answer;
    private int categoryId;

    // 省略构造方法和Getter/Setter方法
}

Then, we create a QuestionDao class to implement the database operation of the test questions:

import java.sql.*;

public class QuestionDao {
    private Connection conn;

    public QuestionDao() {
        // 连接数据库
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_exam", "username", "password");
    }

    public int addQuestion(Question question) throws SQLException {
        PreparedStatement stmt = conn.prepareStatement("INSERT INTO tb_question(content, option_a, option_b, option_c, option_d, answer, category_id) VALUES (?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
        stmt.setString(1, question.getContent());
        stmt.setString(2, question.getOptionA());
        stmt.setString(3, question.getOptionB());
        stmt.setString(4, question.getOptionC());
        stmt.setString(5, question.getOptionD());
        stmt.setString(6, question.getAnswer());
        stmt.setInt(7, question.getCategoryId());

        int rowsAffected = stmt.executeUpdate();

        if (rowsAffected == 1) {
            ResultSet rs = stmt.getGeneratedKeys();
            if (rs.next()) {
                question.setId(rs.getInt(1));
            }
        }

        return rowsAffected;
    }

    // 省略其他数据库操作方法
}

Finally, we create a QuestionManager class to Implement the test question editing and management functions:

import java.sql.SQLException;

public class QuestionManager {
    private QuestionDao questionDao;

    public QuestionManager() {
        questionDao = new QuestionDao();
    }

    public int addQuestion(Question question) {
        try {
            return questionDao.addQuestion(question);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

    // 省略其他试题编辑和管理功能的方法
}

The above code example demonstrates how to use Java to write the test question editing and management functions of the online examination system. You can expand and modify it according to actual needs to achieve more complex functions. At the same time, you can also use other Java frameworks, such as Spring and Hibernate, to simplify the development process and improve the maintainability of the code.

The above is the detailed content of Test question editing and management functions of online examination system written in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn