| Author |
how to set the the URL of database directly from the database.properties file
|
Maria Laxmi
Ranch Hand
Joined: Aug 08, 2008
Posts: 40
|
|
Hi, I am writing Unit test to test the DAO operation. For that I need to set the data source . I am doing that by using ds.setDriverClassName("someClassName") ds.setUsername("userName") ds.setPassword("userPassWord") ds.setUrl("urlOftheDataBse") dao.setDataSource(ds); Is there any way that we can set the data source directly from database.properties file (specially the URL). So, whenever there is any change in the URL of the database, it doesn't required to update manually in each test file. Thanks.
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26184
|
|
|
Well, you could write code that reads that information in from a property file. I think I'm missing the point of your question though.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Maria Laxmi
Ranch Hand
Joined: Aug 08, 2008
Posts: 40
|
|
Well, my question is how you can read the information from the property file? Thanks.
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26184
|
|
Originally posted by nis bootwala: Well, my question is how you can read the information from the property file?
Now that question I understand! Take a look at this code sample for reading in a property file.
|
 |
Maria Laxmi
Ranch Hand
Joined: Aug 08, 2008
Posts: 40
|
|
Hey, Thanks a lot. Solution works for me but there is only one problem. It works only if you give the absolute path for the property file e.g "C:/project/......./filename.properties". Is there any way I can just give "filename.properties" ? My property files are in WEB-INF folder.
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26184
|
|
Originally posted by nis bootwala: Is there any way I can just give "filename.properties" ? My property files are in WEB-INF folder.
Yes. You can use the getResourceAsStream() method. This article has an example.
|
 |
Maria Laxmi
Ranch Hand
Joined: Aug 08, 2008
Posts: 40
|
|
Thanks a lot for your help. But I have already tried using "getResourceAsStream()" in following different ways and it doesn't work. 1)properties.load(this.getClass().getResourceAsStream("/database.properties")); 2)properties.load(getClass().getResourceAsStream("database.properties")); 3)ClassLoader.getSystemResourceAsStream("/WEB-INF/config/database.properties"); this.getClass().getResourceAsStream("/database.properties"); ResourceBundle.getBundle("WEB-INF.config.database"); It throws NullPointer Exception. Thanks.
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26184
|
|
|
Moving to Java in General since this is now about classloading rather than testing.
|
 |
 |
|
|
subject: how to set the the URL of database directly from the database.properties file
|
|
|