java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver exception resolution

 

Table of contents

question

reason

solve


        When developing a DB synchronization tool today, because the original jdbcDriver is used to link the database, the code is as follows "Class.forName(this.jdbcDriver);", and my configuration here is ""jdbcDriver":"com.mysql .cj.jdbc.Driver" ", when the program reaches this line, it reports an error " java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver ". At that time, I wondered why I couldn't find the corresponding class because there were obvious dependencies and configurations. Next, let's see how to solve this problem.

question

        When the program executes to "Class.forName(this.jdbcDriver);", the following error is reported:

java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:264)

reason

        After troubleshooting, the problem is solved, and the reason is directly recorded here.

  1. Missing MySQL JDBC Driver: This error usually occurs when the MySQL JDBC driver is not properly configured and loaded in your application. Please ensure that your project is configured correctly and that the MySQL JDBC driver (such as the mysql-connector-java.jar file) is on the classpath.

  2. Classpath issues: A ClassNotFoundException can also result if the MySQL JDBC driver is outside the classpath or not properly accessible. Please check the classpath settings and make sure the MySQL JDBC driver can be accessed and loaded correctly.

  3. Version Mismatch: A ClassNotFoundException may also result if you are trying to use an incompatible version of the MySQL JDBC driver. Make sure your MySQL JDBC driver version is compatible with your application and MySQL database version.

  4. Compilation errors: ClassNotFoundException will also be caused if problems are encountered during compilation (such as import modifier errors, driver does not exist, etc.). Before and during compilation, carefully review your code and dependencies.

Workarounds include:

  • Make sure the MySQL JDBC driver is properly configured and loaded, and that it is on the classpath.
  • Check the classpath settings to make sure the MySQL JDBC driver can be accessed and loaded correctly.
  • Make sure the MySQL JDBC driver version is compatible with the application and MySQL database version.
  • Check for any errors while compiling, and fix them.
  • If none of the above works, try re-downloading and installing the MySQL JDBC driver for your application and MySQL database version.

solve

The reason for my own project is due to the "3. Version mismatch" problem. After investigation, the version path of the "mysql-connector-java.jar" package starting from 5XX is: "com.mysql.jdbc.Driver", and the version path starting from 6XX and above is: "com.mysql.cj.jdbc.Driver" . I can delete the version package myself.

Hope this knowledge can help you

Guess you like

Origin blog.csdn.net/lly576403061/article/details/131461953