1-Day-1
1-Day-1
-->In Enterprise Applications, to prepare Data Access Layer we will use DAO Design pattern.
1) We are able to hide all data access implementation from Business / Service Layer.
2) It is very simple to switch data access layer from one data access tech to anther data access tech.
without giving any effect to Service/Business Layer, that is from JDBC to Hibernate,....
3) DAOs are able to provide centralized Data Access in enterprise Application, it simplifies the
4) While preparing Service/Business layer Code Simplification is possible, that is, it is very simple to
5) We are able get Standardization to prepare Data Access layer with DAOs.
1) It adds one more layer to enterprise Application, may get maintenance problems.
2) Along with DAOs, we have to implement some other Design patterns like Factory
Classes, DTO[Data Transfer Objects],....in enterprise applications.
EX:
static
{
dao = new StudentDaoImpl();
}
public static StudentDao getStudentDao()
{
In Service layer
StudentDao dao = StudentDao.getStudentDao();
5) We must not cache DAO references, because, Factory classes / Factory methods are providing
Single instances of DAO to the service layer, if DAO is required in multiple modules then it is
required to create more than one DAO reference.
9) We have make sure that all the objects which are used by DAOs are following Java bean
conventions or not.
Resources:
1. htmls:
a) addform.html
b) searchform.html
c) deleteform.html
d) existed.html
e) notexisted.html
3 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses
C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Java Frameworks Package
f) success.html
g) failure.html
h) layout.html
i) header.html
j) menu.html
k) welcome.html
l) footer.html
2. jsps
a) display.jsp
3.Servlets
a) ControllerServlet
4. Services:
a)StudentService
5.DAOs
a)StudentDao
6)DTOs
a)StudentDTo
7)Factories
a)ConnectionFactory
b)StudentServiceFactory
c)StudentDaoFactory
8)JARS:
Design Patterns:
a)DAO
b)MVC
c)DTO
d)Factory
MySQl:
use College;
SName varchar(40),
SAddr varchar(40)
);
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ex:
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
Servlet maven dependency
Jstl dependency
Spring core
Spring Context
MySql Connector
<!--
https://mvnrepository.com/artifact/javax.servlet/servle
t-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>provided</scope>
</dependency>
<!--
https://mvnrepository.com/artifact/javax.servlet.jsp/js
p-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/
spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.0.2</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/
spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-
connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
header.html
footer.html
welcome.html
menu.html
home.jsp
addform.html
addhome.jsp
[header.html]
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Header Page</title>
[footer.html]
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1">
<title>Footer Page</title>
</head>
<body bgcolor="blue">
<center>
<font color="white" size="5">
<b>
CCTeam Software Solutions, Bansal Complex, Agra
</b>
</font>
</center>
</body>
</html>
[welcome.html]
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1">
<title>Welcome Page</title>
</head>
<body bgcolor="lightblue">
10 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses
C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Java Frameworks Package
<center>
<br><br><br>
<font color="red" size="6">
<b>
<marquee>
Welcome To CCTeam Software Solutions
</marquee>
</b>
</font>
</center>
</body>
</html>
[menu.html]
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1">
<title>Menu Page</title>
</head>
<body bgcolor="lightyellow">
<center>
<h3>
<br><br>
<a href="./addform.html" target="body">Add Student</a><br><br>
<a href="./searchform.html" target="body">Search
Student</a><br><br>
<a href="./deleteform.html" target="body">Delete Student</a>
</h3>
</center>
</body>
</html>
[layout.html]
<!DOCTYPE html>
<frameset rows="20%,65%,15%">
<frame src="header.html"/>
<frameset cols="20%,80%">
<frame src="menu.html"/>
<frame src="welcome.html" name="body"/>
</frameset>
11 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses
C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Java Frameworks Package
<frame src="footer.html"/>
</frameset>
[addform.html]
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Add Form Page</title>
</head>
<body bgcolor="lightblue">
<form method="POST" action="./controller">
<center>
[searchform.html]
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="lightblue">
<form method="POST" action="./controller">
<br><br><br>
<center>
<table>
<tr>
<td>Student Id</td><td><input type="text" name="sid"/></td>
</tr>
<tr>
<td>
<input type="submit" value="SEARCH"
name="button"/></td>
[deleteform.html]
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="lightblue">
<form method="POST" action="./controller">
<br><br><br>
<center>
<table>
<tr>
<td>Student Id</td><td><input type="text" name="sid"/></td>
</tr>
<tr>
<td><input type="submit" value="DELETE" name="button"/></td>
</tr>
</table>
</center>
</form>
</body>
</html>
[existed.html]
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1">
<title>Existed Page</title>
</head>
<body bgcolor="lightblue">
[notexisted.html]
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="lightblue">
<center>
<br><br><br>
<font color="red" size="6">
<b>
Student Not Existed
</b>
</font>
</center>
</body>
</html>
[success.html]
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Insert title here</title>
15 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses
C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Java Frameworks Package
</head>
<body bgcolor="lightblue">
<center>
<br><br><br>
<font color="red" size="6">
<b>
Success
</b>
</font>
</center>
</body>
</html>
[failure.html]
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="lightblue">
<center>
<br><br><br>
<font color="red" size="6">
<b>
Failure
</b>
</font>
</center>
</body>
</html>
searchhome.jsp
updateform.html
deleteform.html
deletehome.jsp
success.jsp
successhome.jsp
failure.jsp
failurehome.jsp
existed.jsp
existedhome.jsp
[display.jsp]
<%@page import="com.ccteam.beans.Student"%>
<%!
Student st;
%>
<%
st = (Student)request.getAttribute("st");
%>
<html>
<body bgcolor="lightblue">
<center>
<br><br><br>
<table border = "1" bgcolor="white">
<tr>
<td>Student Id</td><td><%= st.getSid() %></td>
</tr>
<tr>
<td>Student Name</td><td><%= st.getSname() %></td>
</tr>
<tr>
<td>Student Address</td><td><%= st.getSaddr()
%></td>
</tr>
17 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses
C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Java Frameworks Package
</table>
</center>
</body>
</html>
}
public String getSname()
{
return sname;
}
public void setSname(String sname)
{
this.sname = sname;
}
public String getSaddr()
{
return saddr;
}
public void setSaddr(String saddr)
{
this.saddr = saddr;
}
[ControllerServlet.java]
package com.ccteam.controller;
import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import com.ccteam.beans.Student;
import com.ccteam.factory.StudentServiceFactory;
import com.ccteam.services.StudentService;
if(status.equals("success"))
{
rd =
request.getRequestDispatcher("./success.html
");
rd.forward(request, response);
}
if(status.equals("failure"))
{
rd =
request.getRequestDispatcher("./failure.html
");
rd.forward(request, response);
}
if(status.equals("existed"))
{
rd =
request.getRequestDispatcher("./existed.html
");
rd.forward(request, response);
}
}
if(button_Label.equals("SEARCH"))
{
String sid = request.getParameter("sid");
Student st = service.searchStudent(sid);
RequestDispatcher dispatcher = null;
if( st == null)
{
dispatcher =
request.getRequestDispatcher("notexisted.htm
l");
dispatcher.forward(request, response);
}
else
{
request.setAttribute("st", st);
dispatcher =
request.getRequestDispatcher("display.jsp");
dispatcher.forward(request, response);
}
}
if(button_Label.equals("DELETE"))
{
String sid = request.getParameter("sid");
StudentService service =
StudentServiceFactory.getStudentService();
status = service.deleteStudent(sid);
RequestDispatcher dispatcher = null;
if( status.equals("success"))
{
dispatcher =
request.getRequestDispatcher("success.html");
dispatcher.forward(request, response);
}
if( status.equals("failure"))
{
dispatcher =
request.getRequestDispatcher("failure.html");
dispatcher.forward(request, response);
}
if( status.equals("notexisted"))
{
dispatcher =
request.getRequestDispatcher("notexisted.html");
dispatcher.forward(request, response);
}
}
[StudentService.java]
package com.ccteam.services;
import com.ccteam.beans.Student;
[StudentServiceImpl]
package com.ccteam.services;
import com.ccteam.beans.Student;
import com.ccteam.dao.StudentDao;
import com.ccteam.factory.StudentDaoFactory;
String status="";
@Override
status = dao.add(st);
return status;
@Override
Student st = null;
st = dao.search(sid);
return st;
@Override
status = dao.delete(sid);
return status;
[StudentDao.java]
package com.ccteam.dao;
import com.ccteam.beans.Student;
[StudentDaoImpl.java]
package com.ccteam.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.ccteam.beans.Student;
import com.ccteam.factory.ConnectionFactrory;
}
@Override
public String delete(String sid)
{
try
{
Connection con =
ConnectionFactrory.getConnection();
PreparedStatement pst = con.prepareStatement("select * from
student where sid = ?");
pst.setString(1, sid);
ResultSet rs = pst.executeQuery();
boolean b = rs.next();
if(b == true)
{
pst = con.prepareStatement("delete from student where sid =
?");
pst.setString(1, sid);
pst.executeUpdate();
status = "success";
}
else
{
status = "notexisted";
}
}
catch (Exception e)
{
status = "failure";
e.printStackTrace();
}
return status;
}
}
[StudentServiceFactory.java]
package com.ccteam.factory;
import com.ccteam.services.StudentService;
import com.ccteam.services.StudentServiceImpl;
static
return service;
[StudentDaoFactory.java]
package com.ccteam.factory;
import com.ccteam.dao.StudentDao;
import com.ccteam.dao.StudentDaoImpl;
static
return dao;
[ConnnectionFactory.java]
package com.ccteam.factory;
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionFactory
{
private static Connection con;
static
{
try
{
//Class.forName("oracle.jdbc.OracleDriver");
Class.forName("com.mysql.cj.jdbc.Driver");
//con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe
", "system", "durga");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Col
lege","root","mysql");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection()
{
return con;
}
}
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-
api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-
api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-
core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.0.2</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-
context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependencies>
<build>
<finalName>app46cruddaodemo</finalName>
</build>
</project>