【问题标题】:Using Prepared Statement to select values for JQuery JTable使用 Prepared Statement 为 JQuery JTable 选择值
【发布时间】:2015-01-16 16:43:48
【问题描述】:

我正在根据由 Java Servlet 处理的 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= ? ORDER BY C3 ASC ' 在第 1 行

【问题讨论】:

  • 我没有看到您的查询有任何问题,但我发现您的查询存在冲突和异常。您查询说您的 where 类是 C2= ?或 C3= ?或 C4= ? OR C5= ? 但您的例外是 OR C1= 有问题?或 C2= ?或 C3= ? ORDER BY C3 ASC 你确定你正在运行相同的查询。
  • @PrasadKhode 抱歉编辑了错误部分

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


【解决方案1】:

在选择子句中的 FROM 之前或 C5 之后添加空格:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 2020-04-12
    相关资源
    最近更新 更多