JSP
<form action="SearchSQuestion" method="post">
<table align="center">
<tr>
<td><label>题目内容:</label></td>
<td><input name="problem" type="text" value="${problem}"/></td>
<td> </td>
<td><label>答案:</label></td>
<td><input name="answer" type="text" value="${answer}"/></td>
<td> </td>
<td><button class="btn btn-primary" style="margin-bottom: 8px;" type="submit" >查询</button></td>
</tr>
</table>
</form>
DAO
// 条件查询单选题信息
public List<Schoice> SearchSchoice(String problem, String answer) {
ArrayList<Schoice> list = new ArrayList<Schoice>();
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/appoi";
Connection con = DriverManager.getConnection(url, "root", "123348");
StringBuilder sql = new StringBuilder(
"select id,problem,optionA,optionB,optionC,optionD,answer,jointime from s_choice where 1=1 ");
List<String> paramList = new ArrayList<String>();
if (problem != null && !"".equals(problem.trim())) {
sql.append(" and problem=? ");
paramList.add(problem);
}
if (answer != null && !"".equals(answer.trim())) {
sql.append(" and answer like '%' ? '%' ");
paramList.add(answer);
}
PreparedStatement ptmt = con.prepareStatement(sql.toString());
for (int i = 0; i < paramList.size(); i++) {
ptmt.setString(i + 1, paramList.get(i));
}
ResultSet rs = ptmt.executeQuery();
while (rs.next()) {
Schoice schoice = new Schoice();
list.add(schoice);
schoice.setId(rs.getInt("id"));
schoice.setProblem(rs.getString("problem"));
schoice.setOptionA(rs.getString("optionA"));
schoice.setOptionB(rs.getString("optionB"));
schoice.setOptionC(rs.getString("optionC"));
schoice.setOptionD(rs.getString("optionD"));
schoice.setAnswer(rs.getString("answer"));
schoice.setJointime(rs.getDate("jointime"));
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("条件查询单选题信息");
return list;
}
Servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String problem = request.getParameter("problem");
String answer = request.getParameter("answer");
request.setAttribute("problem",problem);
request.setAttribute("answer",answer);
a list =new a();
request.setAttribute("sc",list.querySchoiceList(problem, answer));
request.getRequestDispatcher("/searchsquestion.jsp").forward(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
service
public List<Schoice> querySchoiceList(String problem, String answer) {
SQuestionDao sq = new SQuestionDao();
return sq.SearchSchoice(problem, answer);
}