问答详情
源自:2-3 JDBCUtil开发

我的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;
   }

提问者:Saving_ 2017-05-10 17:48

个回答

  • loness
    2018-06-30 18:57:14

    可以将代码改成

    ```

        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");

    ```


  • Echo鑫
    2017-05-13 16:52:22

    一样的问题

  • rover99x
    2017-05-11 07:19:23

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