implementing DAO

Ads

 
 

Share on Google+Share on Google+

virgo25
implementing DAO
2 Answer(s)      8 years ago
Posted in : Struts
Ads
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

Ads









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
DAO Classes
DAO Classes  login page code for Dao classes
DAO Example
DAO Example  Dear Friends Could any one please give me any example of DAO application in struts? Thanks & Regards Rajesh
implementing security - Security
implementing security  wanna learn cyptography in java...how must i proceed
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
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
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 ????? please help
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 ????? please help
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 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
dao
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
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
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
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
Version of spring>spring-dao dependency
List of Version of spring>spring-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
Version of com.enterprisemath>em-dao dependency
List of Version of com.enterprisemath>em-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
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
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