JSP使用MySQL数据库报错java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

错误

在JavaWeb中的JSP中使用MySQL数据库报错找不到 com.mysql.jdbc.Driver

错误日志如下:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1858)
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1709)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:264)
	at utils.DBUtils.getConnection(DBUtils.java:14)
	at dao.UserDao.selectAll(UserDao.java:27)
	at servlet.UserServlet.doPost(UserServlet.java:28)
	at servlet.UserServlet.doGet(UserServlet.java:47)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
	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:170)
	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:423)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
	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(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

原因

有经验的都明白其实是没有添加第三方加包,但是这个项目中是添加了的,只是没有找到而已。

查看项目结构:

第三方加包是添加在External Libraries下的,而添加方式是如下这种:

这在JavaWeb中是不可采取的,读取不到jar包的路径。

解决

web/WEB-INF目录下创建一个lib文件夹,然后把jar包复制到该文件夹下,即可成功使用数据库。

posted @ 2021-11-22 21:42  RioTian  阅读(159)  评论(0编辑  收藏  举报