File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Using Properties File to set database path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Using Properties File to set database path" Watch "Using Properties File to set database path" New topic
Author

Using Properties File to set database path

Simon O'Brien
Greenhorn

Joined: Nov 21, 2007
Posts: 7
Hi All

I am using the singleton pattern for my DatabaseAccess class.

I want to be able to pass the database path and filename to the DatabaseAccess class when it is first created however I DO NOT want to have to pass the database path and filename as parameters in my DatabaseAccess.getInstance() call as I feel this would be confusing and ugly.

Would it be acceptable to pass the database path and filename to my DatabaseAccess class using the suncertify.properties file or do you think this is a bad idea ie:

Musab Al-Rawi
Ranch Hand

Joined: Aug 06, 2007
Posts: 231
Hi,

Ok may be I am just addicted to using the system properties more than I should, but i find using them is an easy way to communicate this kind of constat (mostly unchanged) values as follows:

System.getProperties().setProperty(property_name, property_value);
&
String value = System.getProperties().getProperty(property_name);


SCBCD - SCWCD - SCJD - SCJP - OCA
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Hi,

personally, I pass the filename as parameter in getInstance...

I'm would be against using the suncertify.properties for that... which would be very ugly to me...
It also means your application wouldn't work without this file.
I feel it is a trick to go around Object Oriented..

I defined :
- getInstance(String file), throwing IllegalArgumentException if someone tries to create another instance..
- getInstance() for subsequent use.

Regards,
Alex
Simon O'Brien
Greenhorn

Joined: Nov 21, 2007
Posts: 7
Thanks for the replies.

Alex, you said that you ....


I defined :
- getInstance(String file), throwing IllegalArgumentException if someone tries to create another instance..
- getInstance() for subsequent use.


I was thinking of doing this but my worry was that having a getInstance(String file) would be confusing as it implies that it is possible to get another instance using a different database file.

I was going to create a 'PropertiesManager' which would create the suncertify.properties property file if it doesn't exist when a 'getProperty' or 'setProperty' is called. So a example flow for the database would be ...

1) Network Client UI Starts
2) Network Client UI calls 'PropertyManager.getProperty("DB_FILE_LOC")' to try and get saved database file location
3) Property manager cant find suncertify.properties so it creates it
4) 'PropertyManager.getProperty("DB_FILE_LOC")' returns "" as the property doesn't exist
5) User has to enter database file location into the Network Client UI
6) User presses 'Start/OK'
7) Network Client UI calls 'PropertyManager.setProperty("DB_FILE_LOC", dbFileLocValue)' to set saved database file location

What do you think?

Thanks again.

Simon
bao do
Greenhorn

Joined: Jan 07, 2008
Posts: 4
Hello,

In my assignment, it says

"Your programs must not require use of command line property specifications. All configuration must be done via a GUI, and must be persistent between runs of the program. Such configuration information must be stored in a file called suncertify.properties which must be located in the current working directory."

So let's say I start up the app in "server" mode, and this server allows to choose a db file to run which I then save in my suncertify.properties. If I run the app again, do I use that same db file and not ask the user which database file to use ?

Right now, I'm implementing it such that every time the app is ran in Alone mode or Server mode, it asks the user to choose a db file to use. If this is right, why would I need to save the db file path in the suncertify.properties ?

Thanks

Bao
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Originally posted by Simon O'Brien:
Thanks for the replies.
I was thinking of doing this but my worry was that having a getInstance(String file) would be confusing as it implies that it is possible to get another instance using a different database file.



If this method is called again, I throw a runtime exception, the method without argument must be used. I don't know how much it's a good approach however.

I personally don't like the idea of using properties to communicate this information.
[ January 08, 2008: Message edited by: Alex Belisle Turcot ]
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Originally posted by bao do:
Hello,
So let's say I start up the app in "server" mode, and this server allows to choose a db file to run which I then save in my suncertify.properties. If I run the app again, do I use that same db file and not ask the user which database file to use ?



For me, I automatically populate the fields in the user interface with the data from the property file. The user is still free to select another database, but if he wishes to keep the same, he only clicks ok.

Similar to how Internet Explorer populates web pages with your username/pwd, but then you still need to click OK or can choose to enter a different user/pwd.

Regards,
Alex
[ January 08, 2008: Message edited by: Alex Belisle Turcot ]
bao do
Greenhorn

Joined: Jan 07, 2008
Posts: 4
"For me, I automatically populate the fields in the user interface with the data from the property file. The user is still free to select another database, but if he wishes to keep the same, he only clicks ok."

I understand your point, but the requirement states "configuration must be persistent between runs of programs", so it seems to mean that everytime they run the app, it must uses the same db file. Your way would allow users to choose another db file which goes against the requirement. Is it not ?
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Hi,

I don't agree, I use a singleton, so If I automatically open the database, it means that the user will NEVER be able to connect another database.

Unless, I guess, if the Data singleton is not linked to the database file.

Since the last used information are automatically populated, I feel it's being consistent.

What other people think ?

Regards,
Alex
Rafael Prado
Ranch Hand

Joined: May 17, 2003
Posts: 33
Originally posted by bao do:
"the requirement states "configuration must be persistent between runs of programs", so it seems to mean that everytime they run the app, it must uses the same db file.


Doesn't it mean "everytime they run the app, what was selected as configuration in the previous run should still be in the file for the next run"? It's a bit different from what you're saying.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using Properties File to set database path
 
Similar Threads
Is this a valid Singleton wrapper class
B&S 2.2.2. - Thread Safe Access to File
Initial design review
Composition
Dependency injection for database server