简体   繁体   中英

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 im 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 Z93F725 A07423FE1C889F448B33D21F46Z.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.ZBF56D570E62CDB 573C86B18526296117Z.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.ZB6EFD606D118D 0F62066E31419FF04CCZ.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.ZB6EFD606D118D0F620 66E31419FF04CCZ.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();
    }
    }
}

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

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 is com.mysql.jdbc.Driver'. This is deprecated. The new driver class is com.mysql.jdbc.Driver'. This is deprecated. The new driver class is com.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

Answer for IntelliJ ultimate edition

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

Tomcat服务器运行配置

Add the jar file

将 jar 文件添加到应用程序服务器库

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

在此处输入图像描述

Invalidate caches and restart

Click here on how to invalidate caches

Download the mysql-connector.jar file from the given link mysql-connector.jar

apache-tomcat-10.0.23 -> lib -> mysql-connector.jar

Just open the tomcat folder and go to the lib folder and then paste the mysql-connector.jar file there. It worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM