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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀