【问题标题】:Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax线程“主”com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException 中的异常:您的 SQL 语法有错误
【发布时间】:2015-03-25 09:16:46
【问题描述】:

我正在尝试通过 java 执行上述查询,但在第 6 行出现以下错误:

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();
    }
}

【问题讨论】:

  • 您发布的错误堆栈不完整。将其与包含在错误堆栈中的 sql 语句一起发布。
  • 我更新了我的帖子。谢谢

标签: java mysql jdbc


【解决方案1】:

对于PreparedStatement,没有必要将sql语句变量传递给executeQuery。这是你的主要问题。

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

【讨论】:

    【解决方案2】:

    变化:

    ResultSet rs = preparedStatement.executeQuery(selectSQL);

    ResultSet rs = preparedStatement.executeQuery();

    【讨论】:

    • 为什么?有什么区别?并且将'?' 放在单引号中并使用prepared statement 是行不通的。
    • 在 SQL 查询中,字符串用一对单引号括起来。
    • javaprepared statement 用单引号括起来是不正确的。
    • 我试过不带括号但没有用单引号括住?,但同样的错误。
    • 对不起我的错误...错误在这里ResultSet rs = preparedStatement.executeQuery(selectSQL); 将其更改为ResultSet rs = preparedStatement.executeQuery(); 并且无需添加单引号
    猜你喜欢
    • 1970-01-01
    • 2020-10-26
    • 1970-01-01
    • 2021-02-23
    • 2017-02-10
    • 2017-10-30
    • 1970-01-01
    • 2018-07-28
    • 2012-08-12
    相关资源
    最近更新 更多