找不到符号PreparedStatement在JAR升级后。

huangapple go评论77阅读模式
英文:

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?

找不到符号PreparedStatement在JAR升级后。

答案1

得分: 4

com.mysql.jdbc.PreparedStatement 是 MySQL 5.x JDBC 驱动程序的内部类。您的代码不应该导入它。应该使用标准的 java.sql.PreparedStatement 类。

在 MySQL 8.x JDBC 驱动程序中,包名已更改,这导致您的代码开始出现编译错误。

解决方法:

  1. 修复您的代码,以便不导入任何 MySQL 实现类1。改用 java.sql.*javax.sql.* 类。

  2. 更改项目的依赖项,使 MySQL 驱动程序 JAR 不是编译时的依赖项。这样做将导致对 JDBC 驱动程序的意外源代码依赖项被标记为编译错误。它还会阻止您的 IDE 对 import 语句提供错误的建议。(我猜测这就是错误的导入进入您的代码库的方式。)

  3. 如果您的代码仍然使用 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:

  1. Fix your code so that it doesn't import any MySQL implementation classes<sup>1</sup>. Use the java.sql.* and javax.sql.* class instead.

  2. 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.)

  3. If your code is (still) using Class.forName to load the JDBC driver, change it to use java.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.

huangapple
  • 本文由 发表于 2020年8月4日 18:50:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63245300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定