SlideShare a Scribd company logo
SESSION – 5 & 6
By → Anuj Kumar Singh
What is Spring DAO (Data Access Object)
DAO is used to read and write data to and from database
respectively.The table below shows the responsibility of Spring and
User.
Note : The Spring Framework takes care of all the low-level details that
can make JDBC such a tedious API to develop with
Action Spring User
Define Connection Parameter Yes
Open the Connection Yes
Specify the SQL statement Yes
Declare parameters and provide parameter values Yes
Prepare and Execute the Statement Yes
Set up the loop to iterate through the result (if any) Yes
Do the work for each iteration Yes
Process any Exception Yes
Handle Transactions Yes
Close the Connection,Statement and ResultSet Yes
Steps for DAO
We have to follow three steps while configuring the Spring Context in XML.
Step 1: Configure Data Source
Driver Based Data Source
Step 2: Configure JDBC Template
JDBC Template
Step 3: Configure custom DAO Class
Step 1: Configure Data Source
The basic steps of JDBC Connection in Java, first we load the driver using
Class.forName(), then getting connection using DriverManager providing
the URL such as Connection con= DriverManager.getConnection(, ,) and
then using Statement or PreparedStatement.
In Spring we need to pass connection details (such as DriverName, URL,
Username, Password) to Spring framework. The benefit of configuring data
sources in this way is that they can be managed completely external to the
application, leaving the application to simply ask for a data source when it’s
ready to access the database.
Driver Based Data Source
It is the simplest data source that can be configured in Spring. There are
Two data source classes.
• DriverManagerDataSource
• SingleConnectionDataSource
The basic difference is DriverManagerDataSource provides a new
connection each time, where as SingleConnectionDataSource provides the
same connection. Let’s see an example, to connect MySQL database I am
using com.mysql.jdbc.Driver class. We use DriverManagerDataSource so
the configuration would be:
Your IOC of spring bean for database connection looks like
How it is if we take the connection details (Driver class name, url,
username, password etc. ) from a property file rather than defining in
Spring Context XML file itself?
Yes, it is a good idea to take the database connection details from a
property file. So let’s create a property file. We will name the file as:
jdbc.properties
You need this extra entry to include the properties from jdbc.properties file.
Properties define in jdbc.properties file
Step 2: Configure JDBC Template
After Driver Based Data Source the next step is JdbcTemplate. It is the
central class in the JDBC core package. It simplifies the use of JDBC
since it handles the creation and release of resources.
This helps to avoid common errors such as forgetting to always close
the connection.
This class executes SQL queries, update statements or stored
procedure calls, imitating iteration over ResultSets and extraction of
returned parameter values. It also catches JDBC exceptions and
translates them to the generic, more informative, exception hierarchy
defined in the org.springframework.dao package.
In Spring IOC container and given JdbcTemplate to DAOs as a bean
reference.
Step 3: Configure custom DAO Class
We already discussed that the JdbcTemplate can be used within a DAO
implementation via direct instantiation with a DataSource reference OR
be configured in a Spring IOC container and given to DAOs as a bean
reference. Now we will see how they can be implemented in the DAO
class.
Configured in Spring IOC container and given JdbcTemplate to DAOs
as a bean reference.
In all our examples, we adopted this option only…
In DAO Class
Bean id must be
JDBCTemplate
JDBCTemplate not
DataSource
Spring DAO  Example 1 : Inserting data in database by Test class
a. POJO class
b. DAO class
c. Test class
d. Xml File
Example 2  Inserting data in database with the Xml File
a. POJO class
b. DAO class
c. Test class
d. Xml File
Example 3Data Insertion and retrieval from Database
Spring framework  DAO
We can apply restriction also on the data access based on any particular
email.
String email=“anujkumarsingh@gmail.com”;
Then in query pass it like “Select * from emps where email= ‘ ”+email+” ’ ”;
Spring framework  DAO
Spring framework  DAO

More Related Content

PPTX
Spring jdbc dao
PDF
Introduction to Spring's Dependency Injection
PPTX
Spring JDBCTemplate
PPT
JEE5 New Features
PPTX
Introduction to jQuery
PPTX
Spring database - part2
PPTX
DataBase Connectivity
PPTX
1. java database connectivity (jdbc)
Spring jdbc dao
Introduction to Spring's Dependency Injection
Spring JDBCTemplate
JEE5 New Features
Introduction to jQuery
Spring database - part2
DataBase Connectivity
1. java database connectivity (jdbc)

What's hot (20)

PPS
Jdbc api
PDF
Overview Of JDBC
PPTX
Spring dependency injection
PPT
JDBC Tutorial
PPTX
Java database connectivity
PPT
Java database connectivity with MYSQL
PPTX
ODT
Spring IOC advantages and developing spring application sample
ODP
Dependency Injection in Spring in 10min
PPTX
Database Access With JDBC
PPTX
Java database connectivity with MySql
PPT
Jdbc ppt
PPT
java jdbc connection
PPTX
JDBC ppt
PPTX
PPT
JDBC Java Database Connectivity
PPT
Java database connectivity
PPTX
jsp MySQL database connectivity
PPT
3 database-jdbc(1)
Jdbc api
Overview Of JDBC
Spring dependency injection
JDBC Tutorial
Java database connectivity
Java database connectivity with MYSQL
Spring IOC advantages and developing spring application sample
Dependency Injection in Spring in 10min
Database Access With JDBC
Java database connectivity with MySql
Jdbc ppt
java jdbc connection
JDBC ppt
JDBC Java Database Connectivity
Java database connectivity
jsp MySQL database connectivity
3 database-jdbc(1)
Ad

Similar to Spring framework DAO (20)

PPTX
Enterprise Spring
PPT
jdbc_presentation.ppt
DOC
PPTX
JDBC PPT(4).pptx java Database Connectivity
PDF
Chapter 5 JDBC.pdf for stufent of computer andtudent It s
PDF
Introduction to JDBC and JDBC Drivers
PPTX
Jdbc
PPTX
Jdjdbcbc Jdjdbcbc JdjdbcJdjdbcbc Jdjdbcbc Jdjdbcbcbc JdJdbcbc
PPTX
Java Data Base Connectivity concepts.pptx
PPT
Final Database Connectivity in JAVA.ppt
PDF
PPSX
JDBC: java DataBase connectivity
PPTX
PPTX
Jdbc
PPT
Java Database Connectivity
PPTX
03-JDBC.pptx
PPTX
PPT
Java jdbc
PPTX
JDBCshwdbyuqwgdihbjwbdigwydwiuhsouhso NEW1.pptx
PPTX
21CS642 Module 5 JDBC PPT.pptx VI SEM CSE Students
Enterprise Spring
jdbc_presentation.ppt
JDBC PPT(4).pptx java Database Connectivity
Chapter 5 JDBC.pdf for stufent of computer andtudent It s
Introduction to JDBC and JDBC Drivers
Jdbc
Jdjdbcbc Jdjdbcbc JdjdbcJdjdbcbc Jdjdbcbc Jdjdbcbcbc JdJdbcbc
Java Data Base Connectivity concepts.pptx
Final Database Connectivity in JAVA.ppt
JDBC: java DataBase connectivity
Jdbc
Java Database Connectivity
03-JDBC.pptx
Java jdbc
JDBCshwdbyuqwgdihbjwbdigwydwiuhsouhso NEW1.pptx
21CS642 Module 5 JDBC PPT.pptx VI SEM CSE Students
Ad

More from Anuj Singh Rajput (20)

PPTX
Web technology
PPTX
Java script
PPTX
Html (hypertext markup language)
PPTX
PPTX
Jsp session 13
PPTX
Jsp session 12
PPTX
Jsp session 11
PPTX
Jsp session 10
PPTX
Jsp session 9
PPTX
Jsp session 8
PPTX
Jsp session 7
PPTX
Jsp session 6
PPTX
Jsp session 5
PPTX
Jsp session 4
PPTX
Jsp session 3
PPTX
Jsp session 2
PPTX
Jsp session 1
PPTX
Servlet session 14
PPTX
Servlet session 13
Web technology
Java script
Html (hypertext markup language)
Jsp session 13
Jsp session 12
Jsp session 11
Jsp session 10
Jsp session 9
Jsp session 8
Jsp session 7
Jsp session 6
Jsp session 5
Jsp session 4
Jsp session 3
Jsp session 2
Jsp session 1
Servlet session 14
Servlet session 13

Recently uploaded (20)

PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
01-Introduction-to-Information-Management.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Lesson notes of climatology university.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Pharma ospi slides which help in ospi learning
202450812 BayCHI UCSC-SV 20250812 v17.pptx
GDM (1) (1).pptx small presentation for students
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
01-Introduction-to-Information-Management.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Cell Types and Its function , kingdom of life
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Lesson notes of climatology university.
Final Presentation General Medicine 03-08-2024.pptx
VCE English Exam - Section C Student Revision Booklet
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
O7-L3 Supply Chain Operations - ICLT Program
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
STATICS OF THE RIGID BODIES Hibbelers.pdf
Cell Structure & Organelles in detailed.
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Pharma ospi slides which help in ospi learning

Spring framework DAO

  • 1. SESSION – 5 & 6 By → Anuj Kumar Singh
  • 2. What is Spring DAO (Data Access Object) DAO is used to read and write data to and from database respectively.The table below shows the responsibility of Spring and User. Note : The Spring Framework takes care of all the low-level details that can make JDBC such a tedious API to develop with
  • 3. Action Spring User Define Connection Parameter Yes Open the Connection Yes Specify the SQL statement Yes Declare parameters and provide parameter values Yes Prepare and Execute the Statement Yes Set up the loop to iterate through the result (if any) Yes Do the work for each iteration Yes Process any Exception Yes Handle Transactions Yes Close the Connection,Statement and ResultSet Yes
  • 4. Steps for DAO We have to follow three steps while configuring the Spring Context in XML. Step 1: Configure Data Source Driver Based Data Source Step 2: Configure JDBC Template JDBC Template Step 3: Configure custom DAO Class
  • 5. Step 1: Configure Data Source The basic steps of JDBC Connection in Java, first we load the driver using Class.forName(), then getting connection using DriverManager providing the URL such as Connection con= DriverManager.getConnection(, ,) and then using Statement or PreparedStatement. In Spring we need to pass connection details (such as DriverName, URL, Username, Password) to Spring framework. The benefit of configuring data sources in this way is that they can be managed completely external to the application, leaving the application to simply ask for a data source when it’s ready to access the database.
  • 6. Driver Based Data Source It is the simplest data source that can be configured in Spring. There are Two data source classes. • DriverManagerDataSource • SingleConnectionDataSource The basic difference is DriverManagerDataSource provides a new connection each time, where as SingleConnectionDataSource provides the same connection. Let’s see an example, to connect MySQL database I am using com.mysql.jdbc.Driver class. We use DriverManagerDataSource so the configuration would be:
  • 7. Your IOC of spring bean for database connection looks like
  • 8. How it is if we take the connection details (Driver class name, url, username, password etc. ) from a property file rather than defining in Spring Context XML file itself? Yes, it is a good idea to take the database connection details from a property file. So let’s create a property file. We will name the file as: jdbc.properties
  • 9. You need this extra entry to include the properties from jdbc.properties file. Properties define in jdbc.properties file
  • 10. Step 2: Configure JDBC Template After Driver Based Data Source the next step is JdbcTemplate. It is the central class in the JDBC core package. It simplifies the use of JDBC since it handles the creation and release of resources. This helps to avoid common errors such as forgetting to always close the connection. This class executes SQL queries, update statements or stored procedure calls, imitating iteration over ResultSets and extraction of returned parameter values. It also catches JDBC exceptions and translates them to the generic, more informative, exception hierarchy defined in the org.springframework.dao package.
  • 11. In Spring IOC container and given JdbcTemplate to DAOs as a bean reference.
  • 12. Step 3: Configure custom DAO Class We already discussed that the JdbcTemplate can be used within a DAO implementation via direct instantiation with a DataSource reference OR be configured in a Spring IOC container and given to DAOs as a bean reference. Now we will see how they can be implemented in the DAO class. Configured in Spring IOC container and given JdbcTemplate to DAOs as a bean reference. In all our examples, we adopted this option only…
  • 13. In DAO Class Bean id must be JDBCTemplate JDBCTemplate not DataSource
  • 14. Spring DAO  Example 1 : Inserting data in database by Test class a. POJO class
  • 18. Example 2  Inserting data in database with the Xml File a. POJO class
  • 22. Example 3Data Insertion and retrieval from Database
  • 24. We can apply restriction also on the data access based on any particular email. String email=“anujkumarsingh@gmail.com”; Then in query pass it like “Select * from emps where email= ‘ ”+email+” ’ ”;