What is 'DAO'?
DAO stands for Data Access Object, and it is one of the core patterns of J2EE.
Data Access Objects are used to avoid implementing business logic and data access logic together in the same body of code (usually in the middle tier of a J2EE application). 'Data Access Logic' in Java typically means database access calls using JDBC. When the business logic and the data access logic are not separated, two main problems arise:
- the business logic becomes harder to maintain, and
- it is harder to change the application to access a different database.
By introducing a DAO, these problems do not arise, as the DAO separates data access into another class that can be called from the business objects. This separation not only makes for a cleaner and more maintainable design, it also insulates the business logic layer from the data access mechanisms of the database. In other words, it becomes easier to switch from one database access mechanism to another, simply by plugging in another DAO. Provided the DAOs implement the same interface, then the business objects do not even have to know that the database has changed!