DAO_Design_Pattern_-Notes_lyst1825
DAO_Design_Pattern_-Notes_lyst1825
In every JDBC program, we have written till now, there are two components namely-
● Database Details
● Database Operations
Database Operations is nothing but the CRUD(Create, Read, Update, Delete) operations.
And we have written code in such a way that both Database details and database operations
are present in the single class. But whenever we write such code, few problems may arise
What are the problems?
● Maintainability
● Flexibility
So developers came up with a solution,i.e., Single Responsibility Principle and according to
this principle, a single class should have a Single Responsibility.
If a class has multiple responsibilities, then Maintainability becomes difficult because one
change in the code might affect the entire program and the code which we have written is not
flexible in nature.
Assume that code which we have written, has to connect to a database and we are connecting
to MySQL database, but what if we had another database that is managed by another
RDBMS(for eg, Oracle DBMS) and oracle driver has a different driver and the Database
Details code will not work for oracle since it is written specifically for MySQL. So our
code is not flexible enough.
How do we bring the changes so that both the problems i.e., Maintainability and
Flexibility will be resolved?
Developers before us have come up with Tried and Tested Solutions to resolve the
issue.
These Tried and Tested Solutions are technically called as Design Pattern and there
are many Design Pattern such as-
● Data Access Object Design Pattern
● Model View Controller Design Pattern
● Singleton Design Pattern
● Factory Design Pattern
And we will be discussing the Data Access Object [DAO] Design Pattern in this session.
There are few steps DAO Design Pattern, which if we follow then the problems will be
resolved
Steps to implement DAO Design Pattern:
1. Create a Data Transfer Object class[DTO class]
2. Create a DAO interface and implement the interface
3. Create a ConnectorFactory class
Previously we had discussed the problems we would face as a programmer using JDBC if we
do not use the concept of DAO(Data Access Object) Design Pattern.
Now let us see how we can implement DAO in our program.
Assume we have an employee table in our database and we have to perform CRUD(Create,
Read, Update, Delete) operations on that table and we do that by separating the code into
logical parts as shown below-
DTO class is a POJO [Plain Old Java Object] class, which contains instance variables and
getters and setters and the responsibility of this class is to create objects of the Entities present
in our table.
DAO class is a class through which we can perform operations on the table like selecting all the
employee details or fetching one employee information, etc.,
So we will create a interface EmployeeDAO which contains abstract methods like getEmp(),
insertEmployee(id, name, designation, salary), updateEmployee(Employee) and
deleteEmployee(id), etc., and this interface will be implemented by another class called as
EmployeeDAOImpl and this class implements all the methods of EmployeeDAO interface and
gives its implementation by overriding it.
Connector class is used to get a connection to our database. So the connector class has to
establish the connection and return the reference con so that we can perform CRUD operations
on the table.
The Application Layer wanted the details of the employee whose id is 1 and pass on the
information to EmployeeDAOImpl class and EmployeeDAOImpl class requested
ConnectorFactory class to give the connection to the database and using this connection,
EmployeeDAOImpl selected the record of that employee and using the Employee class created
an object and populated the records into the instance variables and Employee class transferred
the object back to the Application Layer
Let us now implement this in the code-
First, we have to create 3 packages-
● com.tap.dto
● com.tap.dao
● com.tap.connector
package com.tap.dto;
package com.tap.dao;
import java.util.List;
import com.tap.dto.Employee;
public interface EmployeeDAO {
List getEmployees();
Employee getEmployee(int id);
boolean insertEmployee(int id, String name, String designation, int
salary);
boolean updateEmployee(Employee e);
boolean deleteEmployee(int id);
EmployeeDAOImpl class
package com.tap.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.tap.connector.ConnectorFactory;
import com.tap.dto.Employee;
try {
Connection con = ConnectorFactory.requestConnection();
String query = "select * from emp";
Statement stmt = con.createStatement();
ResultSet res = stmt.executeQuery(query);
emplist = new ArrayList<Employee>();
while(res.next()==true)
{
int id = res.getInt(1);
String name = res.getString(2);
String designation = res.getString(3);
int salary= res.getInt(4);
Employee e = new
Employee(id,name,designation,salary);
emplist.add(e);
} catch (Exception e) {
e.printStackTrace();
}
return emplist;
}
return null;
}
return false;
}
return false;
}
return false;
}
}
Create ConnectorFactory class inside com.tap.connector package
package com.tap.connector;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
package jdbc;
import java.util.List;
import com.tap.dao.EmployeeDAOImpl;
import com.tap.dto.Employee;
for(Employee e :employees) {
System.out.println(e);
}
}
}
Output:
getEmployee(id)-
e = new
Employee(res.getInt(1),res.getString(2),res.getString(3),res.getInt(4));
} catch (Exception e2) {
e2.printStackTrace();
}
return e;
}
Let us now write the code in DaoDriver class so that we can get one employee row/record.
package jdbc;
import java.util.List;
import java.util.Scanner;
import com.tap.dao.EmployeeDAOImpl;
import com.tap.dto.Employee;
Employee e = emplDAOImpl.getEmployee(id);
System.out.println(e);
}
Output:
try {
Connection con = ConnectorFactory.requestConnection();
String query = "update emp set salary = ? where id = ?";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setInt(1, e.getSalary());
pstmt.setInt(2, e.getId());
i = pstmt.executeUpdate();
}
catch (Exception e2) {
e2.printStackTrace();
}
if(i==1) {
return true;
}
else {
return false;
}
}
Let us now write the code in DaoDriver class so that we can update employee’s salary-
package jdbc;
import java.util.List;
import java.util.Scanner;
import com.tap.dao.EmployeeDAOImpl;
import com.tap.dto.Employee;
Employee e = emplDAOImpl.getEmployee(id);
System.out.println(e);
e.setSalary(newSalary);
System.out.println(emplDAOImpl.updateEmployee(e));
}
}
.
Output:
And the data would be updated in the database also-