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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序