解决在Tomcat服务器上报java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver的问题。
报错的代码:

java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1407)
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1215)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:264)
	at com.zhang.dao.BaseDao.getCoon(BaseDao.java:29)
	at com.zhang.dao.BaseDao.select(BaseDao.java:69)
	at com.zhang.dao.UserDao.doLogin(UserDao.java:32)
	at com.zhang.service.UserService.doLogin(UserService.java:26)
	at com.zhang.servlet.LoginServlet.doPost(LoginServlet.java:29)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:681)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:889)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
	at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

第一步: 首先你检查driver的值,在com.mysql.cj.jdbc.Driver的双引号里面不能有任何的空格

String driver = "com.mysql.cj.jdbc.Driver";

第二步: 如果此方法不能解决,就检查mysql-connector-java-8.0.18.jar是否加入(注意看此驱动上的箭头)
在这里插入图片描述
如果没有箭头,那么就鼠标单击右键,然后点击Add as Library。
在这里插入图片描述
然后点击OK后,此时驱动的依赖就添加成功了
在这里插入图片描述
第三步: 如果此时还是报加载驱动异常,那么在Project Structure中在Artifacts中相应的目录的lib下看有没有导入mysql 的驱动依赖,如过没有,导入即可(我的问题就是导入就好了)。
在这里插入图片描述

Logo

快速构建 Web 应用程序

更多推荐