动态Web项目:java.lang.ClassNotFoundException:com.mysql.jdbc.Driver

时间:2016-05-29 17:04:30

标签: java eclipse jdbc

我正在尝试在Register servlet中连接我的Database类,但是我收到以下错误。我已经导入了mysql jar,如下所示:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse

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

数据库类:

package org.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Database {
    Connection con;

    public void connect() throws Exception {
        if (con != null)
            return;

        try {
             //The error is here.
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        String url = "jdbc:mysql://localhost:3306/validationdata";
        con = DriverManager.getConnection(url, "root", "");
        System.out.println("It woks :)");
    }

}

错误跟踪

Mai 29, 2016 6:57:57 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNUNG: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Validation' did not find a matching property.
Mai 29, 2016 6:57:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Server version:        Apache Tomcat/7.0.69
Mai 29, 2016 6:57:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Server built:          Apr 11 2016 07:57:09 UTC
Mai 29, 2016 6:57:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Server number:         7.0.69.0
Mai 29, 2016 6:57:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: OS Name:               Windows 8.1
Mai 29, 2016 6:57:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: OS Version:            6.3
Mai 29, 2016 6:57:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Architecture:          amd64
Mai 29, 2016 6:57:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Java Home:             C:\Program Files\Java\jre1.8.0_74
Mai 29, 2016 6:57:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: JVM Version:           1.8.0_74-b02
Mai 29, 2016 6:57:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: JVM Vendor:            Oracle Corporation
Mai 29, 2016 6:57:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: CATALINA_BASE:         G:\eclipse_workspace\Java_JSP_JDBC\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Mai 29, 2016 6:57:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: CATALINA_HOME:         C:\Program Files\Apache Software Foundation\Tomcat 7.0
Mai 29, 2016 6:57:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dcatalina.base=G:\eclipse_workspace\Java_JSP_JDBC\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Mai 29, 2016 6:57:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 7.0
Mai 29, 2016 6:57:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dwtp.deploy=G:\eclipse_workspace\Java_JSP_JDBC\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Mai 29, 2016 6:57:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Djava.endorsed.dirs=C:\Program Files\Apache Software Foundation\Tomcat 7.0\endorsed
Mai 29, 2016 6:57:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dfile.encoding=Cp1252
Mai 29, 2016 6:57:57 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFORMATION: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_74\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_74/bin/server;C:/Program Files/Java/jre1.8.0_74/bin;C:/Program Files/Java/jre1.8.0_74/lib/amd64;C:\xampp\ImageMagick-7.0.1-Q16;C:\Program Files\ImageMagick-7.0.1-Q16;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\MiKTeX 2.9\miktex\bin\x64\;;G:\eclipse\eclipse for dynamic web\eclipse;;.
Mai 29, 2016 6:57:58 PM org.apache.coyote.AbstractProtocol init
INFORMATION: Initializing ProtocolHandler ["http-bio-8082"]
Mai 29, 2016 6:57:58 PM org.apache.coyote.AbstractProtocol init
INFORMATION: Initializing ProtocolHandler ["ajp-bio-8009"]
Mai 29, 2016 6:57:58 PM org.apache.catalina.startup.Catalina load
INFORMATION: Initialization processed in 1070 ms
Mai 29, 2016 6:57:58 PM org.apache.catalina.core.StandardService startInternal
INFORMATION: Starting service Catalina
Mai 29, 2016 6:57:58 PM org.apache.catalina.core.StandardEngine startInternal
INFORMATION: Starting Servlet Engine: Apache Tomcat/7.0.69
Mai 29, 2016 6:57:59 PM org.apache.coyote.AbstractProtocol start
INFORMATION: Starting ProtocolHandler ["http-bio-8082"]
Mai 29, 2016 6:57:59 PM org.apache.coyote.AbstractProtocol start
INFORMATION: Starting ProtocolHandler ["ajp-bio-8009"]
Mai 29, 2016 6:57:59 PM org.apache.catalina.startup.Catalina start
INFORMATION: Server startup in 1508 ms
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1858)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1701)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at org.model.Database.connect(Database.java:15)
    at Register.doPost(Register.java:26)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/validationdata
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at org.model.Database.connect(Database.java:21)
    at Register.doPost(Register.java:26)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

图片

enter image description here

2 个答案:

答案 0 :(得分:2)

将mysql-connector-java-5.1.39-bin.jar放在WEB-INF / lib目录中,这样你的web应用程序就会在服务器启动时找到jar

答案 1 :(得分:0)

尝试将Class.forName("com.mysql.jdbc.Driver");替换为Class.forName("com.mysql.jdbc.Driver");