Hi friends can you give me sample code for Entire j2ee web
application with struts and hibernate.I mean code for Dao,
DaoImpl, HibernateDao ,HibernateDaoImpl, Dto , service ,
ServiceImpl , i.e.



Hi friends can you give me sample code for Entire j2ee web application with struts and hibernate.I..

Answer / nainasona

{{{{{{{{{{{{{{{{{{{{{{{{JDBC DAO}}}}}}}}}}}}}}}}}}}}}}}}}}}}
public class JDBCDAO
{
static Logger logger =
Logger.getLogger(JDBCDAO.class);
/**
* Connection Object
*/
private Connection connection = null;

/**
* PreparedStatement Object
*/
private PreparedStatement preparedStatement = null;

/**
* Statement Object
*/
private Statement statement = null;
/**
* Default Constructor for JDBCDAO
* @param none
* @exception ApplicationException
*/

JDBCDAO() throws ApplicationException
{
try
{
//Get the database connection
connection =
ServiceLocator.getConnection(false);
if(null != connection)
{
//Set auto commmit to false

connection.setAutoCommit(false);
}
}
catch(SQLException se)
{
/*
* Convert the SQLException to
* ApplicationException and
throw ApplicationException
*/
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,se);
}
}

/**
* This method executes the given SQL statement,
which may be
* an INSERT, UPDATE, or DELETE statement or an SQL
statement
* that returns nothing, such as an SQL DDL
statement.
* @param strQuery - SQL INSERT, UPDATE or DELETE
statement
* or an SQL
statement

that returns nothing
* @exception ApplicationException
* @return intExe - Either the row count for INSERT,
UPDATE
* or DELETE
statements,

or 0 for SQL
* statements
that return

nothing
*/
public int executeUpdate(String strQuery) throws
ApplicationException
{
int intExe = 0;
Statement stExeUpdate = null;
try
{
//Check if connection is
not null
if(null != connection)
{
stExeUpdate =
connection.createStatement();

/*
* Check if
Statement is not null and
* Query String is
not null or blank
*/
if((null !=
stExeUpdate) && (null != strQuery

&&

!("".equals(strQuery))))
{
//Excecute
the query
intExe =
stExeUpdate.executeUpdate(
strQuery);
}
}
}
catch(SQLException se)
{
/*
* Convert the SQLException to
* ApplicationException and
throw ApplicationException
*/
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,se);
}
finally
{
stExeUpdate = null;
}
return intExe;
}

/**
* This method Executes the given SQL statement,
* which returns a single ResultSet object.
* @param strQuery - SQL statement to be sent to the
database
* @exception ApplicationException
* @return rsExeQuery - ResultSet object that
contains the
* data

produced by the given query
*/
public ResultSet executeQuery(String strQuery)
throws ApplicationException
{

ResultSet rsExeQuery = null;
Statement stExeQuery = null;
try
{
//Check if connection is not null
if(null != connection)
{
stExeQuery =
connection.createStatement();

/*
* Check if
Statement is not null and
* Query String is
not null or blank
*/
if((null != stExeQuery) &&
(null != strQuery &&

!("".equals(strQuery))))
{
//Execute query
rsExeQuery =
stExeQuery.executeQuery(

strQuery);
}
}
}
catch(SQLException se)
{
logger.error("Error : "+se);
/*
* Convert the SQLException to
* ApplicationException and
throw ApplicationException
*/
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,se);
}
finally
{
stExeQuery = null;
}
return rsExeQuery;
}

/**
* This method creates a PreparedStatement object
for sending
* parameterized SQL statements to the database.
* @param prepareQuery - SQL statement that may
contain one
* or
more '?'

IN parameter placeholders
* @exception ApplicationException
* @return none
*/
public void prepareStatment(String prepareQuery)
throws ApplicationException
{
try
{
/*
* Check if Connection is not null
and
* Query String is not null or blank
*/
if((null != connection) && (null !=
prepareQuery &&
!("".equals(prepareQuery))))
{
//Prepare Statement

preparedStatement=connection.prepareStatement(

prepareQuery);
}
}
catch(SQLException se)
{
/*
* Convert the SQLException to
* ApplicationException and
throw ApplicationException
*/
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,se);
}
}

/**
* This method adds a set of parameters to this
PreparedStatement
* object's batch of commands.
* @param attribTypes - List of attribute types
* @param attribValues - List of attribute values
* @exception ApplicationException
* @return none
*/
public void addBatch(ArrayList attribTypes,
ArrayList attribValues) throws
ApplicationException
{
int intCount = 0;
int intSQLType = 0;
int intSize = 0;
String strSQLType = "";
try
{
if(null != attribTypes &&
null !=

attribValues)
{
intSize =
attribTypes.size();
for(intCount
= 0;

intCount < intSize;


intCount++)
{


strSQLType =(String)attribTypes.get(


intCount);


if(null

!= strSQLType &&


!("".equals(strSQLType)))
{


intSQLType = Integer.parseInt(


strSQLType);





if(null != preparedStatement)


{





/*


* Sets the value of the


* designated parameter with


* the given object


*/


preparedStatement.setObject(


intCount,


attribValues.get(intCount),


intSQLType);


}
}
}


preparedStatement.addBatch();
}
}
catch(SQLException se)
{
/*
* Convert the SQLException to
* ApplicationException and
throw ApplicationException
*/
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,se);
}
finally
{
intCount = 0;
intSQLType = 0;
intSize = 0;
strSQLType = "";
}
}

/**
* This method has to be used when working on
CreateStatements
* object, where there is a need to perform a batch
insert.
* @param String[] strQuery It will contained the
array of
* insert
queries.
* @exception ApplicationException
* @return void
*/
public void insertBatchData(String [] strQuery)
throws
ApplicationException
{
int intSize =0;
try
{
if(null!=strQuery)
{
statement =
connection.createStatement();
intSize = strQuery.length;
for(int i=0;i<intSize;i++)
{
statement.addBatch(strQuery[i]);
}
statement.executeBatch();
}
}
catch(SQLException se)
{
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,se);
}
finally
{
try
{
if(null!=statement)
{
statement.close();
}
}
catch(SQLException se)
{
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,se);
}
}
}

/**
* This method has to be used when working on
PreparedStatement
* object, where there is a need to perform a batch
insert,
* update or delete.
* @param none
* @exception ApplicationException
* @return intExe - The integer value returned by
the
*
executeUpdate method

of PreparedStatement
*/
public int executeUpdate() throws
ApplicationException
{
int intExe = 0;
try
{
if(null != preparedStatement)
{
/*
* Execute the SQL statement
in this
* PreparedStatement object
*/
intExe =
preparedStatement.executeUpdate();
}
}
catch(SQLException se)
{
/*
* Convert the SQLException to
* ApplicationException and
throw ApplicationException
*/
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,se);
}
return intExe;
}

/**
* This method will make all changes made since the
previous
* commit/rollback permanent and releases any
database locks
* currently held by this Connection object
* @param none
* @exception ApplicationException
* @return none
*/
void commit() throws ApplicationException
{
try
{
//Check if connection is not
null
if(null != connection)
{
//Commit data to
database
connection.commit();
}
}
catch(SQLException se)
{
/*
* Convert the SQLException to
* ApplicationException and
throw ApplicationException
*/
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,se);
}
}

/**
* This method will undo all changes made in the
current
* transaction and releases any database locks
currently
* held by this Connection object.
* @param none
* @exception ApplicationException
* @return none
*/
void rollback() throws ApplicationException
{
try
{
//Check if connection is not null
if(null != connection)
{
//Rollback the
transaction
connection.rollback();
}
}
catch(SQLException se)
{
/*
* Convert the SQLException to
* ApplicationException and
throw ApplicationException
*/
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,se);
}
}

/**
* This method is used to release this Connection
object's
* database and JDBC resources immediately instead
of waiting
* for them to be automatically released.
* @param none
* @exception ApplicationException
* @return none
*/
void close() throws ApplicationException
{
try
{
//Check if prepared
statement is not null
if(null != preparedStatement)
{

preparedStatement.close();
}

//Check if connection is not null or
already closed
if((null != connection) &&
(!(connection.isClosed())))
{
//Close the
connection
connection.close();
}
}
catch(SQLException se)
{
/*
* Convert the SQLException to
* ApplicationException and
throw ApplicationException
*/
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,se);
}
finally
{
preparedStatement = null;
connection = null;
}
}

/**
* This method has to be used when working on
CreateStatements
* object, where there is a need to perform a batch
insert.
* @param List lstQuery contains the list of
* insert
queries.
* @exception ApplicationException
* @return void
*/
public void insertBatchData(List lstQuery) throws
ApplicationException
{
int intSize =0;
try
{

if(null!=lstQuery)
{
statement =
connection.createStatement();
intSize = lstQuery.size();
for(int i=0;i<intSize;i++)
{

statement.addBatch((String)lstQuery.get(i));
}
statement.executeBatch();
}
}
catch(SQLException se)
{
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,se);
}
finally
{
try
{
if(null!=statement)
{
statement.close();
}
}
catch(SQLException se)
{
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,se);
}
}
}

/**
* @return Returns the preparedStatement.
*/
public PreparedStatement
getPreparedStatement()
{
return preparedStatement;
}

/**
* This method executes the given SQL statement,
which may be
* an INSERT, UPDATE, or DELETE statement or an SQL
statement
* that returns nothing, such as an SQL DDL
statement.
* @param strQuery - SQL INSERT, UPDATE or DELETE
statement
* or an SQL
statement

that returns nothing
* @exception ApplicationException
* @return intExe - Either the row count for INSERT,
UPDATE
* or DELETE
statements,

or 0 for SQL
* statements
that return

nothing
*/
public int delete(String strQuery) throws
ApplicationException
{
int intExe = 0;
StringBuffer sbQuery = null;
Statement stExeUpdate = null;

try
{
//Check if connection is
not null
if(null != connection)
{
stExeUpdate =
connection.createStatement();

/*
* Check if
Statement is not null and
* Query String is
not null or blank
*/
if((null !=
stExeUpdate) && (null != strQuery

&&

!("".equals(strQuery))))
{
sbQuery =
new StringBuffer();


sbQuery.append("insert into ");

sbQuery.append(FirstConstants

.TABLE_APPL_USER_TEMP);

sbQuery.append(" values");

sbQuery.append(" ('PURGE', 1)");

// Excecute
insert
intExe =
stExeUpdate.executeUpdate(

sbQuery.toString());

//Excecute
the query
intExe =
stExeUpdate.executeUpdate(
strQuery);
}
}
}
catch(SQLException se)
{
/*
* Convert the SQLException to
* ApplicationException and
throw ApplicationException
*/
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,se);
}
finally
{
sbQuery = null;
stExeUpdate = null;
}
return intExe;
}
}
{{{{{{{{{{{{{{{{{{{{{{{{JDBC DAO}}}}}}}}}}}}}}}}}}}}}}}}}}}}

{{{{{{{{{{{{{{{{{{{{{{{{HIBERNATE DAO}}}}}}}}}}}}}}}}}}}}}}}
public class HibernateDAO
{
//Create Logger for logging debug and exception
statements
static Logger logger =
Logger.getLogger(HibernateDAO.class);

/**
* Hibernate Session object
*/
private Session session = null;

/**
* Transaction object
*/
private Transaction transaction = null;

/**
* SessionFactory object
*/
private static SessionFactory sessionFactory = null;

/**
* This method will return the <Session> object
maintained in the
* instance scope.
* @param none
* @exception none
* @return session - the <Session> object maintained
in the
* instance
scope.
*/
public Session getSession()
{
return session;
}

/**
* Default Constructor for HibernateDAO
* @param none
* @exception ApplicationException
*/
HibernateDAO() throws ApplicationException
{
try
{
//Getting Connection
Connection connection =
null;
try
{
//Get the database
connection
connection =
ServiceLocator.getConnection(false);
if(null !=
connection)
{
//Set auto
commmit to false

connection.setAutoCommit(false);
}
}
catch(SQLException se)
{
/*
* Convert the
SQLException to
*
ApplicationException and throw ApplicationException
*/
throw new
ApplicationException(

ExceptionConstants.DATABASE_ERROR,se);
}

/*
* Checking if SessionFactory is
null, if null,
* obtaining a new instance of
SessionFactory
*/
if(null == sessionFactory)
{
sessionFactory = new

Configuration().configure().buildSessionFactory();
}
session =
sessionFactory.openSession(connection);

/*
* Checking if hibernate Session
is not null,
* if not null, then begin a new
transaction
*/
if(null != session)
{
transaction =
session.beginTransaction();
}
}
catch(HibernateException he)
{
/*
* Convert the
HibernateException to
* ApplicationException and
throw ApplicationException
*/
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,he);
}
}

/**
* This method will either save() or update() the
given instance,
* depending upon the value of its identifier
property
* @param databean - a transient or detached
instance containing
* new or
updated state
* @exception ApplicationException
* @return none
*/
public void saveOrUpdate(final DataBean databean)
throws ApplicationException
{
try
{
if(null != session && null !=
databean)
{
//Save data to session

session.saveOrUpdate(databean);
//session.flush();
}
}
catch(HibernateException he)
{
/*
* Convert the
HibernateException to
* ApplicationException and
throw ApplicationException
*/
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,he);
}
}

/**
* This method will delete the serliazable object
* @param databean - a transient or detached
instance containing
* new or
updated state
* @exception ApplicationException
* @return none
*/
public void delete(final DataBean databean,
UserPrincipal userPrincipal)
throws ApplicationException
{
String strUserID = null;
UserIDBean userIDBean = null;
try
{
if(null != session
&& null != databean)
{
strUserID =
AppUtil.getUserName(userPrincipal);
if(null !=
strUserID)
{

userIDBean = new UserIDBean();

userIDBean.setUserName(strUserID);

this.saveOrUpdate(userIDBean);
}

//Delete
record from session

session.delete(databean);
}

}
catch(HibernateException he)
{
/*
* Convert
the HibernateException to
*
ApplicationException and throw ApplicationException
*/
throw new
ApplicationException(

ExceptionConstants.DATABASE_ERROR,he);
}
}

/**
* Reads the persistent state associated with the
given
* identifier into the given transient instance.
* @param databean - an "empty" instance of the
persistent class
* @param intID - a valid identifier of an existing
persistent
* instance of the
class
* @exception ApplicationException
* @return none
*/
public void load(final DataBean databean, final int
intID)
throws ApplicationException
{
Integer integerID = null;
try
{
if(0 != intID)
{
integerID = new
Integer(intID);
if(null != session)
{
//load data
in Session

session.load(databean,integerID);
}
}
}
catch(HibernateException he)
{
/*
* Convert the
HibernateException to
* ApplicationException and
throw ApplicationException
*/
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,he);
}
finally
{
integerID = null;
}
}

/**
* Runs the HQL Query framed by the user and returs
a collection of
* the respective data bean as a List.
* @param strHQLQuery - a valid HQL Query String
* @exception ApplicationException
* @return none
*/
public List load(String strHQLQuery) throws
ApplicationException
{
List lst = null;
try
{
if(null != session)
{
//load data in Session
Query query =
session.createQuery(strHQLQuery);
lst = query.list();
}
}
catch(HibernateException he)
{
/*
* Convert the
HibernateException to
* ApplicationException and
throw ApplicationException
*/
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,he);
}
return lst;
}

/**
* This method will commit the underlying
transaction if and
* only if the transaction was initiated by this
object.
* @param none
* @exception ApplicationException
* @return none
*/
void commit() throws ApplicationException
{
try
{
if(null != transaction)
{
//Commit transaction to
database
transaction.commit();
}
}
catch(HibernateException he)
{
logger.debug("HibernateException
Message"+he.getMessage());
he.printStackTrace();
/*
* Convert the
HibernateException to
* ApplicationException and
throw ApplicationException
*/
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,he);
}
}

/**
* This method will force the underlying transaction
to roll back.
* @param none
* @exception ApplicationException
* @return none
*/
void rollback() throws ApplicationException
{
try
{
if(null != transaction)
{
//Rollback data from session
transaction.rollback();
}
}
catch(HibernateException he)
{
/*
* Convert the
HibernateException to
* ApplicationException and
throw ApplicationException
*/
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,he);
}
}

/**
* This method will end the Session by disconnecting
from the
* JDBC connection and cleaning up.
* @param none
* @exception ApplicationException
* @return none
*/
void close() throws ApplicationException
{
try
{
if(null != session)
{
//Release resources from
Session
session.flush();
session.close();
}
}
catch(HibernateException he)
{
/*
* Convert the
HibernateException to
* ApplicationException and
throw ApplicationException
*/
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,he);
}
finally
{
transaction = null;
session = null;
}
}

/**
* This method will persist the transient state of
the object
* Perform a persist before saveOrUpdate if the
parent child
* heirarchy is more than one level
* @param databean - a transient or detached
instance containing
* new or
updated state
* @exception ApplicationException
* @return none
*/
public void persist(final DataBean databean)
throws ApplicationException
{
try
{
if(null != session && null !=
databean)
{
//Save data to session
session.persist(databean);

}
}
catch(HibernateException he)
{
/*
* Convert the
HibernateException to
* ApplicationException and
throw ApplicationException
*/
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,he);
}
}

/**
* This method evict the databean from the session
* @param databean - an "empty" instance of the
persistent class
* @exception ApplicationException
* @return none
*/
public void evict(final DataBean databean)
throws ApplicationException
{
try
{
if(null != session)
{

session.evict(databean);
}
}
catch(HibernateException he)
{
/*
* Convert the
HibernateException to
* ApplicationException and
throw ApplicationException
*/
throw new ApplicationException(

ExceptionConstants.DATABASE_ERROR,he);
}
}
}
{{{{{{{{{{{{{{{{{{{{{{{{HIBERNATE DAO}}}}}}}}}}}}}}}}}}}}}}}

Is This Answer Correct ?    0 Yes 2 No

Post New Answer

More J2EE Interview Questions

Explain j2ee architecture.

0 Answers  


where exactly the servlets and jsp's are stored?

6 Answers   CG-VAK, JHJ, TCS,


what is defect. Is it runtime exception or comiletime exception

3 Answers   L&T,


What is javafx java?

0 Answers  


What is iso 3166?

0 Answers  






What is the J2EE module?

0 Answers  


What do you mean by j2ee module?

0 Answers  


What is java transaction service (jts)?

0 Answers  


What is create method?

0 Answers  


What is a plugin in java?

0 Answers  


What are the four components of j2ee application?

0 Answers  


What is fatal error?

0 Answers  


Categories