Alex Sun
Alex Sun

Reputation: 1

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

i tried to test connect mysql by maven in IDEA, however it occurs "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" all the time, my code is like that in maven:

 <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
            <type>jar</type>
            <scope>compile</scope>
 </dependency>

in java:

Connection connection = null;
    try{
        Class.forName("com.mysql.jdbc.Driver");
    }catch (ClassNotFoundException e){
        e.printStackTrace();
        System.err.println("mysql class not found");
    }

i've tried many methods and the mysql connector certainly in my .m2 folder, but it raises a 'java.lang.ClassNotFoundException: com.mysql.jdbc.Driver' exception all the time, it's really disappointing, can u help me out?

Upvotes: 0

Views: 513

Answers (1)

roronoa_zoro
roronoa_zoro

Reputation: 620

The Problem could be with one of these below:

  1. Check if the IDEA points to the same .m2 directory which you are checking (settings -> Build, Execution & deployment -> Maven/Gradle Check for the .m2 directly pointing)
  2. change the settings-> Build, Execution & deployment -> Maven/Gradle uncheck the option "work offline"
  3. if you have simply copy pasted the jar into .m2 directory it wont work you need to do a maven third party jar install

Lastly do a File -> Invalidate Cache & Restart your IDEA

Upvotes: 4

Related Questions