2015-03-25 69 views
0

我想通過java執行上述查詢,但在第6行中出現以下錯誤:線程「主」中的異常com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL語法中有錯誤

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.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 lexicon_lemma = ?)' at line 1 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:422) 
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377) 
at com.mysql.jdbc.Util.getInstance(Util.java:360) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:978) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823) 
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435) 
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582) 
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2526) 
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2484) 
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1446) 

我找不到語法有什麼問題......任何建議? 這是我的查詢:

public static void InsertEntry(String lexiconType, String lemma, String lang) throws SQLException { 
    String selectSQL = "SELECT id FROM lexicons WHERE (lexicon_type = ? AND lexicon_lemma = ?)"; 
    PreparedStatement preparedStatement = connection.prepareStatement(selectSQL); 
    preparedStatement.setString(1, lexiconType); 
    preparedStatement.setString(2, lemma); 
    ResultSet rs = preparedStatement.executeQuery(selectSQL); 
    int id = -1; 
    if (rs.next()) { 
     id = rs.getInt(1); 
     System.out.println(id); 
    } else { 
     String insertSQL = "INSERT INTO lexicons " 
    + "(lexicon_type,lexicon_lemma,lexicon_lang) VALUES" 
    + "(?,?,?)"; 
     PreparedStatement prepStatement = connection.prepareStatement(insertSQL); 
     prepStatement.setString(1, lexiconType); 
     prepStatement.setString(2, lemma); 
     prepStatement.setString(3, lang); 
     prepStatement .executeUpdate(); 
     prepStatement.close(); 
    } 
} 
+0

你的錯誤堆棧是不完整的。將它包含在錯誤堆棧中的sql語句中。 – 2015-03-25 09:39:11

+0

我已更新我的帖子。謝謝 – Omen 2015-03-25 09:50:29

回答

1

沒有必要的SQL語句變量傳遞給executeQueryPreparedStatement。這是你案件中的主要問題。

PreparedStatement preparedStatement = connection.prepareStatement(selectSQL); 
preparedStatement.setString(1, lexiconType); 
preparedStatement.setString(2, lemma); 
// error is in the following statement 
ResultSet rs = preparedStatement.executeQuery(selectSQL); 

正如你重置的sql執行,該語句不被識別爲準備語句,但父母Statement這是在引號之間尋找有效輸入蜇傷等,這些都是不存在的實例。因此是語法錯誤。

更改

ResultSet rs = preparedStatement.executeQuery(selectSQL); 

ResultSet rs = preparedStatement.executeQuery(); 

,它應該是工作。

參考:發佈

PreparedStatement.html#executeQuery

+0

我的粗心大意.. 。!謝謝!問題解決了! – Omen 2015-03-25 10:01:57

0

變化:

ResultSet rs = preparedStatement.executeQuery(selectSQL);

ResultSet rs = preparedStatement.executeQuery();

+0

爲什麼?有什麼區別?把''?''放在單引號中並使用'prepared statement'不會起作用。 – 2015-03-25 09:32:59

+0

在SQL查詢中,字符串包含在一對單引號內。 – Abhi 2015-03-25 09:36:06

+0

用'java'和'prepared statement'封閉在單引號中是不正確的。 – 2015-03-25 09:37:10

相關問題