gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:56:29 25 4
gpt4 key购买 nike

我正在尝试运行简单的代码来连接 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>

最佳答案

您的运行时 CLASSPATH 中缺少 JDBC 驱动程序库,即 Tomcat 在其中查找所需类以执行编译后的代码的 CLASSPATH。

关于javax.servlet.ServletException : java. lang.NoClassDefFoundError:com/mysql/jdbc/Driver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60707549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com