java.lang.NoClassDefFoundError:mysql数据库连接驱动 -问答-阿里云开发者社区-阿里云

开发者社区> montos> 正文

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

2020-03-22 15:01:44 502 3

我试图运行简单的代码与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

取消 提交回答
全部回答(3)
  • 海贼王fans
    2020-03-25 16:38:39

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

    0 0
  • Mr.z
    2020-03-22 16:09:58

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

    }

    }

    0 0
  • montos
    2020-03-23 09:43:21

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

    回答来源:Stack Overflow

    0 0
添加回答
相关问答

17

回答

【大咖问答】对话PostgreSQL 中国社区发起人之一,阿里云数据库高级专家 德哥

阿里ACE 彭飞 2019-07-10 09:36:10 962354浏览量 回答数 17

162

回答

惊喜翻倍:免费ECS+免费环境配置~!(ECS免费体验6个月活动3月31日结束)

豆妹 2014-10-29 17:52:21 222937浏览量 回答数 162

145

回答

【新手入门】云服务器linux使用手册

fanyue88888 2012-11-26 17:14:18 157038浏览量 回答数 145

8

回答

OceanBase 使用动画(持续更新)

mq4096 2019-02-20 17:16:36 333784浏览量 回答数 8

110

回答

OSS存储服务-客户端工具

newegg11 2012-05-17 15:37:18 292226浏览量 回答数 110

22

回答

爬虫数据管理【问答合集】

我是管理员 2018-08-10 16:37:41 146742浏览量 回答数 22

18

回答

阿里云开放端口权限

xcxx 2016-07-20 15:03:33 643011浏览量 回答数 18

38

回答

安全组详解,新手必看教程

我的中国 2017-11-30 15:23:46 256828浏览量 回答数 38

249

回答

阿里云LNAMP(Linux + Nginx + Apache + MySQL + PHP)环境一键安装脚本

云代维 2014-02-14 15:26:06 302557浏览量 回答数 249

2

回答

区域选择帮助

fanyue88888 2012-12-07 15:54:30 203977浏览量 回答数 2
+关注
3
文章
5115
问答
问答排行榜
最热
最新
推荐问答
相关电子书
更多
文娱运维技术
立即下载
《SaaS模式云原生数据仓库应用场景实践》
立即下载
《看见新力量:二》电子书
立即下载