• Post Reply Bookmark Topic Watch Topic
  • New Topic

Proper way to store and retrieve information  RSS feed

 
Dilip kumar
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In my J2EE application I'm connecting to 4 different databases based on the criteria selected by the user. Currently I'm storing all the info regarding these database in the array and using iteration to find the database name and other info. I have the feeling that this is not the proper way. I have to change the array size and add more entries if I need to add more databases. I would appreciate if anyone can suggest me a Object Oriented way of doing this.

Thanks
 
Charlie Goth
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
collections
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that sounds like a good thing to be worried about. I hope we can help.

You said J2EE ... is that servlets only or are you connecting from an EJB container?

How similar or different are the 4 databases? Can you get a connection to any one and run the same SQL or are they structurally different?

How many places do you have code that picks one database from the array? The answer I'm hoping for is "1".

How do you pick a database based on criteria? I guess that's not really important to the solution, but I'm curious if it's straightforward or takes some logic to figure out.
 
Dilip kumar
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response.

- No EJBs. Just servlets and JSPs.
- Queries are little bit different for each database.
- I need to pick up the database info every time the user picks up different criteria but at only one place.
- I need to connect to different database based on the location selected by user.
[ June 02, 2005: Message edited by: Dilip kumar ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could go so far as to make a data accessor for each query for each database. Then map the user's criteria to a logical database name somehow and get the right accessor from a factory:

Now you can add more databases and add them to the factory through configuration without ever touching this code. On the down side you may have to write a whole bunch of DAs, but if you can abstract most of tohe tricky stuff to an abstract base class the concrete classes might only provide connection info and SQL.

I use this kind of solution so much I worry that every problem looks like a nail just because I love my hammer. Let us know what you wind up with!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!