0

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?

0

1 Answer 1

4

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

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

1 Comment

thanks very much! I solved my problem, the problem is that I turned off the 'auto import', and every time I make a change in the pom.xml I execute the 'mvn install' manually, after I turn on the 'auto import', I no longer type the command line, and it works!

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.