我是靠谱客的博主 狂野缘分,这篇文章主要介绍java.lang.ClassNotFoundException: com.mysql.jdbc.Driver,现在分享给大家,希望可以做个参考。

连接数据库时经常出现的异常就是java.lang.ClassNotFoundException: com.mysql.jdbc.Driver。所以今天我结合自己的经验给大家总结一下,碰到这类的异常要如何处理,首先给大家分享一下我在用MyBatis逆向生成时碰到到的这个异常。

异常信息如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
java.lang.RuntimeException: Exception getting JDBC Driver at org.mybatis.generator.internal.JDBCConnectionFactory.getDriver(JDBCConnectionFactory.java:100) at org.mybatis.generator.internal.JDBCConnectionFactory.getConnection(JDBCConnectionFactory.java:70) at org.mybatis.generator.config.Context.getConnection(Context.java:753) at org.mybatis.generator.config.Context.introspectTables(Context.java:631) at org.mybatis.generator.api.MyBatisGenerator.generate(MyBatisGenerator.java:257) at org.mybatis.generator.api.MyBatisGenerator.generate(MyBatisGenerator.java:139) at com.MyBatis.Test.create(Test.java:22) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.junit.runner.JUnitCore.run(JUnitCore.java:160) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:348) at org.mybatis.generator.internal.ObjectFactory.internalClassForName(ObjectFactory.java:181) at org.mybatis.generator.internal.ObjectFactory.externalClassForName(ObjectFactory.java:136) at org.mybatis.generator.internal.JDBCConnectionFactory.getDriver(JDBCConnectionFactory.java:97) ... 28 more

异常原因:pom.xml配置文件中未添加mysql依赖

解决方法:pom.xml配置文件中标签中添加mysql依赖

复制代码
1
2
3
4
5
6
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency>

总结下出现com.mysql.jdbc.Driver异常的原因:

首先可以肯定的是这个异常就出在数据库的连接上,那我们逐个排查与数据库连接的代码总会找到异常所在。如果你实在是找不到异常所在,就把与数据库有关的代码注释起来,重新再写一遍,可能就解决问题了。下面总结如下:

1. mysql四大参数容易出错,比如少个字母或者多个字母都不行,可参考如下:
复制代码
1
2
3
4
5
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/E3303 jdbc.username=root jdbc.password=root
2.mysql的依赖没有添加,有时我们可能粗心大意,忘记了,这个时候回去检查一下。
3.有时我们会用到c3p0-config.xml配置文件,文件名必须这么命名,而且文件位置必须放到src目录下,否则也会报类似异常。

先写到这里,后续碰到了会补充,希望对大家有帮助。

最后

以上就是狂野缘分最近收集整理的关于java.lang.ClassNotFoundException: com.mysql.jdbc.Driver的全部内容,更多相关java.lang.ClassNotFoundException:内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部