0

I am having an issue when executing code which requires the com.mysql.jdbc.Driver. Although I have the mysql-connector-java-5.1.25-bin.jar in my Project Structure (I am running a Gradle-based, Spring Boot application and I am trying to connect it to a locally hosted MySQL database on XAMMP) when I execute the code I am getting a ClassNotFoundException. I have read up on some similar threads here and people say that the mysql-connector-java-5.1.25-bin.jar should also be added to the webserver's lib directory. I have no clue where to find it, or how to do it at all because I am kinda new to java and handling .jar files.

Below you will find the exception. Here is the error I am getting

Here is my code. Here is my code

alex popov
  • 63
  • 4
  • 1
    In a project built with Gradle, the dependency JARs typically aren't present in your project on the filesystem; instead you declare them in the `dependencies` block in your `build.gradle` file. Can you share the contents of your build.gradle file? (as text please, not an image) – dnault Oct 21 '20 at 21:52
  • 1
    As for adding it to the web server's `lib` directory, I wouldn't worry about it. Spring Boot runs an embedded web server that runs a single webapp, so as far as I know there aren't any benefits to sharing the driver by putting it in a common lib directory. See https://stackoverflow.com/questions/6981564/why-must-the-jdbc-driver-be-put-in-tomcat-home-lib-folder if you want to understand why it might be important if you were running multiple webapps in the same JVM. – dnault Oct 21 '20 at 22:02
  • It appears I was missing the dependency in the build.gradle file.Thank you for your answer! – alex popov Oct 21 '20 at 22:21

1 Answers1

2

If you are using a SpringBoot application and maven, you need to add to your pom.xml this dependency:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.49</version>
</dependency>

Or if you use the gradle, you need to add the dependency to your build.gradle file:

compile "mysql:mysql-connector-java:5.1.49"
Volodya Lombrozo
  • 2,325
  • 2
  • 16
  • 34