Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connecting to number of databases.

 
jagan raja
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have got five different databases i.e 5 different hosts.

I have to connect to them and retrieve data without making a performance hit.

My Code for establishing connection with single database is as follows.


Can any one help me out ???
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the question?
 
jagan raja
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to establish connection with 5 different host's at a time?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do the above five times, with a different variable for each connection.
 
Vinod Tiwari
Ranch Hand
Posts: 466
1
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write a generic code and pass database details (which can be stored in some property of XML file) as parameter to establish connection....
 
jagan raja
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did that 5 times and it works fine.

But i want to know if there is any other simpler way of doing it.

Now my code is as below.


How to store these details in some property of XML file and use it?

Thanks in advance
 
Jan Cumps
Bartender
Posts: 2584
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(assuming this is a stand alone application)

You could create an XML file with contents like this:


And then, in your java code, use one of the XML APIs to read the file and create a connection for each occurrence of the <connection> tag.
You can store these connections in conArray.
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want several connections to the same database with the same login credentials? That is what you have. Or if the literal strings you posted were just examples, then you have a fixed number of connections.

I would have changed the makeConnection() method to pass the URL, username and password strings, and had the DriverManager.getConnection() method used the passed arguments. Or I would have wrapped all of this in a Connection wrapper class. Then each connection could use a different database and different credentials.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic