SQL 1064 Syntax Error using a JDBC prepared statement

孤者浪人 提交于 2019-12-02 05:32:50

问题


I have:

String query = "INSERT INTO Basestations VALUES(?, ?, ?, ?, ?, ?, ?,"
               + "?, ?, ?, ?, ?, ?, ?, ?)";                  


PreparedStatement prep = conn.prepareStatement(query);

prep.setInt(1, profile.getNetworkId());
prep.setInt(2, profile.getBaseStationId());
prep.setInt(8, profile.getLoadLevel());
prep.setInt(11, profile.getPositionX());
prep.setInt(12, profile.getPositionY());
prep.setInt(13, profile.getPort());

prep.setDouble(3, profile.getSignalStrength());
prep.setDouble(4, profile.getFrequency());
prep.setDouble(6, profile.getMaxBitrate());
prep.setDouble(7, profile.getGuaranteedBitrate());
prep.setDouble(10, profile.getRange());

prep.setString(5, profile.getNetworkType());
prep.setString(9, profile.getProvider());
prep.setString(14, profile.getCharging());

prep.setBoolean(15, true);


prep.executeUpdate(query);

and i am getting:

INFO: SQL Exception: INFO: State : 42000 INFO: Message: 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 '?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' at line 1 INFO: Error : 1064

what could be wrong?


回答1:


You're passing an string representing an invalid SQL statement to the executeUpdate() method when you don't need to. Try just doing prep.executeUpdate();.




回答2:


In your last line you don't need pass the variable query.

So change

 prep.executeUpdate(query);

For:

 prep.executeUpdate();



回答3:


The main error is here:

 // incorrect
 prep.executeUpdate(query);

 // correct
 prep.executeUpdate();

But please try to put your SQL in the following form:

UPDATE table_name(field1, field2, field3) VALUES(?, ? ,?)

This will prevent your code from breaking if there is an update to the table.



来源:https://stackoverflow.com/questions/8897340/sql-1064-syntax-error-using-a-jdbc-prepared-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!