2016-07-27 45 views
1

我試圖運行一個準備語句,並在MySQL語法中得到一個錯誤。不知道什麼是錯誤?Prepared Statement的SQL語法錯誤?

代碼:

PreparedStatement stmt = null; 
conn = ds.getConnection(); 
String query = "select distinct FName from PRL p1, RequestView p2 where p1.RequestId = p2.RequestId and p1.PackageId = p2.PackageId and p1.ProductId = ? and p1.ProductNumber = ? and p1.Version = ? order by p2.ArtName"; 
stmt = conn.prepareStatement(query); 
stmt.setString(1,id); 
stmt.setString(2,num); 
stmt.setString(3,ver); 
rs = stmt.executeQuery(query); 

錯誤:

Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: 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 '? and p1.ESTProductMatNumber =? and p1.Version =? order by p2.ArtworkName' at line 1 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936) 

回答

1

沒關係。我想到了。 rs = stmt.executeQuery(query);必須是rs = stmt.executeQuery();

+0

您正在執行查詢,並將查詢作爲其中一個值執行,據推測。 – tadman

+0

順便說一句,如果你使用Java 7+,考慮使用像這樣的自動切斷功能: 嘗試(stmt = conn.prepareStatement(查詢)){ stmt.setString(1,id); stmt.setString(2,num); stmt.setString(3,ver); rs = stmt.executeQuery(query); ... } //如果你想要處理它,請使用catch – lospejos