2
votes

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?

enter image description here

2
@khelwood - Could you please help to understand? Do you mean import com.mysql.jdbc.* is now repalced with import java.sql.* with new JAR files? (I'm not a professional Java developer, so not much aware about this package changes.) - Krunal
No, I mean the only PreparedStatement type in Java I've ever had to use is java.sql.PreparedStatement, and maybe that was the type you were looking for. - khelwood
Actully I've import com.mysql.jdbc.PreparedStatement; in my project, which was working fine until I upgraded JDBC connection jar file to mysql-connector-java-8.0.21.jar. What can be it's resolution - Krunal
It was perhaps a mistake to import com.mysql.jdbc.PreparedStatement - when working with JDBC, you usually only code against the java.sql interfaces. - Gyro Gearless

2 Answers

4
votes

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 classes1. 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.


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.

3
votes

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.