implementing DAO

implementing DAO

View Answers

December 29, 2008 at 1:46 AM

Hi friend,


Java DAO Implementation
This tutorial shows, how to create the source code of Dao and DaoImpl class.
The Data Access Object (DAO) is an important component in business applications. 
Business applications almost always need access to data from relational or object databases and the Java platform offers many techniques for accessing this data. 
The Data Access Object design pattern provides a technique for separating object persistence and data access logic from any particular persistence mechanism or API.
The Java Database Connectivity (JDBC) API, provides the capability to execute SQL queries against a database and then fetch the results, one column at a time.
The Java DAO approach provides flexibility to change an application's persistence mechanism over time without the need to re-engineer application logic that interacts with the Data Access Object tier.
The Data Access Object design pattern also provides a simple, consistent API for data access that does not require knowledge of JDBC, EJB, Hibernate, or Spring interfaces.
Use a Data Access Object (DAO) to abstract and encapsulate all access to the data source. The DAO manages the connection with the data source to obtain and store data.

Source Code of Dao.java
package com.roseindia.portal.dao;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import java.util.List;

import com.roseindia.portal.model.*;

/**
 * @author Sandeep Kumar Suman
 */

@Transactional (propagation = Propagation.REQUIRED, readOnly = false)

public interface Dao{

    public List<Emp> findByName(String name);
    
  public void addEmp(Emp user);

  public void updateEmp(Emp chatEmp);

  public List<Emp> loginEmp(String username, String password);
  
  public void deleteEmp(Emp removeEmp);

}

---------------------------

December 29, 2008 at 1:47 AM

Source Code of DaoImpl.java
package com.roseindia.portal.dao.impl;

import com.roseindia.portal.dao.*;
import javax.persistence.EntityTransaction;


import java.util.*;
import java.text.*;
import java.util.ArrayList;
import java.util.Collection;
import java.sql.*;

import javax.persistence.Persistence;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.roseindia.portal.dao.BaseDao;
import com.roseindia.portal.model.*;

import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

/**
* @author Sandeep Kumar Suman
*/

@Transactional (propagation = Propagation.REQUIRED, readOnly = false)
public class DaoImpl implements Dao {


public List<Emp> findByName(String name){
Query query = this.getEntityManager().createQuery("select u FROM
Emp u where u.userName='" + name +"'");
return query.getResultList();
}

private EntityManager entityManager;
/**
* Set the JPA EntityManager to use.
*/
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}

/**
* Return the JPA EntityManager to use.
*/
public EntityManager getEntityManager() {
return entityManager;
}

public void addEmp(Emp user){
this.getEntityManager().persist(user);
}

public void updateEmp(Emp user){
this.getEntityManager().merge(user);
}

public List<Emp> loginEmp(String username, String password){
System.out.println("Login Emp in Emp Service DAO");
Query query = this.getEntityManager().createQuery("select u FROM
Emp u where u.userName='" + username +"' and u.password='" + password +"'");
return query.getResultList();
}

public void deleteUser(User removeUser){
System.out.println("Removing User: "+removeUser.getFirstName());
this.getEntityManager().remove(removeUser);
}
}
---------------------------------------------------

Visit for more information:

http://www.roseindia.net/struts/struts2/

Thanks









Related Tutorials/Questions & Answers:
implementing DAO - Struts
implementing DAO  Hi Java gurus I am pure beginner in java..., exam in 3 days, and just now i found out our lecturer post a demo on DAO... divided into DAO factory Impl,employeeDAOImpl,DAOFactory,EmployeeDAO. "I strongly
Implementing Interface
Implementing Interface  interface Mat { void read( ); void display( ); } Create a class Matrix by implementing interface Mat. Derive class MatrixOp from Matrix and provide functions to add and multiply two matrices. Also derive
Advertisements
Implementing Vectors.
Implementing Vectors.  Create a class Student which stores Name ,Rollno and Grandtotal of a student. Use class Vector to maintain an array of students in the descending order of the Grand_total. Provide the following functions 1
implementing security - Security
implementing security  wanna learn cyptography in java...how must i proceed
DAO Classes
DAO Classes  login page code for Dao classes
Implementing FTP in Java Code
Implementing FTP in Java Code  Hi, My job is to write a program in Java in my project. I have to implement FTP in my Java Code. Share me some of the code of Implementing FTP in Java Code. Thanks   Hi, Apache ftp
Implementing voice commands
Implementing voice commands  Am trying to write a java program to implement voice commands within a windows platform using preferably java (though any language will do). I would be glad for any tips and procedures that would help
code for implementing sale purchase
code for implementing sale purchase  i have two tables in database. in one table i have stock of sms and further details and other table is for customer .if customer purchases my sms then the value will be deduct from my table
DAO Example
DAO Example  Dear Friends Could any one please give me any example of DAO application in struts? Thanks & Regards Rajesh
DAO in Struts
DAO in Struts  Can Roseindia provide a simple tutorial for implementation of DAO with struts 1.2? I a link already exits plz do tell me. Thank you
DAO - JDBC
DAO  what is dao ? and how to use it?  Hi Friend, The DAO is a pattern that provides a technique for separating object persistence and data access logic from any particular persistence mechanism or API. Thanks
doubt on DAO's
doubt on DAO's  hai frnds.... can anyoneexplain about how... our own plugin????? and please help me. how to use dao s while integrating struts with hibernate..??? actually what is the purpose of dao's
doubt on DAO's
doubt on DAO's  hai frnds.... can anyoneexplain about how... our own plugin????? and please help me. how to use dao s while integrating struts with hibernate..??? actually what is the purpose of dao's
dao pack
dao pack  package com.tsi.dao; import java.sql.*; import com.tsi.constants.*; public class DaoPack { public static Connection conn = null; public static Connection createConnection() { try
dao pack
dao pack  package com.tsi.dao; import java.sql.*; import com.tsi.constants.*; public class DaoPack { public static Connection conn = null; public static Connection createConnection() { try
Implementing Digits restriction Using JQuery
Implementing Digits restriction Using JQuery  Hi Sir I have following field in my html form : <div>Age<input type="text" name="quantity" id="quantity" /></div> I want to implement two things : 1
dao
Exception handling in super and subclass while implementing inheritance,,?
Exception handling in super and subclass while implementing inheritance,,?  How to implement Superclass Exceptions with SubClass while implementing Inheritance concept in core java.? Please answer for this with sample code
ModuleNotFoundError: No module named 'dao'
ModuleNotFoundError: No module named 'dao'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'dao' How to remove the ModuleNotFoundError: No module named 'dao' error
ModuleNotFoundError: No module named 'dao'
ModuleNotFoundError: No module named 'dao'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'dao' How to remove the ModuleNotFoundError: No module named 'dao' error
CRUD DAO
CRUD DAO  how to create dao for create,read,update and delete?   /* *ConnectionManager * * *Version:1.0 * *Date:25-Nov-2011 * */ package com.student.dao; import java.sql.*; import org.apache.log4j.Logger
spring DAO module turorial
spring DAO module turorial  how to integrate springDAO and spring webMVC
JSP Project: implementing video chatting in JSP
JSP Project: implementing video chatting in JSP  Hi!!! I am developing a chatting application in JSP and Servlet. I want to implement video chatting facility on my website. How can I do this task. Please help at the earliest
Implementing ArrayList's functionalities into arrays - Java Beginners
Implementing ArrayList's functionalities into arrays  Hi guys, i'm trying to solve an exercise were is asked to write a program that manages the grades of a list of students (given by the exercise) for one single exam; The list
Version of spring>spring-dao dependency
List of Version of spring>spring-dao dependency
Version of springframework>spring-dao dependency
List of Version of springframework>spring-dao dependency
Version of co.jufeng>jufeng-dao dependency
List of Version of co.jufeng>jufeng-dao dependency
Version of com.aoindustries>ao-dao dependency
List of Version of com.aoindustries>ao-dao dependency
Version of com.butor>butor-dao dependency
List of Version of com.butor>butor-dao dependency
Version of com.enterprisemath>em-dao dependency
List of Version of com.enterprisemath>em-dao dependency
Version of com.ibatis>ibatis-dao dependency
List of Version of com.ibatis>ibatis-dao dependency
Version of com.smartitengineering>smart-dao dependency
List of Version of com.smartitengineering>smart-dao dependency
What is the DAO Layer in any application?
What is the DAO Layer in any application?  Hi, What is the use of the DAO layer in any application and how to develop it. Explain with the example code. Thanks   The DAO layer is also known as data access layer
ModuleNotFoundError: No module named 'dao-deploy'
ModuleNotFoundError: No module named 'dao-deploy'  Hi, My Python... 'dao-deploy' How to remove the ModuleNotFoundError: No module named 'dao... have to install padas library. You can install dao-deploy python
ModuleNotFoundError: No module named 'SQLAlchemy-Dao'
ModuleNotFoundError: No module named 'SQLAlchemy-Dao'  Hi, My... named 'SQLAlchemy-Dao' How to remove the ModuleNotFoundError: No module named 'SQLAlchemy-Dao' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'worm-dao'
ModuleNotFoundError: No module named 'worm-dao'  Hi, My Python... 'worm-dao' How to remove the ModuleNotFoundError: No module named 'worm-dao... to install padas library. You can install worm-dao python with following
ModuleNotFoundError: No module named 'worm-dao'
ModuleNotFoundError: No module named 'worm-dao'  Hi, My Python... 'worm-dao' How to remove the ModuleNotFoundError: No module named 'worm-dao... to install padas library. You can install worm-dao python with following
ModuleNotFoundError: No module named 'dao-deploy'
ModuleNotFoundError: No module named 'dao-deploy'  Hi, My Python... 'dao-deploy' How to remove the ModuleNotFoundError: No module named 'dao... have to install padas library. You can install dao-deploy python
implementing an algorithm using multi threads - Java Beginners
implementing an algorithm using multi threads  Hi i need to implement an algorith in multi threads.Algorithm has data dependency so i need to pass data from one thread to another thread. I am posting my algorithm which needs
DAO DTO design pattern
DAO DTO design pattern  Hi,using dao and dto i want to perform insert,update and delete operation.and the data should navigate from 1 frame 2 another.that page should b smthng like this: <%@ page language="java" import
DAO DTO design pattern
DAO DTO design pattern  Hi,using dao and dto i want to perform insert,update and delete operation.and the data should navigate from 1 frame 2 another.that page should b smthng like this: <%@ page language="java" import
DAO DTO design pattern
DAO DTO design pattern  Hi,using dao and dto i want to perform insert,update and delete operation.and the data should navigate from 1 frame 2 another.that page should b smthng like this: <%@ page language="java" import
DAO DTO design pattern
DAO DTO design pattern  Hi,using dao and dto i want to perform insert,update and delete operation.and the data should navigate from 1 frame 2 another.that page should b smthng like this: <%@ page language="java" import
how to create dao
how to create dao   code1: package com.dao; import java.sql.*; import com.beans.*; public class DbAccess { public void createStudent(SampleBean sampleBean) throws SQLException { Connection con=null; try
The easy slider plug-in implementing with numeric boxes
The easy slider plug-in implementing with numeric boxes In this tutorial, we will discuss about implementing 'easySlider' plug in of jQuery with numeric boxes.In this example, easySlider plug in used to develop automatic
Version of com.butor>butor-auth-dao dependency
List of Version of com.butor>butor-auth-dao dependency
Version of com.butor>butor-attrset-dao dependency
List of Version of com.butor>butor-attrset-dao dependency
Version of com.butor>butor-portal-dao dependency
List of Version of com.butor>butor-portal-dao dependency
Version of com.erudika>para-dao-mongodb dependency
List of Version of com.erudika>para-dao-mongodb dependency
Version of com.gammapeit>generic-injected-dao dependency
List of Version of com.gammapeit>generic-injected-dao dependency

Ads