gpt4 book ai didi

java - PreparedStatement 抛出 MySQLSyntaxErrorException

转载 作者:行者123 更新时间:2023-11-30 23:52:03 25 4
gpt4 key购买 nike

我有以下 Java 代码:

String query = "Select 1 from myTable where name = ? and age = ?";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, name);
stmt.setInt(2, age);
ResultSet rs = stmt.executeQuery();

每当我运行上面的代码时,它都会在第 2 行(声明 PreparedStatement 的地方)抛出异常:

Error 0221202: SQLException = 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 age = ?' at line 1

有人对我做错了什么有什么建议吗?谢谢!

最佳答案

你可以试试这个

String query = "Select 1 from myTable where `name` = ? and age = ?";

String query = "Select count(*) from myTable where `name` = ? and age = ?";  

无论如何,你的代码对我来说工作得很好;
您确定您的字符串中没有特殊字符吗?
使用的 MySQL 和驱动程序版本是多少?
这是正确的代码吗?

关于java - PreparedStatement 抛出 MySQLSyntaxErrorException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2811973/

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