my dog learned polymorphism
The moose likes JDBC and Relational Databases and the fly likes Factory concept to provide DB transparency Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Factory concept to provide DB transparency" Watch "Factory concept to provide DB transparency" New topic

Factory concept to provide DB transparency

Neha Yadav

Joined: Aug 26, 2004
Posts: 5
Hi All

My software needs to connect to any database, whose name is provided dynamically at run time. I have java prg packaged as .jar files for kind of database which provide connection to it.

The problem is i need to present a generic interface using Factory concept which should take care of interacting with the appropriate .jar file depending upon the DB dynamically.

Could someone please tell me how this can be done?
My code needs to independent of database xml/relational and also independent of any JDBC code. I have not implemented this abstraction.

Thank you
[ August 27, 2004: Message edited by: Neha Yadav ]
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Hi Neha,

If you've already implemented your DB connectivity code then I think the optimum solution will be somewhat dependent on how you've done it.

The basic idea of a class factory is that, based on some information you supply either as a parameter or through some kind of configuration, it will manufacture that right kind of object of a given type.

If your database code (SQL & JDBC) are completely platform neutral then the only differences between databases will be the JDBC Driver and the connection string. You can abstract this further by using a javax.sql.DataSource (a Connection factory) with JNDI.

Otherwise you'll probably need to map your DAOs to the appropriate database, say in a properties file, and create a factory class which performs the lookup for the requested DAO, creating your object with something like Class.forName("com.myorg.myapp.dao.MyDAO").newInstance().

Does that make any sense?

I agree. Here's the link:
subject: Factory concept to provide DB transparency
It's not a secret anymore!