第一次测试都没过。。感觉代码都是一样的啊有没有大佬帮我看看

来源:2-3 JDBCUtil开发

慕九州6528401

2018-07-29 19:24

package com.imooc.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/*
JDBC工具类
 */
public class JDBCUtil {
    /*
    获取Connection
    @return所获得的JDBC的Connection
     */
    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        String driverClass="com.mysql.jdbc.Driver";
        String url="jdbc:mysql:///spring_data";
        String user="root";
        String password="";
       
        Connection connection=DriverManager.getConnection(url, user, password);
        return connection;
    }
}
package com.imooc.util;

import org.junit.Assert;
import org.junit.Test;
import java.sql.Connection;

public class JDBCUtilTest {
    @Test
    public  void  testGetConnection() throws  Exception{
       
        Assert.assertNotNull(connection);
    }
}

报的是下面这两个代码里我加粗了

at com.imooc.util.JDBCUtil.getConnection(JDBCUtil.java:20)

at com.imooc.util.JDBCUtilTest.testGetConnection(JDBCUtilTest.java:10)


写回答 关注

4回答

  • 叫我王大锤
    2018-07-29 20:14:20
    已采纳

    我的老哥 正确的问问题的方式是 贴出来报错信息 你贴出来代码 别人几乎不看的 报错信息最直观 其实你什么地方错了 仔细看报错信息就好了 你这个报错信息太少了 报错信息要贴的话 全一点


    目测你这个连接的时候就错掉了 就没有连接上

    大概扫了下你代码 好像是密码没有写 又或者没有导包?又或者导的驱动包不支持你的数据库版本?

    具体问题还要看详细的报错信息

    叫我王大锤 回复慕九州652...

    ClassNotFoundException: com.mysql.jdbc.Driver 出错了多自己找错 并且要找到方法 你出错那么多行信息 其实这行最关键 其他都是这个导致的错误 这行说明 驱动有问题 没有找到驱动包

    2018-07-30 20:25:32

    共 3 条回复 >

  • 慕容5618489
    2019-01-16 14:58:17
    1:String driverClass="com.mysql.jdbc.Driver"
    你的driverClass是哪里来的,你缺少一个驱动 就Class.forname(driverClass);


  • 慕九州6528401
    2018-07-30 09:54:38

    "C:\Program Files\Java\jdk1.8.0_121\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\idea\IntelliJ IDEA 2018.1.6\lib\idea_rt.jar=6769:D:\idea\IntelliJ IDEA 2018.1.6\bin" -Dfile.encoding=UTF-8 -classpath "D:\idea\IntelliJ IDEA 2018.1.6\lib\idea_rt.jar;D:\idea\IntelliJ IDEA 2018.1.6\plugins\junit\lib\junit-rt.jar;D:\idea\IntelliJ IDEA 2018.1.6\plugins\junit\lib\junit5-rt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sqljdbc41.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\rt.jar;D:\springdata\target\test-classes;D:\springdata\target\classes;C:\Users\41545\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\41545\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.imooc.util.JDBCUtilTest,testGetConnection


    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:264)

    at com.imooc.util.JDBCUtil.getConnection(JDBCUtil.java:18)

    at com.imooc.util.JDBCUtilTest.testGetConnection(JDBCUtilTest.java:8)

    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:50)

    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)

    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)

    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)

    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)



    Process finished with exit code -1


  • 慕九州6528401
    2018-07-29 20:04:32

    加粗的两行不见了。。

    Class.forName(driverClass);
    Connection connection = JDBCUtil.getConnection();

轻松愉快之玩转SpringData

利用Spring Data提高开发效率,提升程序员的幸福指数

34087 学习 · 119 问题

查看课程

相似问题