• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

connecting databases dynamically

 
dordije maric
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, help.

I am fairly new in Web Java programming and need guide how to solve next database connecting problem:

I must to work with many MySql databases named, for example, Somedata_1_2011, Somedata_1_2012, Somedata_2_2011,Somedata_3_2012, etc.
It can be many databases, up to Somedata_400_2012.

All databases have same structure, same number of tables with same number of fields.
I cant change databases (to group them in one database or something similar).

Well, for example I need to dynamically visit every Somedata_n_2012 database and collect some data and do some sum math with them.
Then I need to display data on web page.
In advance I do not know how many databases will be, I must chech if for example Somedata_14_2012 exists, and so on.

I would like to use JSF and JDBC but if you suggest something better ...


How can I dynamically create connections, read data, go to next database etc ?
Please advice.

Thank you in advance.

Dordije.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dordije,
Welcome to CodeRanch!

Using the "old fashioned" (non datasource) JDBC access would work. Like this. You can loop through database name/user/passwords stored in a file and connect to each in turn. Remember to encrypt the passwords!
 
dordije maric
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Dordije,
Welcome to CodeRanch!

Using the "old fashioned" (non datasource) JDBC access would work. Like this. You can loop through database name/user/passwords stored in a file and connect to each in turn. Remember to encrypt the passwords!




Thank you Jeanne.

I have more serious thing that confuses me lot off: how to organize all that in code ? I have read about JSF (also JSP) pages, backing beans, managed beans, controller classes and EJBs.
So, what parts of code to put in what classes ? There are many variations. You proposed using the non datasource JDBC access and I will stick with that.
Problem is that I can,t see biger picture.

I many articles I saw that we must have entity classes, and this is most clear to me.
Then, we can have some DAO classes which do database accessing job.

Also, we need class as backing bean for JSF page. As I saw in some examples that in this class I must inject EJB (@EJB of DAO class).
As I saw in backing bean we must have properties that we bind to some elements on JSF page.
In my case I must prepare some list and bind it to dataTable on JSF page.
Do I use annotations (JPA) or do job without using them ?

I think that you can see what bothers me.
Where to put code for preparing connection for database ? Where to put data access code, in same or in another class ?
In what class do loop through databases ?

Can you give me some rough code of mentioned classes that will constitute desired solution.
There are many variations, also old and new Java APIs and technologies. How to organize ?

Thanks again.

Dordije.

 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome,

That's a lot of questions...

Is this an existing application or brand-new?

WP
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dordije maric wrote:Can you give me some rough code of mentioned classes that will constitute desired solution.
There are many variations, also old and new Java APIs and technologies. How to organize ?

This sounds like you are asking for the code. As far as organizing - you have layers. If using JDBC, you have a DAO (not an @Entity.)

I recommend trying one thing at a time. Get the JDBC working first. Or get the JSF working first but don't call the JDBC yet. The idea is to get one thing working at a time so when problems show up, you know the most recent change is what broke it.
 
dordije maric
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you Jeanne again.

Yes it looks like I asked for code. Sorry, I didn't mean anybody to code for me but to help me in how to organize code among mentioned types of classes and Java technologies.
As rough code I mean this type of code: put entities in entity java bean (for example entity book), make connection in this type of class, put data access code in that type of class, etc.

Before I made this post I read many articles and two e-books about Java. I realized that there are several Java tools and technologies to do same job.
As in this forum participate many expirienced Java developers, I thought that somebody can advice me what Java technologies to use and how to organize project classes.
In this way for me is also valuable your statement: If using JDBC, you have a DAO (not an @Entity).

To William: databases are old, I worked on them with Harbour compiler without any problems. But I wish to learn Java and migrate to Web level.
As I am fresh new in Java I have many questions and dilemmas.

Best regards to all,

Dordije.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It wasn't just the code. It was also the question was a bit overwhelming/vague. Some tips to get started:

1) You use either JDBC or JPA. For what you want, JDBC is better because you are switching databases at runtime.
2) You don't need an ejb
3) It is good to have a dao
4) Write and test the back end code before starting with JSF.
 
dordije maric
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeanne.

That is guide I needed !

Yesterday I started with small steps. I have created one entity-like class and class that prepares list of entities.
Also created one JSF page with dataTable element binding it to list of entities.
I can display data on that page.

Now I will try to change second class to use JDBC and connect to one of databases, and see what I will get.

Best regards,

Dordije.




 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic