0

Code:

            Class.forName("com.mysql.jdbc.Driver");

            mysql = new MySQL("localhost", 3306, "test", "root", "");
            MySQL.connect();

Exception:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:340)
    at me.veteranfighter.discordbot.Discordbot.main(Discordbot.java:30)

I also tried Class.forName("com.mysql.jdbc.Driver"); that didn't work either

I use java 14 and have already implemented the MySQL connection jar.

Maybe someone can help. Would be very grateful...

Shadow
  • 33,525
  • 10
  • 51
  • 64
Veteranfighter
  • 3
  • 1
  • 1
  • 3
  • Your code cannot find the JAR file where the drivers for MySQL are located. When you obtain the JAR file `mysql-connector-XXX.jar` where XXX is the version, you need to move into `jre/lib/ext` folder and set the `classpath` so it can be found. In Windows, you can set the classpath by going into System Environment Variables. Go to this post to know where to obtain it: https://stackoverflow.com/questions/25546417/where-can-i-download-mysql-jdbc-jar-from – acarlstein Aug 15 '20 at 20:48
  • 1
    Hi, Even though I had already referenced ```mysql-connector-java-8.0.25.jar``` file in my project, and configured ```CLASSPATH```, I was still getting the same error. I came across a YouTube video where they used: ```Class.forName("com.mysql.cj.jdbc.Driver");``` instead of ```Class.forName("com.mysql.java.dbc.Driver");``` , and it worked for me! I was then able to connect to the DB. If you are running your application on a server (e.g., Apache Tomcat), make sure to add the JAR file to lib folder of your server. – Jyotsna Masand Feb 21 '22 at 13:15

1 Answers1

1

make sure you've mysql-connector.jar in your Classpath. "com.mysql.jdbc.Driver" must be present in the classpath in order to successfully connect to MySQL database.

you can download from :- https://dev.mysql.com/downloads/connector/j/

Rahul Sawant
  • 1,223
  • 9
  • 12