我的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;
? ?}
2018-06-30
可以將代碼改成
```
? ? 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");
```
2017-05-13
一樣的問題
2017-05-11
如果運行在Windows下,property 的加載不要用getResource而是直接用new File. 好像Windows下不支持這種classpath的搜索。在Linux和Unix上都不會有問題。