第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定

我的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 回復(fù) 有任何疑惑可以回復(fù)我~
#1

慕容5618489

1:數(shù)據(jù)庫表里面是否有數(shù)據(jù) 2:配置文件是否寫清楚
2019-01-16 回復(fù) 有任何疑惑可以回復(fù)我~

一樣的問題

0 回復(fù) 有任何疑惑可以回復(fù)我~

如果運行在Windows下,property 的加載不要用getResource而是直接用new File. 好像Windows下不支持這種classpath的搜索。在Linux和Unix上都不會有問題。

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消

我的JDBC使用硬編碼配置沒問題用配置文件引入老是報空

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號