JavaWeb-05-JSP规范-05-在线考试管理系统02-试题信息管理

JavaWeb-05-JSP规范-05-在线考试管理系统02-试题信息管理

用户信息管理模块传送门:用户信息管理
考试管理模块传送门:考试管理

本次项目文件:点击下载

提取码:922w

1.目标

试题信息管理模块

2.功能实现

  1. 试题信息添加功能
  2. 试题信息查询功能
  3. 试题信息更新功能
  4. 试题信息删除功能

3.准备工作

3.1创建试题信息表(t_question.frm)

drop table if exists t_question;
create table t_question(
	questionId int primary key auto_increment,#题目编号
    subject varchar(255),#题目	1+1=?
    optionA varchar(20), #选项A	1
    optionB varchar(20), #选项B	4
    optionC varchar(20), #选项C	2
    optionD varchar(20), #选项D	5
	answer  char(1)		 #正确答案  C
);
insert into t_question(subject,optionA,optionB,optionC,optionD,answer) value('1+1=?','1','4','2','5','C');
select * from t_question;

3.2创建试题信息表对应实体类

package com.tsccg.entity;

/**
 * @Author: TSCCG
 * @Date: 2021/08/23 22:59
 * 试题信息表对应实体类
 */
public class Question {
    private Integer questionId;
    private String subject;
    private String optionA;
    private String optionB;
    private String optionC;
    private String optionD;
    private String answer;

    /**
     * 无参构造
     */
    public Question() {
    }

    /**
     * 有参构造
     * @param questionId 题目编号
     * @param subject 题目
     * @param optionA 选项A
     * @param optionB 选项B
     * @param optionC 选项C
     * @param optionD 选项D
     * @param answer 正确答案
     */
    public Question(Integer questionId, String subject, String optionA,
                    String optionB, String optionC, String optionD, String answer) {
        this.questionId = questionId;
        this.subject = subject;
        this.optionA = optionA;
        this.optionB = optionB;
        this.optionC = optionC;
        this.optionD = optionD;
        this.answer = answer;
    }

    public Integer getQuestionId() {
        return questionId;
    }

    public void setQuestionId(Integer questionId) {
        this.questionId = questionId;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getOptionA() {
        return optionA;
    }

    public void setOptionA(String optionA) {
        this.optionA = optionA;
    }

    public String getOptionB() {
        return optionB;
    }

    public void setOptionB(String optionB) {
        this.optionB = optionB;
    }

    public String getOptionC() {
        return optionC;
    }

    public void setOptionC(String optionC) {
        this.optionC = optionC;
    }

    public String getOptionD() {
        return optionD;
    }

    public void setOptionD(String optionD) {
        this.optionD = optionD;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }
}

3.3JdbcUtil工具类

package com.tsccg.util;

import java.sql.*;
import java.util.ResourceBundle;

/**
 * @Author: TSCCG
 * @Date: 2021/08/14 15:32
 * JDBC工具类
 * 1.注册驱动*
 * 2.获取连接*
 * 3.获取预编译的数据库操作对象
 * 4.执行sql
 * 5.处理查询结果集
 * 6.关闭资源*
 */
public class JdbcUtil {
    /**
     * 创建资源绑定器对象
     */
    private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("JDBC");
    //1.注册驱动
    static {
        try {
            Class.forName(BUNDLE.getString("driverName"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    /**
     * 2.获取连接
     * @return 返回连接对象
     * @throws SQLException 将SQLException异常上抛
     */
    public static Connection connect() throws SQLException {
        return DriverManager.getConnection(BUNDLE.getString("url"),
                BUNDLE.getString("user"),BUNDLE.getString("password"));
    }

    /**
     * 6.释放资源
     * @param conn 连接对象
     * @param stmt 数据库操作对象
     * @param rs 查询结果集对象
     */
    public static void closeAll(Connection conn, Statement stmt, ResultSet rs) {
        if(rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(stmt != null) {
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if(conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

4.试题信息添加功能

4.1搭建前台页面【question_add】

在web目录下创建question_add.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加试题信息</title>
    <style type="text/css">
        * {
            font-size: 20px;
        }
        h2 {
            color: green;
            font-size: 40px;
        }
    </style>
</head>
<body>
<center>
    <form action="http://localhost:8080/MyWeb/question/add" method="GET">
    <h2>添加试题信息</h2>
    <table border="1" cellpadding="0" cellspacing="0">
        <!-- 题目  -->
        <tr>
            <td>题目:</td>
            <td><input type="text" name="subject"></td>
        </tr>
        <!-- 选项A  -->
        <tr>
            <td>选项A:</td>
            <td><input type="text" name="optionA"></td>
        </tr>
        <!-- 选项B  -->
        <tr>
            <td>选项B:</td>
            <td><input type="text" name="optionB"></td>
        </tr>
        <!-- 选项C  -->
        <tr>
            <td>选项C:</td>
            <td><input type="text" name="optionC"></td>
        </tr>
        <!-- 选项D  -->
        <tr>
            <td>选项D:</td>
            <td><input type="text" name="optionD"></td>
        </tr>
        <!-- 正确答案  -->   
        <tr>
            <td>正确答案:</td>
            <td>
                <label>
                    <input type="radio" name="answer" value="A">A
                </label> 
                <label>
                    <input type="radio" name="answer" value="B">B
                </label> 
                <label>
                    <input type="radio" name="answer" value="C">C
                </label> 
                <label>
                    <input type="radio" name="answer" value="D">D
                </label> 
            </td>
        </tr>
        <!-- 提交  -->
        <tr>
            <td><input type="submit" value="添加试题"></td>
            <td><input type="reset" value="重置"></td>
        </tr>
    </table>
</form>
</center>
</body>
</html>

4.2编写Dao类【QuestionDao】

此类的主要作用是通过JDBC连接数据库,对数据库表的数据进行操作。

这里先实现添加功能。

package com.tsccg.dao;

import com.tsccg.entity.Question;
import com.tsccg.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @Author: TSCCG
 * @Date: 2021/08/23 23:32
 * 通过jdbc连接数据库,对t_question表中数据进行操作
 */
public class QuestionDao {
    /**
     * 1.添加试题信息
     * @param question 需要添加的试题信息
     * @return 返回添加结果,成功返回true,失败返回false
     */
    public boolean questionAdd(Question question) {
        Connection conn = null;
        PreparedStatement ps = null;
        boolean result = false;
        try {
            conn = JdbcUtil.connect();
            String sql = "insert into t_question(subject,optionA,optionB,optionC,optionD,answer) vlaue(?,?,?,?,?,?)";
            //获取预编译的数据库操作对象
            ps = conn.prepareStatement(sql);
            ps.setString(1,question.getSubject());
            ps.setString(2,question.getOptionA());
            ps.setString(3,question.getOptionB());
            ps.setString(4,question.getOptionC());
            ps.setString(5,question.getOptionD());
            ps.setString(6,question.getAnswer());
            //如果插入成功,那么ps.executeUpdate()返回的值一定大于0
            if (ps.executeUpdate() > 0) {
                result = true;
            }
        } catch(SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeAll(conn,ps,null);
        }
        return result;
    }
}

4.3编写Servlet接口实现类【QuestionAddServlet】

package com.tsccg.controller;

import com.tsccg.dao.QuestionDao;
import com.tsccg.entity.Question;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: TSCCG
 * @Date: 2021/08/24 00:05
 * 实现添加试题信息功能
 */
public class QuestionAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.定义
        String subject,optionA,optionB,optionC,optionD,answer;
        Question question = null;
        QuestionDao dao = new QuestionDao();
        //2.通过请求对象获取请求头中请求参数,也就是试题信息
        subject = request.getParameter("subject");
        optionA = request.getParameter("optionA");
        optionB = request.getParameter("optionB");
        optionC = request.getParameter("optionC");
        optionD = request.getParameter("optionD");
        answer = request.getParameter("answer");
        //将试题信息放入实体类对象中
        question = new Question(null,subject,optionA,optionB,optionC,optionD,answer);
        //3.调用Dao类,将试题信息传进去,然后将insert命令推送到数据库服务器,返回执行结果
        boolean result = dao.questionAdd(question);
        //4.通过请求转发,向Tomcat请求调用info.jsp,将处理结果写入响应体
        if (result) {
            request.setAttribute("info","试题添加成功");
        } else {
            request.setAttribute("info","试题添加失败");
        }
        request.getRequestDispatcher("/info.jsp").forward(request,response);
    }
}

4.4编写JSP文件【info.jsp】

在web目录下新建info.jsp,从当前请求作用域对象中读取试题添加处理结果,然后将处理结果结合html代码写入响应体内。

<%@ page import="static java.awt.SystemColor.info" %><%--
  Created by IntelliJ IDEA.
  User: Admin
  Date: 2021/8/24
  Time: 0:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>结果</title>
</head>
<body>
    <center>
        <%
            //从当前请求作用域对象中读取处理信息
            String result = (String)request.getAttribute("info");
        %>
        <%--展示处理信息--%>
        <font style="font-size: 30px" color="red">
            <%=result%>
        </font>
    </center>
</body>
</html>

4.5在导航页的菜单栏增加试题信息管理按钮

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>left</title>
</head>
<body>
    <ul>
        <li>用户信息管理
            <ol>
                <li><a href="/MyWeb/user_add.html" target="right">用户信息注册</a></li>
                <li><a href="/MyWeb/user/find" target="right">用户信息查询</a></li>
            </ol>
        </li>
        <li>试题信息管理
            <ol>
                <li><a href="/MyWeb/question_add.html" target="right">试题信息添加</a></li>
                <li><a href="/MyWeb/question/find" target="right">试题信息查询</a></li>
            </ol>
        </li>
        <li>考试管理</li>
    </ul>
</body>
</html>

4.6测试添加功能

5.试题信息查询功能

5.1在【QuestionDao】中添加查询方法

package com.tsccg.dao;

import com.tsccg.entity.Question;
import com.tsccg.util.JdbcUtil;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: TSCCG
 * @Date: 2021/08/23 23:32
 * 通过jdbc连接数据库,对t_question表中数据进行操作
 */
public class QuestionDao {
    /**
     * 1.添加试题信息
     * @param question 需要添加的试题信息
     * @return 返回添加结果,成功返回true,失败返回false
     */
    public boolean questionAdd(Question question) {...}

    /**
     * 2.查询全部试题信息
     * @return 返回存放试题信息的List集合
     */
    public List<Question> findAll() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Question> questionList = new ArrayList<>();
        try {
            conn = JdbcUtil.connect();
            String sql = "select * from t_question";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                Integer questionId = rs.getInt("questionId");
                String subject = rs.getString("subject");
                String optionA = rs.getString("optionA");
                String optionB = rs.getString("optionB");
                String optionC = rs.getString("optionC");
                String optionD = rs.getString("optionD");
                String answer = rs.getString("answer");
                Question question = new Question(questionId,subject,optionA,optionB,optionC,optionD,answer);
                questionList.add(question);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JdbcUtil.closeAll(conn,ps,rs);
        }
        return questionList;
    }
}

5.2编写Servlet接口实现类【QuestionFindServlet】

package com.tsccg.controller;

import com.tsccg.dao.QuestionDao;
import com.tsccg.entity.Question;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: TSCCG
 * @Date: 2021/08/24 13:43
 */
public class QuestionFindServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        QuestionDao dao = new QuestionDao();
        //1.调用【Dao】类推送查询命令到数据库,获取所有试题信息
        List<Question> questionList = dao.findAll();
        //2.将得到的试题信息放入请求作用域对象
        request.setAttribute("questionList",questionList);
        //3.通过请求转发方式向Tomcat申请调用question_show.jsp将试题信息结合html标签写入响应体
        request.getRequestDispatcher("/questionShow.jsp").forward(request,response);
    }
}

5.3将试题信息写入响应体

在web目录下新建questionShow.jsp文件

从请求作用域对象中读取所有试题信息,将试题信息结合table标签写入响应体中。

<%@ page import="java.util.List" %>
<%@ page import="com.tsccg.entity.Question" %><%--
  Created by IntelliJ IDEA.
  User: Admin
  Date: 2021/8/24
  Time: 13:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>试题信息表</title>
</head>
<body>
    <center>
        <table border="2" cellspacing="0" cellpadding="0" style="font-size: 20px">
            <tr>
                <th>题目编号</th>
                <th>题目描述</th>
                <th>A</th>
                <th>B</th>
                <th>C</th>
                <th>D</th>
                <th>正确答案</th>
            </tr>
            <%
                //读取所有题目信息
                List<Question> questionList = (List<Question>) request.getAttribute("questionList");
                for (Question question : questionList) {
            %>
                    <%--将试题信息结合表格标签写入响应体--%>
                    <tr align="center">
                        <td><%=question.getQuestionId()%></td>
                        <td><%=question.getSubject()%></td>
                        <td><%=question.getOptionA()%></td>
                        <td><%=question.getOptionB()%></td>
                        <td><%=question.getOptionC()%></td>
                        <td><%=question.getOptionD()%></td>
                        <td><%=question.getAnswer()%></td>
                    </tr>
            <%
                }
            %>

        </table>
    </center>
</body>
</html>

5.4测试查询功能

6.试题信息删除功能

6.1添加删除按钮

在questionShow.jsp文件的table标签中添加删除按钮

<%@ page import="java.util.List" %>
<%@ page import="com.tsccg.entity.Question" %><%--
  Created by IntelliJ IDEA.
  User: Admin
  Date: 2021/8/24
  Time: 13:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>试题信息表</title>
</head>
<body>
    <center>
        <table border="2" cellspacing="0" cellpadding="0" style="font-size: 20px">
            <tr>
                <th>题目编号</th>
                <th>题目描述</th>
                <th>A</th>
                <th>B</th>
                <th>C</th>
                <th>D</th>
                <th>正确答案</th>
                <!--添加数据操作-->
                <th>操作</th>
            </tr>
            <%
                //读取所有题目信息
                List<Question> questionList = (List<Question>) request.getAttribute("questionList");
                for (Question question : questionList) {
            %>
                    <%--将试题信息结合表格标签写入响应体--%>
                    <tr align="center">
                        <td><%=question.getQuestionId()%></td>
                        <td><%=question.getSubject()%></td>
                        <td><%=question.getOptionA()%></td>
                        <td><%=question.getOptionB()%></td>
                        <td><%=question.getOptionC()%></td>
                        <td><%=question.getOptionD()%></td>
                        <td><%=question.getAnswer()%></td>
                        <!-- 添加删除按钮 -->
                        <td><a href="/MyWeb/question/delete?questionId=<%=question.getQuestionId()%>">删除</a></td>
                    </tr>
            <%
                }
            %>

        </table>
    </center>
</body>
</html>

6.2在【QuestionDao】类中添加删除方法

package com.tsccg.dao;

import com.tsccg.entity.Question;
import com.tsccg.util.JdbcUtil;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: TSCCG
 * @Date: 2021/08/23 23:32
 * 通过jdbc连接数据库,对t_question表中数据进行操作
 */
public class QuestionDao {
    /**
     * 1.添加试题信息
     * @param question 需要添加的试题信息
     * @return 返回添加结果,成功返回true,失败返回false
     */
    public boolean questionAdd(Question question) {...}

    /**
     * 2.查询全部试题信息
     * @return 返回存放试题信息的List集合
     */
    public List<Question> findAll() {...}

    /**
     * 3.删除试题信息
     * @param questionId 试题编号
     * @return 返回执行结果
     */
    public boolean delete(Integer questionId) {
        Connection conn = null;
        PreparedStatement ps = null;
        boolean result = false;
        try {
            conn = JdbcUtil.connect();
            String sql = "delete from t_question where questionId=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,questionId);
            if (ps.executeUpdate() > 0) {
                result = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JdbcUtil.closeAll(conn,ps,null);
        }
        return result;
    }
}

6.3编写QuestionDeleteServlet

package com.tsccg.controller;

import com.tsccg.dao.QuestionDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: TSCCG
 * @Date: 2021/08/24 15:07
 * 处理删除指定试题请求
 */
public class QuestionDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer questionId =null;
        QuestionDao dao = new QuestionDao();
        //1.调用请求对象读取请求头中的试题编号
        questionId = Integer.parseInt(request.getParameter("questionId"));
        //2.调用Dao类中的delete方法,传入试题编号,将删除指令推送到数据库服务器中,返回执行结果
        boolean result = dao.delete(questionId);
        //3.将执行结果写入请求作用域对象
        request.setAttribute("info",result?"删除成功":"删除失败");
        //4.请求转发向Tomcat请求调用info.jsp文件
        request.getRequestDispatcher("/info.jsp").forward(request,response);
    }
}

6.4测试删除功能

7.试题信息更新功能

7.1更改试题信息页面

在进行更新之前,需要把待更新的数据完整地展示给用户,用户进行修改后,点击更新按钮,进行更新。

也就是说,需要根据试题编号查询出对应试题信息。

7.1.1添加打开更改页面按钮

在questionShow.jsp文件的table标签中添加打开更改页面按钮

<%@ page import="java.util.List" %>
<%@ page import="com.tsccg.entity.Question" %><%--
  Created by IntelliJ IDEA.
  User: Admin
  Date: 2021/8/24
  Time: 13:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>试题信息表</title>
</head>
<body>
    <center>
        <table border="2" cellspacing="0" cellpadding="0" style="font-size: 20px">
            <tr>
                <th>题目编号</th>
                <th>题目描述</th>
                <th>A</th>
                <th>B</th>
                <th>C</th>
                <th>D</th>
                <th>正确答案</th>
                <!--添加数据操作-->
                <th colspan="2">操作</th>
            </tr>
            <%
                //读取所有题目信息
                List<Question> questionList = (List<Question>) request.getAttribute("questionList");
                for (Question question : questionList) {
            %>
                    <%--将试题信息结合表格标签写入响应体--%>
                    <tr align="center">
                        <td><%=question.getQuestionId()%></td>
                        <td><%=question.getSubject()%></td>
                        <td><%=question.getOptionA()%></td>
                        <td><%=question.getOptionB()%></td>
                        <td><%=question.getOptionC()%></td>
                        <td><%=question.getOptionD()%></td>
                        <td><%=question.getAnswer()%></td>
                        <!-- 添加删除按钮 -->
                        <td><a href="/MyWeb/question/delete?questionId=<%=question.getQuestionId()%>">删除</a></td>
                        <!--添加打开更新页面按钮 -->
                        <td><a href="/MyWeb/question/findById?questionId=<%=question.getQuestionId()%>">更新</a></td>
                    </tr>
            <%
                }
            %>

        </table>
    </center>
</body>
</html>

7.1.2在【QuestionDao】类中添加根据试题编号查询方法

package com.tsccg.dao;

import com.tsccg.entity.Question;
import com.tsccg.util.JdbcUtil;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: TSCCG
 * @Date: 2021/08/23 23:32
 * 通过jdbc连接数据库,对t_question表中数据进行操作
 */
public class QuestionDao {
    /**
     * 1.添加试题信息
     * @param question 试题信息表对应实体类的实例对象
     * @return 返回添加结果,成功返回true,失败返回false
     */
    public boolean questionAdd(Question question) {...}

    /**
     * 2.查询全部试题信息
     * @return 返回存放试题信息的List集合
     */
    public List<Question> findAll() {...}

    /**
     * 3.删除试题信息
     * @param questionId 试题编号
     * @return 返回执行结果
     */
    public boolean delete(Integer questionId) {...}
    /**
     * 4.根据试题编号查询试题信息
     * @param questionId 试题编号
     * @return 返回查询出的试题信息
     */
    public Question findById(Integer questionId) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Question question = null;
        try {
            conn = JdbcUtil.connect();
            String sql = "select * from t_question where questionId=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,questionId);
            rs = ps.executeQuery();
            while (rs.next()) {
                String subject = rs.getString("subject");
                String optionA = rs.getString("optionA");
                String optionB = rs.getString("optionB");
                String optionC = rs.getString("optionC");
                String optionD = rs.getString("optionD");
                String answer = rs.getString("answer");
                question = new Question(questionId,subject,optionA,optionB,optionC,optionD,answer);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JdbcUtil.closeAll(conn,ps,rs);
        }
        return question;
    }
}

7.1.3编写QuestionFindByIdServlet

此类用于处理查询请求,作用如下:

  1. 调用读取请求头中的参数信息,得到试题编号
  2. 调用Dao类,传入试题编号,将查询命令推送给数据库服务器,得到试题信息,然后将试题信息写入当前请求作用域对象
  3. 请求转发向Tomcat请求调用questionUpdate.jsp将试题信息写入请求体
package com.tsccg.controller;

import com.tsccg.dao.QuestionDao;
import com.tsccg.entity.Question;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: TSCCG
 * @Date: 2021/08/24 16:13
 */
public class QuestionFindByIdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer questionId = null;
        QuestionDao dao = new QuestionDao();
        Question question = null;
        //1. 调用读取请求头中的参数信息,得到试题编号
        questionId = Integer.parseInt(request.getParameter("questionId"));
        //2. 调用Dao类,传入试题编号,将查询命令推送给数据库服务器,得到试题信息
        question = dao.findById(questionId);
        //将试题信息写入当前请求作用域对象
        request.setAttribute("question",question);
        //3. 请求转发向Tomcat请求调用questionUpdate.jsp将试题信息写入请求体
        request.getRequestDispatcher("questionUpdate").forward(request,response);
    }
}

7.1.4编写更改试题信息页面

在web目录下新建questionUpdate.jsp。

读取当前请求作用域对象中的试题信息,结合form表单写入响应体。

<%@ page import="com.tsccg.entity.Question" %><%--
  Created by IntelliJ IDEA.
  User: Admin
  Date: 2021/8/24
  Time: 16:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
    <title>更改试题信息</title>
    <style type="text/css">
        * {
            font-size: 20px;
        }
        h2 {
            color: green;
            font-size: 40px;
        }
    </style>
</head>
<body>
<%
    //获取试题信息
    Question question = (Question)request.getAttribute("question");
%>
<%--展示试题信息--%>
    <center>
        <form action="/MyWeb/question/update" method="GET">
            <h2>更改试题信息</h2>
            <table border="1" cellpadding="0" cellspacing="0">
                <!-- 题目编号  -->
                <tr>
                    <td>题目编号:</td>
                    <td><input type="text" name="questionId" value="<%=question.getQuestionId()%>" readonly></td>
                </tr>
                <!-- 题目信息  -->
                <tr>
                    <td>题目信息:</td>
                    <td><input type="text" name="subject" value="<%=question.getSubject()%>"></td>
                </tr>
                <!-- 选项A  -->
                <tr>
                    <td>选项A:</td>
                    <td><input type="text" name="optionA" value="<%=question.getOptionA()%>"></td>
                </tr>
                <!-- 选项B  -->
                <tr>
                    <td>选项B:</td>
                    <td><input type="text" name="optionB" value="<%=question.getOptionB()%>"></td>
                </tr>
                <!-- 选项C  -->
                <tr>
                    <td>选项C:</td>
                    <td><input type="text" name="optionC" value="<%=question.getOptionC()%>"></td>
                </tr>
                <!-- 选项D  -->
                <tr>
                    <td>选项D:</td>
                    <td><input type="text" name="optionD" value="<%=question.getOptionD()%>"></td>
                </tr>
                <!-- 正确答案  -->   
                <tr>
                    <td>正确答案:</td>
                    <td>
                        <label>
                            <input type="radio" name="answer" value="A" <%="A".equals(question.getAnswer())?"checked":""%>>A
                        </label> 
                        <label>
                            <input type="radio" name="answer" value="B" <%="B".equals(question.getAnswer())?"checked":""%>>B
                        </label> 
                        <label>
                            <input type="radio" name="answer" value="C" <%="C".equals(question.getAnswer())?"checked":""%>>C
                        </label> 
                        <label>
                            <input type="radio" name="answer" value="D" <%="D".equals(question.getAnswer())?"checked":""%>>D
                        </label> 
                    </td>
                </tr>
                <!-- 提交  -->
                <tr>
                    <td><input type="submit" value="更新试题"></td>
                    <td><input type="reset" value="重置"></td>
                </tr>
            </table>
        </form>
    </center>
</body>
</html>

7.1.5测试打开更改信息页面

7.2更新试题信息

7.2.1在【QuestionDao】类中添加更新方法

package com.tsccg.dao;

import com.tsccg.entity.Question;
import com.tsccg.util.JdbcUtil;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: TSCCG
 * @Date: 2021/08/23 23:32
 * 通过jdbc连接数据库,对t_question表中数据进行操作
 */
public class QuestionDao {
    /**
     * 1.添加试题信息
     * @param question 需要添加的试题信息
     * @return 返回添加结果,成功返回true,失败返回false
     */
    public boolean questionAdd(Question question) {...}

    /**
     * 2.查询全部试题信息
     * @return 返回存放试题信息的List集合
     */
    public List<Question> findAll() {...}

    /**
     * 3.删除试题信息
     * @param questionId 试题编号
     * @return 返回执行结果
     */
    public boolean delete(Integer questionId) {...}
    /**
     * 4.根据试题编号查询试题信息
     * @param questionId 试题编号
     * @return 返回查询出的试题信息
     */
    public Question findById(Integer questionId) {...}
    /**
     * 5.更新试题信息
     * @param question 新的试题信息
     * @return 返回更新结果
     */
    public boolean questionUpdate(Question question) {
        Connection conn = null;
        PreparedStatement ps = null;
        boolean result = false;
        try {
            conn = JdbcUtil.connect();
            String sql = "update t_question set subject=?,optionA=?,optionB=?,optionC=?,optionD=?,answer=? where questionId=?";
            //获取预编译的数据库操作对象
            ps = conn.prepareStatement(sql);
            ps.setString(1,question.getSubject());
            ps.setString(2,question.getOptionA());
            ps.setString(3,question.getOptionB());
            ps.setString(4,question.getOptionC());
            ps.setString(5,question.getOptionD());
            ps.setString(6,question.getAnswer());
            ps.setInt(7,question.getQuestionId());
            //如果更新成功,那么ps.executeUpdate()返回的值一定大于0
            if (ps.executeUpdate() > 0) {
                result = true;
            }
        } catch(SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeAll(conn,ps,null);
        }
        return result;
    }
}

7.2.2编写QuestionUpdateServlet

此类用于处理更新试题信息请求,作用如下:

  1. 调用读取请求头中的参数信息,得到新的试题信息
  2. 调用Dao类,传入新的试题信息,将更新命令推送给数据库服务器,得到处理结果,然后将处理结果写入当前请求作用域对象
  3. 请求转发向Tomcat请求调用info.jsp将处理结果写入请求体
package com.tsccg.controller;

import com.tsccg.dao.QuestionDao;
import com.tsccg.entity.Question;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: TSCCG
 * @Date: 2021/08/24 17:05
 * 处理更新试题信息请求
 */
public class QuestionUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String subject,optionA,optionB,optionC,optionD,answer;
        Integer questionId;
        Question question = null;
        QuestionDao dao = new QuestionDao();
        //1. 调用读取请求头中的参数信息,得到新的试题信息
        questionId = Integer.parseInt(request.getParameter("questionId"));
        subject = request.getParameter("subject");
        optionA = request.getParameter("optionA");
        optionB = request.getParameter("optionB");
        optionC = request.getParameter("optionC");
        optionD = request.getParameter("optionD");
        answer = request.getParameter("answer");
        //将试题信息放入实体类对象中
        question = new Question(questionId,subject,optionA,optionB,optionC,optionD,answer);
        //2. 调用Dao类,传入新的试题信息,将更新命令推送给数据库服务器,得到处理结果,然后将处理结果写入当前请求作用域对象
        boolean result = dao.questionUpdate(question);
        //3. 请求转发向Tomcat请求调用info.jsp将处理结果写入请求体
        if (result) {
            request.setAttribute("info","试题信息更新成功");
        } else {
            request.setAttribute("info","试题信息更新失败");
        }
        request.getRequestDispatcher("/info.jsp").forward(request,response);
    }
}

7.2.3测试更新功能

posted @ 2021-08-24 01:09  TSCCG  阅读(217)  评论(0编辑  收藏  举报