• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Initializing static final fields from values in a database

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am developing a web app and need to store text to be displayed in a database so they can be changed without changing the java code.
Obviously, I don't want to make a database call every time a user requests a page.
I was thinking of creating a class with static final data members to represent each phase that will be displayed. The class will have a static init block that makes a database call to retrieve the stored text and and assign each static final data member a value.
Will this work? Is this a good way to do this or is there a better way ( Singleton? ).
Thanks!
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure that will work. However depending on the stability of the text that you are worrying about you could consider just using a properties file and reading it in as the application fires up.
However this would require refreshing that properties file everytime that you want a change - so the database idea may be a better one.
 
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a thought, but isn't initializing from the database a little risky? What if the database is unavailable or a database exception occurs? If you got an exception while accessing the database, how would you handle it? Would you then give the static final variables default values or would you have to exit the application?
Junilu
 
Roger Graff
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am thinking the class would handle an exception by setting default values.
Thanks for the input guys!
I figured I would init the fields to a hard-coded value in a catch block if an exception occured in the static init block.
How about making the fields private static (non-final) so I could update their values (from the database )after a given amount of time. Of course the class would then need static accessor methods.
JDK 1.3 has a Timer class that could do the job of scheduling the updates, but I'm using JDK 1.2
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should probably take a look at java.util.ResourceBundle and java.util.PropertyResourceBundle
 
We must storm this mad man's lab and destroy his villanous bomb! Are you with me tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic