我试图执行以下代码行:

List<File> sectList = new ArrayList<File>();
    try {
        String query = "SELECT * FROM legaf WHERE ida = ?";
        PreparedStatement preparedStmt = connection.prepareStatement(query);
        preparedStmt.setInt(1, idforfile);
        ResultSet result = preparedStmt.executeQuery(query);
        while (result.next())
        {
            fls.add(result.getInt("idf"));
        }
    }
    catch (SQLException ex)
    {
        ex.printStackTrace();
    }

但我收到了以下例外情况:
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?' at line 1

在下面的陈述中:
ResultSet result = preparedStmt.executeQuery(query);

我正在尝试将查询应用于XAMPP MySQL数据库。我在用Intellij。
fls声明为
private List<Integer> fls = new ArrayList<Integer>();

最佳答案


preparedStmt.executeQuery();

而不是
preparedStmt.executeQuery(query);

否则您将执行Statement.executeQuery(String),它不会解析查询字符串中的参数。

关于java - Java:SQLSyntaxErrorException位于?符号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44115406/

10-10 23:03