DAO

REFERENCE
DAO Layer Explained
: DAO stand for Data Access Object, it is design pattern. The DAO contains a connection to a database and basic operation on the database. The other layer of the application can interact with the database by making an object of DAO class and calling their required method only. It is an important part of the application. It completely hides the data access logic / code from other part of the application. The
user / developer can only access their code through their API.

Data Access Object is a technique which separate the data access logic and object persistence. It also
provides the flexibility to change application prospective without effecting rest of the part of the
application. It provides a very simple and consistence API. Because of its simplicity it has become a
mandatory parts of a web application.

The DAO centralize all the operation on the database.
In the present project the DAO class gets connection from the connection factory class (It holds a
connection). The bean class gives the data on which operation is performed. Finally DAO class
performs the operation on the database and returns the response to the other layer of the application.
The internal processing of Data Access Object is given below.

One of the great advantage of DAO is the, you do not require to write a code for database connection
on every where, you just make an object of DAO class and call the required method to perform
operation on database.

2: What does load-on-startup element mean in web.xml?
– What does the ‘load-on-startup’ element mean in web.xml?
Answer: This element of the Deployment Descriptor (web.xml) is used to determine the order of
initializing the particular servlet when the application server starts up. The content of this element is
expected to be a positive integer and lower the number, earlier the particular servlet will be initialized
during server start up.

If the content is not a positive integer (or if no value is specified as the content) then the application
server is free to initialize the servlet any time it wants during the server start up.
This maybe an application server dependent thing… but, I guess it’s the same for almost all popular
application servers currently being used like WebLogic, WebSphere, Oracle Application Server 10g,
etc.
load-on-startup for Servlets:
……
<servlet id=”servlet1”>
<servlet-name>NameOfTheServlet</servlet-name>
<display-name>DisplayNameOfTheServlet</display-name>
<servlet-class>FullyQualifiedServletClassName</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
……
load-on-startup for JSPs:
……
<servlet>
<servlet-name>NameOfTheServletGenerated</servlet-name>
<jsp-file>RelativePathOfTheJSPFile</jsp-file>
<load-on-startup>3</load-on-startup>
</servlet>

Note you should always set load-on-startup element to be 0 so the servlet is initialized
when the server is started. This will prevent any delay for the first client which uses the
application.

MVC Architecture
The main aim of the MVC architecture is to separate the business logic and application data from the
presentation data to the user.
Here are the reasons why we should use the MVC design pattern.
1. They are resuable : When the problems recurs, there is no need to invent a new solution, we just have to follow the pattern and adapt it as necessary.
2. They are expressive: By using the MVC design pattern our application becomes more expressive.

5. What is need of DAO? Write one simple DAO example?
Ans.
DAO(Data Access logic) provide the machanism to saperate our bussiness logic from data access logic and both code can be change independently that lead to flexibility in our application. and it also provide security because when will see spurce code from web page only the business logic can be seen and cant see the data access logic . and it also provide reusability – DAO: Data Access Object. here the main concept of DAO is to

communicate with the database. DAO mainly contains
1.) DAOFactory: this specifies which kind of database (wether MySQL or ORACLE or MSSQL etc…).

2.) SQLFactory: which extends the DAOFactory. Here we do all the connection stuffs.deciding the driver, hostname, URL, username, password etc and also getting the Connection object.

3.) AdminDAO: this is a interface where we declare all the methods which we need in our application to perform all the database related operation.

4.) SQLAdminFactory: this implements the AdminDAO interface, all its method. this also gets the connection from SQLFactory.

5.) TransactionControlDAO: here we keep the commit and rollback methods.

if required…
6) ConnectionConstant: in this file we can mention the file name (.properties file) where we keep the connection detals, like hostname, username, password, etc…

6. Why do we use DAO?
Ans
: If i want to hide the persistence logic from other components and reduce the direct dependency  to implementflexible system with less maintenance in future then we use DAO. Using DAO to encapsulates all access to the persistence store and regardless what type of data store you used DAO provide uniform API to its client.

DAO completely hides the data store implementation from its client.When data store implementation is changed, no modification is required in the clients of DAO because DAO will be exposed to the client as an interface which will not be changed.

Only changes is its DAOimplementation. – Access to data varies depending upon the source of data (Relational Database, Object Oriented Database, Flat File and Business to Business integration systems). Such disparate data sources offer challenges to create a direct dependency between application code and data access code. Including the connectivity and data access code within application code introduces a tight coupling between the components (JSP, Servlet or Struts Action) and the data source implementation. Such code dependencies
in components make it difficult and tedious to migrate, the application from one type of data source to another. Data Access Object (DAO) makes abstract and encapsulate all access to the data source. The DAO manages the connection with the data source to obtain and store data. The DAO acts as an adapter between the application components (JSP, Servlet or Struts Action) and the data source.