【问题标题】:preparedStatement SQL error in java [closed]java中的preparedStatement SQL错误[关闭]
【发布时间】:2014-09-22 17:16:45
【问题描述】:

执行以下代码时出现错误。

preparedStatement = connection.prepareStatement("select * from fpp_alarm"+ "where id = ? ");
preparedStatement.setString(1, id);   //string id = "4"
ResultSet resultSet = preparedStatement.executeQuery();

错误是:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= '4'' at line 1.

我在这里看到过类似的问题:preparedStatement SQL Error

但是,我在我的代码中找不到 mysql 的“保留字”。在mysql中执行这个查询也是对的。

【问题讨论】:

  • 你这里少了一个空格fpp_alarm"+ "where
  • 片段仅适用于 HTML/JavaScript,请勿在与 HTML/JavaScript 无关的问题中使用它们

标签: java mysql sql jdbc prepared-statement


【解决方案1】:

连接后,您会看到 SQL 语句中的 fpp_alarmwhere 之间没有空格。

select * from fpp_alarmwhere id = ? 

在您的 SQL 文字字符串中添加该空格,在 fpp_alarm 之后或 where 之前。

【讨论】:

    【解决方案2】:

    中间没有空格

    pp_alarm"+ "wher
    

    应该是 ...pp_alarm" + " where ...

    【讨论】:

    • @sparkon,那么您将有这样的查询呈现“... pp_alarm where ..”,现在它呈现为“.. pp_alarmwhere ..”
    • 我的错误是正确的,我没有注意到 where 之前的空格。
    【解决方案3】:

    假设

    String query="select * from fpp_alarm"+ "where id = ?";
    

    当它被评估时,结果是这样的

    select * from fpp_alarmwhere id = ? 
    // the + sign concatenates the two pieces of String 
    // into one with no space between them you can either solve it by doing this
    
    String query="select * from fpp_alarm "+ "where id = ?"; // space after fpp_alarm
    

    但我在这里并没有真正看到 + 的使用,只需像这样尝试

    String query="select * from fpp_alarm where id = ?";
    

    【讨论】:

      猜你喜欢
      • 2016-05-14
      • 2017-09-03
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多