This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes JDBC and the fly likes Multiple database & multiple connection problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Multiple database & multiple connection problem." Watch "Multiple database & multiple connection problem." New topic
Author

Multiple database & multiple connection problem.

Anand Karia
Ranch Hand

Joined: Sep 25, 2004
Posts: 156

Dear All!

Wht should i ve 2 do DBConnection class if i ve multiple databases.

like i ve
1. dbAccount04
2. dbSale04
3. dbPurchase04

While working in dbAccounts i also have 2 get result from dbSale and dbPurchase in single form.

plz be informed that these 3 database are changed yearly as u can c "04" at last.

Plz guide me.


Love is GOD and GOD is Love.
Anand Karia
IT Concretor.......
M/s. Anand Karia Concreting IT
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

You simply need to create multiple instances of your Database connection methodology, such as a DB Connection Pool or an implementation of javax.sql.Connection. When constructing/instantiating the different instances, you would pass in the information specific to each database.

By the way, if you are not using a Connection Pool, you really should. Take a look at the Jakarta Commons DBCP project for one of many available DB Connection Pools.

If you still have questions, post some of the code you are using to make a single connection and someone can help point you in the correct direction.

Lastly, as courtesy, it is helpful when making posts if you use complete words and sentences, and not use abbreviation or "shorts" common on most other forums and Instant Messaging. There are two main reasons why: 1) JavaRanch has a large international user base where English is their second language. Therefore, it is enough of a challenge for them without the use of things like "as u can c" and "Plz". 2) Using these abbreviations and shorts make your post harder to read and understand and give it a less professional presentation. Members here take pride in JavaRanch having a very professional atmosphere. Some users will not even read a post if they see the user using these types of abbreviations. Therefore, it is in your best interest, to be assured the most number of replies, not to use them. Thanks
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Moving this to the JDBC forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60045
    
  65

Lastly, as courtesy, it is helpful when making posts if you use complete words and sentences,


It's much more than a courtesy, it will help you get your questions answered. Most people, myself included, will stop reading a post as soon as I hit a "u" or "ur" or other senseless abbreviation. After all, if you are not going to bother to take the time to make your post legible, why should anyone take the time to respond to it?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Anand Karia
Ranch Hand

Joined: Sep 25, 2004
Posts: 156

Here is my DBConnection class. In this database, database name is fixed but in another dbConnDetail, it changes based on selection after login form.


/////////////////////////////////////////
// DBConnection.java //
/////////////////////////////////////////

import java.sql.*;
public class DBConnection {
private static DBConnection instance;
public Connection Conn;
public static String strUserId,strDatabase;

/** Creates a new instance of DBConnection */
public DBConnection() {
try{
Class.forName("org.postgresql.Driver");
Conn = DriverManager.getConnection("jdbc ostgresql://192.168.0.1:5432/masterdb","postgres","postgres");
System.out.println("Connection Established.........");
}
catch(Exception e){
System.out.println("Connection problem "+ e );
}
}
--------------------------------------
public static synchronized DBConnection getInstance() {
if(instance == null) {
instance = new DBConnection();
}
return instance;
}

------------------------------------------
public void setUserID(String str) throws SQLException
{
this.strUserId = str;
}
public String getUserID() throws SQLException
{
return strUserId;
}

--------------------------------------------------
public void setDatabase(String str) throws SQLException
{
this.strDatabase = str;
}
public String getDatabaseID() throws SQLException
{
return strDatabase;
}

}


//////////////////////////////////////////////

THIS IS ANOTHER CLASS


/////////////////////////////////////////
// dbConnDetail.java //
/////////////////////////////////////////


import java.sql.*;
public class dbConnDetail {
private static dbConnDetail instance;
DBConnection cnn = DBConnection.getInstance();
public Connection myDetailCon;
public static String strUserId;
public String database = "masterdb";
public String dbUserID = "postgres";
public String dbPassword = "postgres";
/** Creates a new instance of DBConnection */
public dbConnDetail() {
try{
dbConnDetails(cnn.getDatabaseID());

/////////////////////////////////////////////////////
////// HERE GETDATABASE IS CALLED FROM DBConnection. This function is set ////// through loading of login form.
////// cnn.setDatabase(strDataBaseName);
/////////////////////////////////////////////////////


}
catch(Exception e)
{System.out.println(e);}
}
----------------------------------------------
public static synchronized dbConnDetail getInstance() {
if(instance == null) {
instance = new dbConnDetail();
}
return instance;
}
--------------------------------------------
public Connection dbConnDetails(String DataBase) {
Connection myDetailCon = null;

try{
Class.forName("org.postgresql.Driver");
String url = "jdbc ostgresql://192.168.0.1:5432/" +DataBase ;
System.out.println(url);
myDetailCon = DriverManager.getConnection(url, dbUserID, dbPassword);
System.out.println("Detail Connection Established.........");
}
catch(Exception e){
System.out.println("Connection Problem "+ e );
}
return myDetailCon;
}
-------------------------------------------
public void closeDetailConnection() throws SQLException
{
myDetailCon.close();
}
}


/////////////////////////////////////////////

This is customer file where i m using dbDetailConn

/////////////////////////////////////////
// Customer Setup.java //
/////////////////////////////////////////


import java.sql.*;
import javax.swing.*;
import java.util.Date;
import java.util.Calendar;
import java.awt.event.*;
import java.util.*;
import java.text.SimpleDateFormat;
import javax.swing.text.BadLocationException;

public class Customer_Setup extends javax.swing.JFrame {
DBConnection cnn = DBConnection.getInstance();
dbConnDetail cnnd = dbConnDetail.getInstance();


<B> HERE IS THE INSTANCE THAT I M CALLING IT IN EVERY FORM. </B>

String sql = "Select * from emp";
Statement st = cnnd.myDetailCon.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE );
rs = st.executeQuery(sql);
System.out.println(sql);


THE ABOVE CODING ARE ON DISPLAY RECORD BUTTON.
BUT RESULT SHOWS
java.lang.NullPointerException


PROBLEMS
========

1. AS FAR AS MASTERDB IS CONCERN EVERYTHING IS WORKING FINE. BUT WHEN I AM USING CUSTOER FILE THAT IS CALLED FROM DETAIL DATABASE CONNECTOIN. AT THE LOAD OF FORM IT IS CREATING DATABASE INSTANCE BUT WHEN I AM CLICKING "SHOW RESULT BUTTON" IT SHOWS

java.lang.NullPointerException



2. AT THE TIME OF OPENING OF CUSTOMER_FILE IT CREATE DATABASE INSTANCE BUT CONNECTION TO DATABASE IS NOT CLOSED AFTER CLICKING EXIT BUTTON.

cnnd.closeDetailConnection();

YOU CAN SEE THE FUNCTION IN dbConnDetail class

Error: java.lang.NullPointerException
Anand Karia
Ranch Hand

Joined: Sep 25, 2004
Posts: 156

OH MY GOD!!!

I GOT THE SOLUTION. I WAS MAKING MISTAKE AT dbDetailConn.

I was creating local connection variable in dbDetailConn. i.e.

Connection myDetailCon = null;

After deleting this line, my project start working properly.


ANAND KARIA
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29219
    
135

Anand,
Glad to see you have it working. Thanks for using full words. You may also want to consider not typing a sentence in all caps. It makes it harder to read and gives the impression that you are shouting.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple database & multiple connection problem.
 
Similar Threads
Multiple database multiple conneciton problem.
Passing Value from 1 class 2 another.
Single Application but Multiple Connection problem
Passing Value from 1 class 2 another
Multiple database multiple conneciton problem.