0

I keep getting the error messages when I run my code attempting to connect to a MySQL server

Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:375)

This is my code. I've made sure to add connector/J to the classpath and still no change.

import java.sql.*;

public class App {
    
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/vehicle", "root", "root"); // For MySQL only

        vehicle focus = new vehicle("Ford", "Focus", 200, 2004, 3000);

        System.out.println(focus.getMake());
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Does this answer your question? [what exactly does this do Class.forName("com.mysql.jdbc.Driver").newInstance();](https://stackoverflow.com/questions/15039265/what-exactly-does-this-do-class-fornamecom-mysql-jdbc-driver-newinstance) – Steffen Sep 11 '22 at 22:36

1 Answers1

0

First of all, loading the JDBC driver using Class.forName is not necessary since JDBC 4.0 which was made available in 2006 (the driver registers its implementation as service provider and will be loaded by the ServiceLoader).

Just remove this line and it should work. If you need the class name, its a good idea to consult the documentation of Connector/J. Take care to read the docs for the correct driver version because the class name changed (currently it's com.mysql.cj.jdbc.Driver).

Update

As Mark Rotteveel pointed out, Class.forName("com.mysql.jdbc.Driver") will not lead to a ClassNotFoundException when used with newer version of Connector/J. At least the newest driver produces a warning:

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

Knowing this, removing Class.forName won't help since the exception simply means, that the driver is not on the class path.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Mihe
  • 2,270
  • 2
  • 4
  • 14
  • Hi when i comment out the line and run the script i get the following error ``` Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/vehicle?autoCorrect=true at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:706) at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229) at App.main(App.java:7) ``` – Corey Maxwell Sep 12 '22 at 00:06
  • Then the driver is not on the class-path or you're using a really old version (I can't imagine this, because even Connector/J 5.1 was JDBC 4.0 compliant). Which versions (Java, Connector/J) are you using? – Mihe Sep 12 '22 at 00:17
  • Ive got a class path environmental variable pointing to the connector jar – Corey Maxwell Sep 12 '22 at 00:28
  • Don't set the evironment variable CLASS_PATH. Just call `java -cp path/to/connector.jar;. App` if you're in the directory containing the App.class file (otherwise replace the dot with the path to the directory containing App.class). If you're on Linux or Mac use a colon instead of a semicolon as separator. – Mihe Sep 12 '22 at 00:32
  • is there a way to do that in vscode – Corey Maxwell Sep 12 '22 at 00:40
  • Have a look [here](https://code.visualstudio.com/docs/java/java-project#_configure-classpath-for-unmanaged-folders) and [here](https://code.visualstudio.com/docs/java/java-project#_manage-dependencies-for-unmanaged-folder). You can also open a terminal within VS Code and enter the java command above manually. – Mihe Sep 12 '22 at 00:49
  • The fact `Class.forName("com.mysql.jdbc.Driver");` produces a `ClassNotFoundException` means that the driver is not on the classpath, so just removing the `Class.forName` line will not solve any problem, it will just move the problem to `DriverManager.getConnection` (throwing a SQLException with a "No suitable driver found" message). – Mark Rotteveel Sep 12 '22 at 07:28
  • @MarkRotteveel, I've checked this and you're right: the MySQL driver produces a warning including the new class name :-) – Mihe Sep 12 '22 at 07:59
  • @MarkRotteveel, I've included your insights in my answer. – Mihe Sep 12 '22 at 08:06