2014-09-22 83 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 = ?";