My Util class..
package com.simpragma.assignment.multithreading.messagehandler.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.log4j.Logger;
import com.simpragma.assignment.multithreading.messagehandler.constants.MessageHandlerConstants;
import com.simpragma.assignment.multithreading.messagehandler.customexception.MessageHandlerException;
/**
* Name ConnectionUtil.java
*
* @author Amruta <br/>
* <b>Purpose </b> This class is designed based on Single ton design
* pattern.
*
******************************************************************************
* Audit Trails.</br> <br/>
* who when version comment</br>
* Amruta 20th December 2012 1.0 Base version created
******************************************************************************
*/
public class ConnectionUtil {
// A static logger variable so that it references the Logger instance named
// "EventManagerService.class"
private static Logger logger = Logger.getLogger(ConnectionUtil.class);
//Instance variables
static private ConnectionUtil connectionUtil=null;
private static Connection connection = null;
private static Properties properties=null;
//getter method for connection
public static Connection getConnection() {
return connection;
}
private ConnectionUtil() throws MessageHandlerException {
logger.debug("Entering constructor");
properties = new Properties();
properties.setProperty("driverClass", "com.mysql.jdbc.Driver");
properties.setProperty("uri", "jdbc:mysql://localhost:3306/SIMPRAGMA");
properties.setProperty("username", "root");
properties.setProperty("password", "123");
try {
properties.store(new FileOutputStream("/home/harshal/Amruta/" +
"Simpragma/Multithreading/MessageHandler/src/configfiles" +
"/config.properties"),null);
properties.load(new FileInputStream("/home/harshal/Amruta/Simpragma/"
+ "Multithreading/MessageHandler/src/configfiles/" +
"config.properties"));
// Loading driver class
Class.forName(properties.getProperty("driverClass"));
// Establishing connection
connection = DriverManager.getConnection(
properties.getProperty("uri"),
properties.getProperty("username"),
properties.getProperty("password"));
} catch (FileNotFoundException e) {
logger.error("Error Description",e);
MessageHandlerException exception = new MessageHandlerException();
exception.setErrorKey(MessageHandlerConstants.FILE_NOT_FOUND_KEY);
throw exception;
} catch (IOException e) {
logger.error("Error Description",e);
MessageHandlerException exception = new MessageHandlerException();
exception.setErrorKey(MessageHandlerConstants.IO_FAILURE_KEY);
throw exception;
} catch (ClassNotFoundException e) {
logger.error("Error Description",e);
MessageHandlerException exception = new MessageHandlerException();
exception.setErrorKey(MessageHandlerConstants.CLASS_NOT_FOUND_KEY);
throw exception;
} catch (SQLException e) {
logger.error("Error Description",e);
MessageHandlerException exception = new MessageHandlerException();
exception.setErrorKey(MessageHandlerConstants.SQL_ERROR_KEY);
throw exception;
}
logger.debug("Exiting constructor");
}
/**
* Instantiates ConnectionUtil class
*
* @return reference of ConnectionUtil class
* @throws MessageHandlerException
*/
public static ConnectionUtil getConnectionUtil() throws MessageHandlerException
{
logger.debug("Entering getConnectionUtil()");
if(connectionUtil==null)
{
connectionUtil=new ConnectionUtil();
}
logger.debug("Exiting getConnectionUtil()");
return connectionUtil;
}
/**
* Used to close object level resources.
*//*
@Override
protected void finalize() throws Throwable {
if(connection!=null)
{
connection.close();
}
}*/
}