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