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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Connecting to number of databases." Watch "Connecting to number of databases." New topic

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

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

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: 459
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
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

Joined: Dec 20, 2006
Posts: 2491

(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

Joined: May 14, 2008
Posts: 5812

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.
Similar Threads
Hi,I have a problem in database connection
Optimal JDBC connection pooling configuration for tomcat server
How to establish JDBC Connection in JSF 2.0 using Oracle Database ?
Connecting to a remote mysql database
need java logic