0% found this document useful (0 votes)
2 views

1-Day-1

The document outlines the Spring JDBC Module, focusing on the Data Access Object (DAO) design pattern used in enterprise applications to separate data access logic from business processing logic. It details the advantages and drawbacks of using DAOs, guidelines for implementation, and provides examples of DAO interfaces, implementations, and related components such as DTOs and factory classes. Additionally, it includes instructions for setting up a project with necessary dependencies and HTML forms for student management.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

1-Day-1

The document outlines the Spring JDBC Module, focusing on the Data Access Object (DAO) design pattern used in enterprise applications to separate data access logic from business processing logic. It details the advantages and drawbacks of using DAOs, guidelines for implementation, and provides examples of DAO interfaces, implementations, and related components such as DTOs and factory classes. Additionally, it includes instructions for setting up a project with necessary dependencies and HTML forms for student management.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Java Frameworks Package

SPRING JDBC MODULE


Spring-DAO:
-->DAO [Data Access Object], it is a design pattern, it able to provide very good
Environment to separate Data Access logic from Business Processing logic.

-->In Enterprise Applications, to prepare Data Access Layer we will use DAO Design pattern.

Advantages of DAOs in Enterprise Applications:

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

Maintenance of Enterprise Applications.

4) While preparing Service/Business layer Code Simplification is possible, that is, it is very simple to

Prepare Service/Business layer.

5) We are able get Standardization to prepare Data Access layer with DAOs.

Drawbacks 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.

Guidelines to prepare DAOs in Enterprise Applications:

1. Prepare a separate DAO interface:


Prepare a separate DAO interface with the required DAO methods, which must represent
CRUD operations.

EX:

public interface StudentDao


{

1 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
public String addStudent(Student std) throws DAOException;
public String updateStudent(Student std)throws DAOException;
public String deleteStudent(String sid)throws DAOException;
public List<Student> findAllStudents()throws DAOException;
public Student findStudentByID(String sid)throws DAOException;
public Student findStudentByName(String sname)throws DAOException;
-----
-----

2. Provide an implementation to DAO interface:

public class StudentDAOImpl implements StudentDao


{
-implementation for all StudentDao interface methods-----
---> We can provide our methods in order to improve code reusability along with
DAO interface methods---
}

3. Prepare DTOs as per the requirement:

public class Student


{
private String sid;
private String sname;
private String saddr;
-----
-----
setXXX() and getXXX()
-----
-----

4. Create Factory Methods/ Factory Classes to generate DAOs:

public class StudentDaoFactory


{
private static StudentDao dao;

static
{
dao = new StudentDaoImpl();
}
public static StudentDao getStudentDao()
{

2 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
return dao;
}
}

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.

6) In case of DAOs, It is suggestible to interact with Databases by using Connection Pooling


mechanisms, not by using DriverManager approach.

7) DAO is not threadsafe, we must not use DAOs in multi-threaded environment.


8) In DAOs we can access close() method in order to close the resources like
connections,.... , so here, before calling close() method we must ensure that whether the
resources are going to be released or not with our close() method call.

9) We have make sure that all the objects which are used by DAOs are following Java bean
conventions or not.

Application on Servlets and JSPs with DAO:

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:

Let's first create a table in the mysql database,

but before creating table, we need to create database first.

create database College;

use College;

4 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

create table Student

SId varchar(10) PRIMARY KEY NOT NULL,

SName varchar(40),

SAddr varchar(40)

);

Select * from Student;

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Project Name [CrudDaoDemo]

5 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
After that need to confirm By Pressing Y and Press Enter Key.

Add Some Dependencies

First of all add compiler dependencies.

Ex:
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
Servlet maven dependency

JSP maven dependency

Jstl dependency

Spring core

Spring Context

MySql Connector

6 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

All Maven Dependencies Which is Used:-

<!--
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>

7 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

<!--
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>

Within webapp folder.

8 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

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>

9 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="maroon">
<center>
<font color="white" size="7">
<b>
Career Compiler SOFTWARE SOLUTIONS
</b>
</font>
</center>
</body>
</html>

[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>

After that set welcome page in web.xml file. [WEB-INF\web.xml]


<web-app>
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>
layout.html
</welcome-file>
</welcome-file-list>
</web-app>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Then execute this project.

Don’t use below welcome code it is just for explanation.

[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>

12 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
<br/><br/><br/>
<table>
<tr>
<td>Student Id</td><td><input type="text" name="sid"/></td>
</tr>
<tr>
<td>Student Name</td><td><input type="text"
name="sname"/></td>
</tr>
<tr>
<td>Student Address</td><td><input type="text"
name="saddr"/></td>
</tr>
<tr>
<td>
<input type="submit" value="ADD" name="button"/>
</tr>
</table>
</center>
</form>
</body>
</html>

[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>

13 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
</tr>
</table>
</center>
</form>
</body>
</html>

[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">

14 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>
Student Existed Already
</b>
</font>
</center>
</body>
</html>

[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

16 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
updatehome.jsp

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>

After this create some packages within Java Resources


Folder [src/main/java]
1)com.ccteam.beans
2)com.ccteam.controller
3)com.ccteam.services
4)com.ccteam.dao
5)com.ccteam.factory
[Student.java]
Create this within [com.ccteam.beans] package
package com.ccteam.beans;
public class Student
{
private String sid;
private String sname;
private String saddr;

public String getSid()


{
return sid;
}
public void setSid(String sid)
{
this.sid = sid;

18 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

}
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;

public class ControllerServlet extends HttpServlet


{

private static final long serialVersionUID = 1L;

19 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
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException
{
String button_Label = request.getParameter("button");
String status = "";
RequestDispatcher rd = null;
if(button_Label.equals("ADD"))
{
StudentService service =
StudentServiceFactory.getStudentService();
Student st = new Student();
st.setSid(request.getParameter("sid"));
st.setSname(request.getParameter("sname"));
st.setSaddr(request.getParameter("saddr"));
status = service.addStudent(st);

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");

20 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
StudentService service =
StudentServiceFactory.getStudentService();

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);

21 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
}
}

}
}

[StudentService.java]
package com.ccteam.services;
import com.ccteam.beans.Student;

public interface StudentService


{
public String addStudent(Student st);
public Student searchStudent(String sid);
public String deleteStudent(String sid);
}

[StudentServiceImpl]
package com.ccteam.services;

import com.ccteam.beans.Student;

import com.ccteam.dao.StudentDao;

import com.ccteam.factory.StudentDaoFactory;

public class StudentServiceImpl implements StudentService

String status="";

@Override

public String addStudent(Student st)

StudentDao dao = StudentDaoFactory.getStudentDao();

22 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

status = dao.add(st);

return status;

@Override

public Student searchStudent(String sid)

Student st = null;

StudentDao dao = StudentDaoFactory.getStudentDao();

st = dao.search(sid);

return st;

@Override

public String deleteStudent(String sid)

StudentDao dao = StudentDaoFactory.getStudentDao();

status = dao.delete(sid);

return status;

[StudentDao.java]
package com.ccteam.dao;

import com.ccteam.beans.Student;

23 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

public interface StudentDao


{
public String add(Student st);
public Student search(String sid);
public String delete(String sid);
}

[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;

public class StudentDaoImpl implements StudentDao


{
String status = "";
@Override
public String add(Student st)
{
try
{
Connection con =
ConnectionFactrory.getConnection();
PreparedStatement pst =
con.prepareStatement("select * from student where StuId = ?");
pst.setString(1, st.getSid());
ResultSet rs = pst.executeQuery();
boolean b = rs.next();
if( b == true )
{
status="existed";
}
else
{
pst = con.prepareStatement("insert into
student values(?,?,?)");

24 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
pst.setString(1,st.getSid());
pst.setString(2, st.getSname());
pst.setString(3, st.getSaddr());
pst.executeUpdate();
status = "success";
}
}
catch (Exception e)
{
status = "failure";
e.printStackTrace();
}
return status;
}
@Override
public Student search(String sid)
{
Student st=null;
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)
{
st = new Student();
st.setSid(rs.getString("SID"));
st.setSname(rs.getString("SNAME"));
st.setSaddr(rs.getString("SADDR"));
}
else
{
st = null;
}
}
catch (Exception e)
{
e.printStackTrace();
}
return st;

25 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

}
@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;

26 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

public class StudentServiceFactory

private static StudentService service;

static

service = new StudentServiceImpl();

public static StudentService getStudentService()

return service;

[StudentDaoFactory.java]
package com.ccteam.factory;

import com.ccteam.dao.StudentDao;

import com.ccteam.dao.StudentDaoImpl;

public class StudentDaoFactory

private static StudentDao dao;

static

dao = new StudentDaoImpl();

27 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

public static StudentDao getStudentDao()

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;
}
}

28 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

And Finally Run the Application.


Complete [pom.xml] file
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ccteam</groupId>
<artifactId>app46cruddaodemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>app46cruddaodemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>

</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

29 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
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</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>

30 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
<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>

</dependencies>
<build>
<finalName>app46cruddaodemo</finalName>
</build>
</project>

31 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)

You might also like