qq_38104700 2019-03-25 21:48 采纳率: 75%
浏览 382
已采纳

帮忙看一下为什么输入查询条件后查询不到信息

JSP

<form action="SearchSQuestion" method="post">
            <table align="center">
                <tr>
                    <td><label>题目内容:</label></td>
                    <td><input name="problem" type="text" value="${problem}"/></td>
                    <td>&nbsp;</td>
                    <td><label>答案:</label></td>
                    <td><input name="answer" type="text" value="${answer}"/></td>
                    <td>&nbsp;</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);
}
  • 写回答

1条回答 默认 最新

  • threenewbee 2019-03-25 23:52
    关注

    贴这么一大堆没用,且不说这里任意一个地方都可能出错,而且数据库表、配置和用户的输入也会可能导致查询不出来。
    你得学会调试。
    调试的关键点在于,首先检查dao的代码得到的参数problem answer是否正确传进来了
    try catch丢出什么错误了,返回值是什么
    缩小你的错误可能发生位置的范围,直到找到问题。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?