• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to set the the URL of database directly from the database.properties file

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Maria Laxmi
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, my question is how you can read the information from the property file?

Thanks.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Java in General since this is now about classloading rather than testing.
 
reply
    Bookmark Topic Watch Topic
  • New Topic