Workkkkk
Workkkkk

Reputation: 285

intelliJ IDEA: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I have downloaded the java connector and added it to my external library, however I am still unable to connect to my database. The error occurs at

Class.forName(driver);

Error:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I've attached a screenshot which should show everything that you need to know.

Picture

SOLUTION: Turns out, you have to put the entire downloaded file in the External Library, not just the folder that its located in

Upvotes: 0

Views: 15455

Answers (6)

Anuprash Gautam
Anuprash Gautam

Reputation: 1

pom.xmlI think it is needless to add any path to the project structure.

Just do this thing if you are using IntelliJ Ultimate.

I just added this dependency to my project's pom.xml and my project started working properly:⤵️

<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.29</version>
      <scope>runtime</scope>
</dependency>

Upvotes: 0

ManKrith
ManKrith

Reputation: 21

I faced similar issue and this is what worked for me . Right- Click on your Project --> Maven --> Reload Project . Now the mysql-connector dependency was added under the External Libraries. enter image description here I am now able to establish connection to my MySQL DB.

Upvotes: 2

Ashad Ahmad
Ashad Ahmad

Reputation: 29

What you can do is, in your "External Libraries Folder", there is a folder that contains various Jars provided by java. Right click on that folder (highlighted in picture attached) and click on "Open Library Settings".

Just a picture of where you have to right click on

It will open up a new window that will have all the jars listed up. Just below the list of all the jars you will find "+" and "-" symbols, click on the "+" symbol and add the mysql-connector jar by selecting it from the File Explorer.

Your application should work now, I am not really sure if it's the correct way to do it, but it works.

Upvotes: 0

Varinderjit
Varinderjit

Reputation: 21

https://www.youtube.com/watch?v=T5Hey0e2Y_g

This will definitely work. But if you get an error saying,'Exporting a package from system module is not allowed with --release', then you should do the following.

Go to File->Settings->Build, Execution, Deployment->Compiler->Java Compiler: Under 'Project bytecode version' write the version of java you are using. But keep in mind that you should be using java version 11+. I've tried it and it worked perfectly.

Upvotes: 0

AntonioOtero
AntonioOtero

Reputation: 1789

I had the same issue. Just leaving this here in case it helps someone.

In my case, I cloned the repo that had the pom.xml file with an old driver that is not compatible with my local database. After cloning, I opened IntelliJ, ran the project and got the same error. Then I changed the pom.xml file with a newer driver and got into this rabbit hole.

The simple fix:

  1. Clone again the repo
  2. Open the pom.xml file with notepad or something else before opening IntelliJ.
  3. Update the pom.xml with the latest connector.
  4. Open IntelliJ
  5. Run project Worked at the first try.

I don't know what IntelliJ does when opening a project for the first time that I cannot change afterwards. I'm sure there must be a settings somewhere that fixes since I can't find it this but this tricked helped me .

Upvotes: 0

mohsenJsh
mohsenJsh

Reputation: 2108

you can add mysql connector via maven dependency like this:

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

or you can download jar file manually at here or search on internet

then add downloaded jar file to your lib directory in project

right click on file at intellij and select 'Add as library'

Upvotes: 5

Related Questions