0

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

2
  • I believe this is not a maven project. so check project libs on project structure Commented Jun 9, 2018 at 17:54
  • The driver is not on the classpath at runtime. Commented Jun 9, 2018 at 21:21

6 Answers 6

5

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'

Sign up to request clarification or add additional context in comments.

2 Comments

I have added it as a library though. It still doesn't work
@Workkkkk please try some tutorial step by step some like this one: javatpoint.com/example-to-connect-to-the-mysql-database please doanload jar file and at it again from this tutorial
2

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.

Comments

0

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 .

Comments

0

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.

Comments

0

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.

4 Comments

They already have that jar inside the project. See screenshot.
In the question's screenshot the jar is not inside the C:\Program Files\Java\jdk, in my answer I meant to include the jar inside this folder, not outside it. Questions Screenshot: External Libraries => (This is where they placed the jar) My Answer: External Libraries => <1.8> => (This is where I am asking them to place the jar)
When you add any jar besides jre, it'll go in External Libraries folder only, no matter the location of that jar in your computer. How do you create a folder inside that?
@v0ld3m0rt Here's how my folder structure looks like. ibb.co/gjg87B1
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.