• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Multiple database & multiple connection problem.

 
Ranch Hand
Posts: 158
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the JDBC forum...
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Anand Karia
Ranch Hand
Posts: 158
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 158
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author & internet detective
Posts: 41995
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic