• 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
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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
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
 
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic