错误:“java.lang.ClassNotFoundException: com.mysql.jdbc.Driver“ 如何解决?

——运行项目时遇到 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 问题,解决方案:

错误如下:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1358)
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1180)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:259)
	at com.niuyun.dao.BaseDao.getConnection(BaseDao.java:44)
	at com.niuyun.service.user.UserServiceImpl.login(UserServiceImpl.java:26)
	at com.niuyun.servlet.user.UserServlet.doGet(UserServlet.java:31)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at com.niuyun.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:18)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:615)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:818)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1626)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:745)

解决方案如下:

1.首先检查了一下连接数据库的配置信息是否写错?

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306~~~
username=
password=

莫有啊,写的没有毛病~

2.检查了一下pom.xml配置中是否没有导入相对应的依赖?

 <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.49version>
    dependency>

莫有啊,导入依赖了~
错误:“java.lang.ClassNotFoundException: com.mysql.jdbc.Driver“ 如何解决?_第1张图片
也有了呀,但是错误依旧~

3.查看Maven仓库中是否有没下载的包?发现也有啊!(以下是我本地的仓库)

错误:“java.lang.ClassNotFoundException: com.mysql.jdbc.Driver“ 如何解决?_第2张图片

4.查看Tomcat中的lib目录下是否有相对应的jar包,发现确实没有(服务器运行所需要的jar包)。

错误:“java.lang.ClassNotFoundException: com.mysql.jdbc.Driver“ 如何解决?_第3张图片
解决:将连接数据库的包导入,可直接复制你的仓库目录中的jar包,将它放置在lib目录下,即可!

5.问题终于找到了,解决后;重新启动服务器,运行项目,成功了;

(有时候导入包,它可能导不进来,需要手动导入一下)。


希望可以帮助到您,解决问题

~~~感谢您的光临~~~

你可能感兴趣的:(Javaweb,javaweb,tomcat,jdbc)