import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
public class AbstractDAO
{
/** The datasource used by this object. */
protected DataSource dataSource = null;
protected String jndiDataSourceName = null;
public static String DEFAULT_SCHEMA;
private static Logger logger = Logger.getLogger(AbstractDAO.class);
/**
* Constructor for the class.
* @param jndiDataSourceName the JNDI name of the datasource
* @throws AbstractDAO if the datasource cannot be located
*/
protected AbstractDAO(String jndiDataSourceName) throws DAOException
{
this.jndiDataSourceName = jndiDataSourceName;
try
{
dataSource = getDataSource(this.jndiDataSourceName);
}
catch(NamingException ne)
{
logger.error("AbstractDAO:: AbstractDAO() -Unable to create the Data Access Object " + ne);
throw new DAOException("AbstractDAO:: AbstractDAO() - Unable to create the Data Access Object",ne);
}
}
/**
* Looks up and returns a DataSource object from the Naming service.
*
* @param jndiName the name used to bind the DataSource object
* in the Naming service
* @return the DataSource object specified by the jndiName parameter
* @throws NamingException if the DataSource object cannot be located
* within the Naming service
*/
public DataSource getDataSource(String jndiName) throws NamingException
{
InitialContext context = new InitialContext();
this.dataSource = (DataSource) context.lookup(jndiName);
return dataSource;
}
/**
* Returns a Connection object from a specific DataSource.
* @param jndiDataSourceName the name of the DataSource
* @return Connection the request Connection object
* @throws Exception is an error is encountered
*/
protected Connection getConnection() throws SQLException
{
Connection connection = dataSource.getConnection();
if(DEFAULT_SCHEMA != null) {
connection.createStatement().execute("ALTER SESSION SET CURRENT_SCHEMA="+DEFAULT_SCHEMA);
}
return connection;
}
/**
* This method is used to check the string value is null or not
*/
protected boolean isBlank(String value)
{
if(value == null || value.equals(""))
{
return true;
}
else
{
return false;
}
}
/**
* Method : close
* Description : This Method closes object for a specific DataSource.
* @param resultSet
* @param statement
* @param connection
*/
protected void close(ResultSet resultSet, Statement statement, Connection connection) throws DAOException
{
try{
if(resultSet!= null)
{
try
{
resultSet.close();
}
catch(Exception ex)
{
logger.error("AbstractDAO:: close() - Unable to close result set object");
throw new DAOException("AbstractDAO:: close() - Unable to close result set object");
}
}
if(statement != null)
{
try
{
statement.close();
}
catch(Exception ex)
{
logger.error("AbstractDAO:: close() - Error While closing the statement "+ex);
throw new DAOException("AbstractDAO:: close() - Unable to close statement object");
}
}
if(connection != null)
{
try
{
connection.close();
}
catch(Exception ex)
{
logger.error("AbstractDAO:: close() - Error While closing the connection "+ex);
throw new DAOException("AbstractDAO:: close() - Unable to close connection object");
}
}
}
catch(Exception exception)
{
logger.error("AbstractDAO:: close() - Error While closing the connection "+exception);
throw new DAOException("AbstractDAO:: close() - Error While closing the connection");
}
}
/**
* Method : roll back
* Description : This method roll back the transaciton.
* @param connection
*/
protected void rollback(Connection connection) throws DAOException
{
if (connection != null)
try {
connection.rollback();
}catch(Exception ex)
{
logger.error("AbstractDAO:: rollback() - Error While roll back the transaciton "+ex);
throw new DAOException("AbstractDAO:: rollback() - Unable to roll back the transaciton");
}
}
/**
* Method : executeUpdate
* Description : this method used to update the DB.
* @param connection
* @param sql
* @throws DAOException
*/
protected void executeUpdate(Connection connection, String sql) throws DAOException
{
logger.info("AdminGuiAbstractDAO:: executeUpdate() - In executeUpdate method");
PreparedStatement statment = null;
try
{
statment = connection.prepareStatement(sql);
statment.executeUpdate();
}
catch(SQLException sqle)
{
logger.debug("AbstractDAO:: executeUpdate() - Unable to Update " + sqle);
throw new DAOException(sqle);
}
catch(Exception exception)
{
logger.debug("AbstractDAO:: executeUpdate() - Error in executeUpdate."+exception);
throw new DAOException("AbstractDAO:: executeUpdate() - "+exception);
}
finally
{
close(null, statment, null);
}
}
}