aspose file tools
The moose likes Java in General and the fly likes how to set the the URL of database directly from the database.properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "how to set the the URL of database directly from the database.properties file" Watch "how to set the the URL of database directly from the database.properties file" New topic
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
    
  66

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
    
  66

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
    
  66

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
    
  66

Moving to Java in General since this is now about classloading rather than testing.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: how to set the the URL of database directly from the database.properties file
 
Similar Threads
Help in connecting to oracle10 database using struts
properties file fails to load
How to get the path of property file.
Spring + JPA/Hibernate + Persistence.xml + applicationContext.xml + ANT hbm2ddl