开发者社区> 问答> 正文

java.lang.NoClassDefFoundError:mysql数据库连接驱动

我试图运行简单的代码与mysql连接。我正在使用eclipse和tomcat。我在库中添加了mysql-connector-java-8.0.1.9.jar:

库-> mysql-> mysql-connector-java-8.0.1.9.jar

我已经花了几个小时来添加和删除mysql-connector-java-8.0.1.9.jar来构建路径,但它无法运行。请帮忙。我需要在运行配置中添加任何东西吗?

这是Eclipse浏览器上的错误:

HTTP Status 500 – Internal Server Error


Type Exception Report

Message javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception 
org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:599)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:500)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)



Root Cause 
javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:917)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:846)
    org.apache.jsp.regCheck_jsp._jspService(regCheck_jsp.java:129)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)



Root Cause 
java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
    compute.DB.con(DB.java:21)
    org.apache.jsp.regCheck_jsp._jspService(regCheck_jsp.java:100)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)



Root Cause 
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1951)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1794)
    compute.DB.con(DB.java:21)
    org.apache.jsp.regCheck_jsp._jspService(regCheck_jsp.java:100)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)



Note The full stack trace of the root cause is available in the server logs.

这是控制台中的输出:

> Mar 16, 2020 4:57:27 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/Mysql] has started
Mar 16, 2020 4:57:27 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/Mysql] is completed
Mar 16, 2020 4:57:41 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/Mysql] threw exception [javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver] with root cause
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1951)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1794)
    at compute.DB.con(DB.java:21)
    at org.apache.jsp.regCheck_jsp._jspService(regCheck_jsp.java:100)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    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:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:165)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1195)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:654)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:319)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:834)

Try - 05 Trying to establish con in regCheck.jsp
Con method called Try 5A
Try 05a - Trying to connect

这是DB.java:

package compute;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.*;

public class DB {


    final static String DB_URL = "jdbc:mysql://localhost:3306/new";
    final static String USER = "admin";
    final static String PASS = "admin";

    public static Connection con() {
        System.out.println("Con method called Try 5A");
        Connection con;
        try {
            System.out.println("Try 05a - Trying to connect");
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            con = DriverManager.getConnection(DB_URL, USER, PASS);
            System.out.println("Try 05a- con established ");
            return con;
        }catch(Exception e) {
            System.out.println(e);
            return null;
        }
    }

    public static Statement st() {
        Statement stmt;
        try {
            stmt=DB.con().createStatement();
            return null;
        }catch(Exception e) {
            System.out.println(e);
            return null;
        }
    }
}

这是regCheck.jsp:

<%@page import="java.sql.Connection" %>
<%@page import="compute.DB" %>
<%@page import="java.sql.PreparedStatement" %>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="ISO-8859-1">
        <title>Insert title here</title>
    </head>
    <body>
        <%
        String user = request.getParameter("username");
        System.out.println("Try - 05 Trying to establish con in regCheck.jsp");
        Connection con = DB.con();
        System.out.println("Preparing Statement in regCheck.jsp");
        PreparedStatement p = con.prepareStatement("Insert into register(username)values(?)");

        p.setString(1,user);
        System.out.println("user::" +user);

        int R = p.executeUpdate();
        if(R!=0)
            response.sendRedirect("index.html");
        else
            response.sendRedirect("register.jsp");

        %>

    </body>
</html>

问题来源:Stack Overflow

展开
收起
montos 2020-03-22 15:01:44 2020 0
3 条回答
写回答
取消 提交回答
  • 个人对建模比较关注

    排查思路:NoClassDefError主要是因为有class文件,但是classLoader中找不到。这种一般出现在类初始化过程中报错,导致class初始化失败,导致没有加载到classLoader中。所以有更上层的错误原因。可以去看对应class的构造器与父类,以及静态类的过程中是否有其他报错。

    2020-03-25 16:38:39
    赞同 展开评论 打赏
  • 1,检查下跑是否成功被ref,如果是POM配置看下依赖中是否引入正确。 参考: 在mysql的官网可以得到想要的版本。

    选择要引入驱动的项目右键->建立路径->配置路径 在打开的包含的四个选项卡的窗口中选择 库 这个选项卡 选择 添加外部JAR包->ok 在你驱动JAR包存放的地方选择好你的JAR包->打开 测试连接代码:

    public class Dao {

    // 公用的数据库连接类

    public static Connection getConnection() throws Exception {

    String driverName = "com.mysql.jdbc.Driver"; // 加载JDBC驱动

    String dbURL = "jdbc:mysql://localhost:3306/test_scheme?auotReconnect=true&useUnicode=true&characterEncoding=utf8"; // 连接服务器和数据login

    // System.out.print(dbURL);

    String userName = "root"; // 用户名

    String userPwd = ""; // 密码

    Class.forName(driverName);

    return DriverManager.getConnection(dbURL, userName, userPwd);

    }

    public static void main(String []s){

    try {

    String sql="select * from test_table";

    PreparedStatement psmt=getConnection().prepareStatement(sql);

    ResultSet rs=psmt.executeQuery();

    while(rs.next()){

    System.out.println(rs.getString(1));

    }

    } catch (Exception e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

    2020-03-22 16:09:58
    赞同 展开评论 打赏
  • 我将mysql-connector-java-8.0.19.jar添加到Tomcat 7.0 / Lib中,现在可以正常工作了。我从图书馆以及所有地方将其删除,并且可以正常工作。我用相同的代码创建了新项目,并且没有将mysql-connector-java-8.0.19.jar添加到任何库或构建路径中,它运行良好。我在YouTube上的某处找到了添加到tomcat的说明。

    回答来源:Stack Overflow

    2020-03-23 09:43:21
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载

相关镜像