'java.lang.ClassNotFoundException: com.mysql.jdbc.Driver even after using the mySql Maven Dependency on Intellij

I am using Intellij for a servlet project and using maven

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

as far as i know, this should handle all the classpath related issues when deploying the war on the tomcat webserver.

but i m getting

08-Apr-2017 13:40:37.047 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /Applications/tomcat/webapps/manager 08-Apr-2017 13:40:37.092 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /Applications/tomcat/webapps/manager has finished in 44 ms java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at com.saurabh.testdb.TestDBServlet.doGet(TestDBServlet.java:34) at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1441) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)

Here is my code for loading the driver

package com.saurabh.testdb;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;

@WebServlet("/TestDBServlet")
public class TestDBServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    //set up connection variable
    String jdbcUrl = "jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false";
    String user = "springstudent";
    String pass = "sringstudent";
    String driver = "com.mysql.jdbc.Driver";

    try {

        PrintWriter out = resp.getWriter();

        out.println("Connecting to database: " + jdbcUrl);

        Class.forName(driver);

        Connection myConn = DriverManager.getConnection(jdbcUrl, user, pass);

        out.println("Connection successful!!!");

        myConn.close();

    } catch (Exception exc) {
        exc.printStackTrace();
    }
    }
}


Solution 1:[1]

You have to add your jdbc_connector to your tomcat server manually, you should to include your jar to this folder $TOMCAT_HOME/lib

Solution 2:[2]

Just reproduced your bug. Seems like the name of the driver has changed in the new version. When you are downloading maven dependencies better be taking the latest Stable version - so you can have a better and faster support from the communities. `com.mysql.cj.jdbc.Driver' That is the error msg I've got:

Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

Solution 3:[3]

Answer for IntelliJ ultimate edition

You can add your JDBC connector jar file to run configuration:

Tomcat server run configuration

Add the jar file

Adding jar files to application server library

Or simply you can add the jar to $TOMCAT_HOME/lib

Generally, the default path is C:\Program Files\Apache Software Foundation\Tomcat 9.0\lib

enter image description here

Invalidate caches and restart

Click here on how to invalidate caches

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 YCF_L
Solution 2 strash
Solution 3