File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Connecting to number of databases. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Connecting to number of databases." Watch "Connecting to number of databases." New topic
Author

Connecting to number of databases.

jagan raja
Ranch Hand

Joined: Jan 29, 2010
Posts: 54
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

Joined: Apr 14, 2004
Posts: 10336

What is the question?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
jagan raja
Ranch Hand

Joined: Jan 29, 2010
Posts: 54
How to establish connection with 5 different host's at a time?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Do the above five times, with a different variable for each connection.
Vinod Tiwari
Ranch Hand

Joined: Feb 06, 2008
Posts: 466
    
    1

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....


Vinod Tiwari | Twitter | Shikshanirman | Guftgu
jagan raja
Ranch Hand

Joined: Jan 29, 2010
Posts: 54
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

Joined: Dec 20, 2006
Posts: 2516
    
  10

(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.


OCUP UML fundamental and ITIL foundation
youtube channel
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5845
    
    7

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.


JBoss In Action
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connecting to number of databases.