简体   繁体   English

JAR 升级后找不到符号 PreparedStatement

[英]cannot find symbol PreparedStatement after JAR upgrade

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: 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? Where to find the location or package for PreparedStatement in new jar file or is it replaced with any other class in new JAR?

在此处输入图像描述

com.mysql.jdbc.PreparedStatement is an internal class to the MySQL 5.x JDBC driver. 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.它应该使用标准java.sql.PreparedStatement class 代替。

The package names have changed in the MySQL 8.x JDBC drivers, and that is what caused your code to start giving compilation errors. package 的名称在 MySQL 8.x JDBC 驱动程序中已更改,这就是导致您的代码开始出现编译错误的原因。

Solution:解决方案:

  1. Fix your code so that it doesn't import any MySQL implementation classes 1 .修复您的代码,使其不会导入任何 MySQL 实现类1 Use the java.sql.* and javax.sql.* class instead.使用java.sql.*javax.sql.* ZA2F2ED4F8EBC2CBB4C21A29DC40ABZ.* ZA2F2ED4F8EBC2CBB4C21A29DC40ABZ.*

  2. Change your project dependencies so that the MySQL driver JAR is not a compile-time dependency.更改您的项目依赖项,以便 MySQL 驱动程序 JAR 不是编译时依赖项。 Doing that will cause any accidental source code dependencies on JDBC drivers to be flagged as compilation errors.这样做会导致 JDBC 驱动程序上的任何意外源代码依赖项被标记为编译错误。 It will also stop your IDE from making incorrect suggestions for import statements.它还将阻止您的 IDE 对import语句提出不正确的建议。 (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; If your code is (still) using Class.forName to load the JDBC driver, change it to use java.sql.DriverManager instead; see javadoc .请参阅javadoc That way you won't be burned by another change in the MySQL driver class name.这样您就不会被 MySQL 驱动程序 class 名称的另一个更改所困扰。


1 - Your Java code should not need to depend on MySQL-specific APIs. 1 - 您的 Java 代码不需要依赖 MySQL 特定的 API。 As far as I know, the MySQL implementation classes mirror the standard JDBC APIs, so you gain nothing by using them directly.据我所知,MySQL 实现类镜像了标准的 JDBC API,因此直接使用它们将一无所获。 This is not true for all vendors.并非所有供应商都是如此。

Try to replace it with:尝试将其替换为:

import java.sql.PreparedStatement;

Using com.mysql.jdbc.PreparedStatement is specific to mysql, and should not be imported directly.使用com.mysql.jdbc.PreparedStatement是特定于 Z81C3B080DAD537DE57E10E097 的,不应直接导入。

So unless you need MySQL specific features, it's always better to use the interface java.sql.PreparedStatement that is database independent.因此,除非您需要 MySQL 特定功能,否则最好使用与数据库无关的接口java.sql.PreparedStatement See also this .另请参阅

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM