Friday, April 14, 2017

DAO Design Pattern

The Data Access Object(DAO) is one of the J2EE pattern. The DAO pattern hides the implementation details of data source from it's clients,so introducing loose coupling between your core business logic and your persistence mechanism. The main purpose of this Design pattern is to implement persistence layer of the Java application.Here persistence store means all database software like oracle,MySQL.

Let us discuss now without DAO in real projects:

In MVC architecture projects or other projects if persistence logic is mixed up with other logic of the application especially (Business logic)then persistence logic  will not become flexible to modify. That means the modification done in persistence logic may disturb other logics of the application.So, DAO pattern main purpose is to provide a clean design-oriented mechanism for accessing all types of data from your application.

Solution: 
The solution is working with DAO design patter. The java class that separates persistence logic from other logic of the application to make persistence logic as flexible logic to modify is called as DAO design pattern.

DAO class contains:

  • It contains the logic to establish the connection
  • It contains the logic to release the connection
  • It contains the logic to perform persistence operation like insert,update,delete and select
All the above logics of DAO can be developed by using any persistence technology like JDBC,Hibernate,entity Bean components etc...

All the Business components of project can use either single DAO or they can use separate DAO class for each Business components .It is always recommended to write separate DAO class for each Business component.

Now we will understand DAO design pattern with Employee details example:

Create Employee table in MySQL:

CREATE TABLE Employee ( 
EmpId int(1)  PRIMARY KEY NOT NULL, 
Name varchar2(20) NOT NULL, 
DeptName varchar(25) NOT NULL, 
Address text 

 );

Now we will write a java program to make DBConnection:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnector {
String driverClassName = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/Employee";
String dbUser = "root";
String dbPwd = "root";
private static DBConnector DBConnector = null;
private DBConnector() {
try {
Class.forName(driverClassName);

catch (ClassNotFoundException e) 
{
e.printStackTrace();
}
}
public Connection getConnection() throws SQLException {
Connection conn = null;
conn = DriverManager.getConnection(connectionUrl, dbUser, dbPwd);
return conn;
}
public static DBConnector getInstance() {
if (DBConnector == null)
{
DBConnector = new DBConnector();
}
return DBConnector;
}
}

Now we will write code for class EmployeeBean:


import java.io.Serializable;
public class EmployeeBean implements Serializable
{
int EmpId;
String name;
String DeptName;
String address;
public EmployeeBean() 
{
}
public StudentBean(int EID, String name, String DeptName, String address) 
{
this.EmpId = EID;
this.name = name;
this.DeptName = DeptName;
this.address = address;
}
public int getEmpId() 
{
return EmpId;
}
public void setEmpId(int EmpId) 
{
this.EmpId = EmpId;
}
public String getName() 
{
return name;
}
public void setName(String name) 
{
this.name = name;
}
public String getDeptName() 
{
return DeptName;
}
public void setDeptName(String DeptName) 
{
this.DeptName = DeptName;
}
public String getAddress() 
{
return address;
}
public void setAddress(String address) 
{
this.address = address;
}
}

Now we will write EmployeeDAO class:

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class EmployeeDAO 
{
Connection connection = null;
PreparedStatement ptmt = null;
ResultSet resultSet = null;
public EmployeeDAO() 
{
}
private Connection getConnection() throws SQLException 
{
Connection conn;
conn = DBConnector.getInstance().getConnection();
return conn;
}
public void add(EmployeeBean EmployeeBean) 
{
try {
String queryString = "INSERT INTO Employee(EmpId, Name, DeptName, Address) VALUES(?,?,?,?)";
connection = getConnection();
ptmt = connection.prepareStatement(queryString);
ptmt.setInt(1, EmployeeBean.getEmpId());
ptmt.setString(2, EmployeeBean.getName());
ptmt.setString(3, EmployeeBean.getDeptName());
ptmt.setString(4, EmployeeBean.getAddress());
ptmt.executeUpdate();
System.out.println("Data Added Successfully");
}
catch (SQLException e) 
{
e.printStackTrace();
finally 
{
try {
if (ptmt != null)
ptmt.close();
if (connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void update(EmployeeBean EmployeeBean) {
try {
String queryString = "UPDATE Employee SET Name=? WHERE EmpId=?";
connection = getConnection();
ptmt = connection.prepareStatement(queryString);
ptmt.setString(1, EmployeeBean.getName());
ptmt.setInt(2, EmployeeBean.getEmpId());
ptmt.executeUpdate();
System.out.println("Table Updated Successfully");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (ptmt != null)
ptmt.close();
if (connection != null)
connection.close();
}
catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void delete(int Empid) {
try {
String queryString = "DELETE FROM Employee WHERE EmpId=?";
connection = getConnection();
ptmt = connection.prepareStatement(queryString);
ptmt.setInt(1, EmpId);
ptmt.executeUpdate();
System.out.println("Data deleted Successfully");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (ptmt != null)
ptmt.close();
if (connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void findAll() {
try {
String queryString = "SELECT * FROM Employee";
connection = getConnection();
ptmt = connection.prepareStatement(queryString);
resultSet = ptmt.executeQuery();
while (resultSet.next()) {
System.out.println("EmpId " + resultSet.getInt("EmpId")
+ ", Name " + resultSet.getString("Name") + ", DeptName "
+ resultSet.getString("DeptName") + ", Address "
+ resultSet.getString("Address"));
}
catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null)
resultSet.close();
if (ptmt != null)
ptmt.close();
if (connection != null)
connection.close();
catch (SQLException e) 
{
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

Main class:

public class MainClass {
public static void main(String[] args) {
EmployeeDAO employee = new EmployeeDAO();
EmployeeBean emp = new EmployeeBean();
emp.setName("lucky");
emp.setEmpId(6);
emp.setDeptName("Techinical");
emp.setAddress("hyderabad");
EmployeeBean eb = new EmployeeBean();
eb.setName("mahesh");
eb.setEmpId(5);
// Adding Data
employee.add(emp);
// Deleting Data
employee.delete(4);
// Updating Data
employee.update(eb);
// Displaying Data
employee.findAll();
}
}

ALSO READ:

JDBC INTERVIEW QUESTIONS
SPRING INTERVIEW QUESTIONS
STRUTS INTERVIEW QUESTIONS
TOP 20 HR INTERVIEW QUESTIONS
HIBERNATE INTERVIEW QUESTIONS






No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...