2014-09-22 95 views
1

当我执行下面的代码时出现错误。java中preparedStatement SQL错误

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

+5

您在这里失踪的空间'fpp_alarm“+” where' – 2014-09-22 17:17:44

+0

片段只对HTML/JavaScript的,不在与HTML/JavaScript – 2014-09-22 18:02:40

回答

0

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

select * from fpp_alarmwhere id = ? 

补充一点空间,在你的SQL文本字符串,无论是后fpp_alarmwhere之前。

0

pp_alarm"+ "wher 

之间没有空格应该是 ...pp_alarm" + " where ...

+0

@sparkon无关的问题中使用它们,那么你将会有这样的查询:“... pp_alarm where ..”,现在它被渲染为“.. pp_alarmwhere ..” – ppuskar 2014-09-22 17:26:29

+0

我的错误它的正确性我在哪里注意到空间。 – SparkOn 2014-09-22 17:27:40

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