java.lang.ClassNotFoundException:com.mysql.jdbc.Driver甚至在Intellij

时间:2017-04-08 08:21:38

标签: java mysql maven tomcat jdbc

我正在使用Intellij进行servlet项目并使用maven

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

据我所知,在tomcat webserver上部署war时,这应该处理所有类路径相关的问题。

但我得到了

  

08-Apr-2017 13:40:37.047 INFO [localhost-startStop-1]   org.apache.catalina.startup.HostConfig.deployDirectory部署Web   应用程序目录/ Applications / tomcat / webapps / manager 08-Apr-2017   13:40:37.092 INFO [localhost-startStop-1]   org.apache.catalina.startup.HostConfig.deployDirectory部署   Web应用程序目录/ Applications / tomcat / webapps / manager has   完成44 ms java.lang.ClassNotFoundException:   com.mysql.jdbc.Driver at   org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285)     在   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 at   javax.servlet.http.HttpServlet.service(HttpServlet.java:635)at at   javax.servlet.http.HttpServlet.service(HttpServlet.java:742)at at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)     在   org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)     在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)     在   org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341)     在   org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)     在   org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)     在   org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:798)     在   org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1441)     在   org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)     在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)     在   org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     在java.lang.Thread.run(Thread.java:745)

这是我加载驱动程序的代码

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();
    }
    }
}

2 个答案:

答案 0 :(得分:1)

您必须手动将jdbc_connector添加到tomcat服务器,您应该将jar包含到此文件夹$TOMCAT_HOME/lib

答案 1 :(得分:0)

刚刚复制了你的bug。好像在新版本中驱动程序的名称已更改。当您下载maven依赖项时,最好采用最新的Stable版本 - 这样您就可以从社区获得更好,更快的支持。 `com.mysql.cj.jdbc.Driver” 这就是我得到的错误信息:

加载类com.mysql.jdbc.Driver'. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver'。驱动程序通过SPI自动注册,通常不需要手动加载驱动程序类。 com.mysql.cj.jdbc.exceptions.CommunicationsException:通信链接失败