英文:
cannot find symbol PreparedStatement after JAR upgrade
问题
我刚刚升级了mysql jdbc连接JAR(从mysql-connector-java-5.0.5.jar到mysql-connector-java-8.0.19.jar),项目开始显示导入语句的错误:
import com.mysql.jdbc.PreparedStatement;
java: 找不到符号
符号: 类 PreparedStatement
位置: 类 package.ClassName
在新的JAR文件中,要在哪里找到PreparedStatement的位置或包,或者它是否被新的JAR中的其他类替代了?
英文:
I just upgraded mysql jdbc connection JAR (from mysql-connector-java-5.0.5.jar to mysql-connector-java-8.0.19.jar) and the project started showing error for import statements:
import com.mysql.jdbc.PreparedStatement;
java: cannot find symbol
symbol: class PreparedStatement
location: class package.ClassName
Where to find the location or package for PreparedStatement in new jar file or is it replaced with any other class in new JAR?
答案1
得分: 4
com.mysql.jdbc.PreparedStatement
是 MySQL 5.x JDBC 驱动程序的内部类。您的代码不应该导入它。应该使用标准的 java.sql.PreparedStatement
类。
在 MySQL 8.x JDBC 驱动程序中,包名已更改,这导致您的代码开始出现编译错误。
解决方法:
-
修复您的代码,以便不导入任何 MySQL 实现类1。改用
java.sql.*
和javax.sql.*
类。 -
更改项目的依赖项,使 MySQL 驱动程序 JAR 不是编译时的依赖项。这样做将导致对 JDBC 驱动程序的意外源代码依赖项被标记为编译错误。它还会阻止您的 IDE 对
import
语句提供错误的建议。(我猜测这就是错误的导入进入您的代码库的方式。) -
如果您的代码仍然使用
Class.forName
来加载 JDBC 驱动程序,请改为使用java.sql.DriverManager
;请参阅javadoc。这样,您就不会受到 MySQL 驱动程序类名称的其他更改所影响。
1 - 您的 Java 代码不应该需要依赖于 MySQL 特定的 API。据我所知,MySQL 实现类反映了标准 JDBC API,因此直接使用它们不会带来任何好处。但不是所有供应商都是如此。
英文:
com.mysql.jdbc.PreparedStatement
is an internal class to the MySQL 5.x JDBC driver. Your code should not import it. It should be using the standard java.sql.PreparedStatement
class instead.
The package names have changed in the MySQL 8.x JDBC drivers, and that is what caused your code to start giving compilation errors.
Solution:
-
Fix your code so that it doesn't import any MySQL implementation classes<sup>1</sup>. Use the
java.sql.*
andjavax.sql.*
class instead. -
Change your project dependencies so that the MySQL driver JAR is not a compile-time dependency. Doing that will cause any accidental source code dependencies on JDBC drivers to be flagged as compilation errors. It will also stop your IDE from making incorrect suggestions for
import
statements. (My guess is that that is how the bogus import got into your codebase.) -
If your code is (still) using
Class.forName
to load the JDBC driver, change it to usejava.sql.DriverManager
instead; see javadoc. That way you won't be burned by another change in the MySQL driver class name.
<sup>1 - Your Java code should not need to depend on MySQL-specific APIs. As far as I know, the MySQL implementation classes mirror the standard JDBC APIs, so you gain nothing by using them directly. This is not true for all vendors.</sup>
答案2
得分: 3
尝试将其替换为:
import java.sql.PreparedStatement;
使用 com.mysql.jdbc.PreparedStatement
是针对 MySQL 特定的,不应直接导入它。
因此,除非您需要 MySQL 特定功能,否则最好使用独立于数据库的接口 java.sql.PreparedStatement
。也请参考 此链接。
英文:
Try to replace it with:
import java.sql.PreparedStatement;
Using com.mysql.jdbc.PreparedStatement
is specific to mysql, and should not be imported directly.
So unless you need MySQL specific features, it's always better to use the interface java.sql.PreparedStatement
that is database independent. See also this.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论