gpt4 book ai didi

Java: SQLSyntaxErrorException 在 ?象征

转载 作者:行者123 更新时间:2023-11-29 03:59:43 25 4
gpt4 key购买 nike

我尝试执行以下代码行:

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: SQLSyntaxErrorException 在 ?象征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44115406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com