數據庫鏈接異常

鏈接數據庫時常常出現的異常就是java.lang.ClassNotFoundException: com.mysql.jdbc.Driver。因此今天我結合本身的經驗給你們總結一下,碰到這類的異常要如何處理,首先給你們分享一下我在用MyBatis逆向生成時碰到到的這個異常。java

異常信息以下

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.BlockJUnit4Cla***unner.runChild(BlockJUnit4Cla***unner.java:70)
    at org.junit.runners.BlockJUnit4Cla***unner.runChild(BlockJUnit4Cla***unner.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依賴mysql

解決該類異常方向

  1. 看了pom.xml文件中是否導入了 「mysql-connector-java」 這個jar包
  2. 若是pom.xml文件中含有jar包信息,檢查是否已經導入jar包
  3. 查看數據庫鏈接信息是否有細微的小錯誤,mysql四大參數容易出錯,好比少個字母或者多個字母都不行,可參考以下:
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/**
    jdbc.username=root
    jdbc.password=root
相關文章
相關標籤/搜索