JavaWeb-05-JSP规范-05-在线考试管理系统02-试题信息管理
用户信息管理模块传送门:用户信息管理模块
1.目标
试题信息管理模块
2.功能实现
- 添加试题信息
- 查询试题信息
- 更新试题信息
- 删除试题信息
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
<%@ 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>