为了账号安全,请及时绑定邮箱和手机立即绑定

我的JDBC使用硬编码配置没问题用配置文件引入老是报空

java.lang.NullPointerException

at com.imooc.hanxiu.JDBCutils.JDBCUtil.getConnection(JDBCUtil.java:33)

at test.java.com.imooc.hanxiu.testApp.testConnection(testApp.java:19)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)

at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)

at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)

at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)





我的代码如下  public static Connection getConnection() throws Exception {

       /**
        * 不建议大家把配置硬编码到代码中
        *
        * 最佳实践:配置性的建议写到配置文件中
        */
//        String url = "jdbc:mysql:///springdata";
//        String user = "root";
//        String password = "123";
//        String driverClass = "com.mysql.jdbc.Driver";
       String classpath = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
       String fileName = classpath + "db.properties";
       System.out.print(fileName);
       FileInputStream fis = new FileInputStream(fileName);
       Properties properties = new Properties();
       properties.load(fis);
//          错误的文件引入待解决
       String url = properties.getProperty("jdbc.url");
       String user = properties.getProperty("jdbc.user");
       String password = properties.getProperty("jdbc.password");
       String driverClass = properties.getProperty("jdbc.driverClass");

       Class.forName(driverClass);
       Connection connection = DriverManager.getConnection(url, user, password);
       return connection;
   }

正在回答

3 回答

可以将代码改成

```

    public static Connection getConnection() throws IOException, SQLException, ClassNotFoundException {

        InputStream is = JDBCUtil.class.getResourceAsStream("/db.properties");

        Properties properties = new Properties();

        properties.load(is);


        String url = properties.getProperty("jdbc.url");

        String driver = properties.getProperty("jdbc.driver");

        String user = properties.getProperty("jdbc.user");

        String password = properties.getProperty("jdbc.password");


        Class.forName(driver);

        Connection connection = DriverManager.getConnection(url, user, password);

        return connection;

    }

```

主要注意第一行的修改

```

InputStream is = JDBCUtil.class.getResourceAsStream("/db.properties");

```


0 回复 有任何疑惑可以回复我~
#1

慕容5618489

1:数据库表里面是否有数据 2:配置文件是否写清楚
2019-01-16 回复 有任何疑惑可以回复我~

一样的问题

0 回复 有任何疑惑可以回复我~

如果运行在Windows下,property 的加载不要用getResource而是直接用new File. 好像Windows下不支持这种classpath的搜索。在Linux和Unix上都不会有问题。

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

我的JDBC使用硬编码配置没问题用配置文件引入老是报空

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信