java中的preparedStatement SQL错误

时间:2014-09-22 17:16:45

标签: java mysql sql jdbc prepared-statement

执行以下代码时出错。

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中执行此查询时也是正确的。

3 个答案:

答案 0 :(得分:0)

连接时,您会发现SQL语句中fpp_alarmwhere之间没有空格。

select * from fpp_alarmwhere id = ? 

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

答案 1 :(得分:0)

之间没有空格
pp_alarm"+ "wher

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

答案 2 :(得分:0)

假设

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 = ?";