我嘗試了許多可用的相關問題的答案,但沒有一個是成功的。db.properties - 空指針異常
這裏是我的問題,
我有一個動態的Web應用程序,它使用Oracle數據庫檢索數據。爲了讓漂亮的我使用的是db.properties配置文件,使數據源,
下面是dataFactory.class代碼:
public DataSource getDatasource() {
Properties props = new Properties();
FileInputStream fis = null;
InputStream in = null;
OracleDataSource oracleDS = null;
try {
fis = new FileInputStream("db.properties");
props.load(fis);
oracleDS = new OracleDataSource();
oracleDS.setURL(props.getProperty("url"));
oracleDS.setUser(props.getProperty("user"));
oracleDS.setPassword(props.getProperty("password"));
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return oracleDS;
}
,並讓我使用它EmployeeDao連接.class,
DataSource ds;
EmpDatasourceFactory empDs = new EmpDatasourceFactory();
Connection conn;
public boolean insertRecord(Employee emp) {
boolean status = false;
ds = empDs.getDatasource();
try {
conn = ds.getConnection(); //Getting the null point exception
}
}
注意:請注意,我有必要的所有其他代碼。
和問題,當我做一個java文件,並做出一些方法,並使用它來建立連接它工作正常,我的意思是當它作爲一個Java應用程序運行。
但是,當用於employeeDao.java並使用apache tomcat服務器運行它時,它會給我一個錯誤。請幫忙。!!!
那麼,經過一些修改後,工作。感謝那。 我已將我的方法包含在答案框中。謝謝。 – KingFeming 2014-09-01 05:32:52
對於任何不知情的讀者,我已將「ResourceBundle.get」更正爲「ResourceBundle.getBundle」。 – 2014-09-01 07:16:53
謝謝你@Joop Eggen – KingFeming 2014-09-01 09:56:38