使用Prepared Statement为JQuery JTable选择值

时间:2014-11-19 08:34:33

标签: java mysql servlets prepared-statement jquery-jtable

我正在根据Java Servlets处理的Jquery JTable的搜索查询字符串选择值。我收到过滤器的请求参数以及startPageIndex和recordPageIndex。但是有一个Mysql错误。请查看下面的代码/错误。

根据页面

选择列
  if(filter != null && !filter.trim().equals("")){
        query = "SELECT C1, C2, C3, C4, C5"
                + "FROM DEPARTMENTS "
                + "WHERE  C2= ? OR C3= ? OR C4= ? OR C5= ? "
                + "ORDER BY department ASC  LIMIT "
                + "? ,  ? ";              

        // the last two values used for table pagination

        pStmt = dbConnection.prepareStatement(query);

        pStmt.setString(1, filter.trim());
        pStmt.setString(2, filter.trim());
        pStmt.setString(3, filter.trim());
        pStmt.setString(4, filter.trim());
        pStmt.setInt(5, startPageIndex);
        pStmt.setInt(6, recordsPerPage);

        ResultSet rs = pStmt.executeQuery(query);
        System.out.println("query executed " + query);

          while (rs.next()) {
                   // Stores in List used to populate in Jquery JTable
          }
    }

MySQL错误:

您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以获得正确的语法,以便在附近使用'?或C3 =?或C4 =?还是C5 =?订购C3 ASC'在第1行

1 个答案:

答案 0 :(得分:0)

在select子句中在FROM之前或之后添加空格:

query = "SELECT C1, C2, C3, C4, C5 "
        + "FROM DEPARTMENTS "
        + "WHERE C2 = ? OR C3 = ? OR C4 = ? OR C5 = ? "
        + "ORDER BY department ASC LIMIT ?, ?";