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

What's hot

Jdbc api
Jdbc apiJdbc api
Jdbc api
kamal kotecha
 
Overview Of JDBC
Overview Of JDBCOverview Of JDBC
Overview Of JDBC
Mindfire Solutions
 
Spring dependency injection
Spring dependency injectionSpring dependency injection
Spring dependency injectionsrmelody
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
Atul Saurabh
 
Java database connectivity with MYSQL
Java database connectivity with MYSQLJava database connectivity with MYSQL
Java database connectivity with MYSQL
Adil Mehmoood
 
Spring IOC advantages and developing spring application sample
Spring IOC advantages and developing spring application sample Spring IOC advantages and developing spring application sample
Spring IOC advantages and developing spring application sample
Sunil kumar Mohanty
 
Dependency Injection in Spring in 10min
Dependency Injection in Spring in 10minDependency Injection in Spring in 10min
Dependency Injection in Spring in 10minCorneil du Plessis
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
Dharani Kumar Madduri
 
Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySql
Dhyey Dattani
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
Vikas Jagtap
 
java jdbc connection
java jdbc connectionjava jdbc connection
java jdbc connection
Waheed Warraich
 
JDBC ppt
JDBC pptJDBC ppt
JDBC ppt
Rohit Jain
 
JDBC Java Database Connectivity
JDBC Java Database ConnectivityJDBC Java Database Connectivity
JDBC Java Database Connectivity
Ranjan Kumar
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
Vaishali Modi
 
jsp MySQL database connectivity
jsp MySQL database connectivityjsp MySQL database connectivity
jsp MySQL database connectivity
baabtra.com - No. 1 supplier of quality freshers
 
3 database-jdbc(1)
3 database-jdbc(1)3 database-jdbc(1)
3 database-jdbc(1)
hameedkhan2017
 

What's hot (20)

Jdbc api
Jdbc apiJdbc api
Jdbc api
 
Overview Of JDBC
Overview Of JDBCOverview Of JDBC
Overview Of JDBC
 
Spring dependency injection
Spring dependency injectionSpring dependency injection
Spring dependency injection
 
JDBC Tutorial
JDBC TutorialJDBC Tutorial
JDBC Tutorial
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Java database connectivity with MYSQL
Java database connectivity with MYSQLJava database connectivity with MYSQL
Java database connectivity with MYSQL
 
Jdbc
JdbcJdbc
Jdbc
 
Spring IOC advantages and developing spring application sample
Spring IOC advantages and developing spring application sample Spring IOC advantages and developing spring application sample
Spring IOC advantages and developing spring application sample
 
Dependency Injection in Spring in 10min
Dependency Injection in Spring in 10minDependency Injection in Spring in 10min
Dependency Injection in Spring in 10min
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
 
Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySql
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
JDBC
JDBCJDBC
JDBC
 
java jdbc connection
java jdbc connectionjava jdbc connection
java jdbc connection
 
JDBC ppt
JDBC pptJDBC ppt
JDBC ppt
 
JDBC
JDBCJDBC
JDBC
 
JDBC Java Database Connectivity
JDBC Java Database ConnectivityJDBC Java Database Connectivity
JDBC Java Database Connectivity
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
jsp MySQL database connectivity
jsp MySQL database connectivityjsp MySQL database connectivity
jsp MySQL database connectivity
 
3 database-jdbc(1)
3 database-jdbc(1)3 database-jdbc(1)
3 database-jdbc(1)
 

Similar to Spring framework DAO

Enterprise Spring
Enterprise SpringEnterprise Spring
Enterprise Spring
Emprovise
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
DrMeenakshiS
 
Introduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversIntroduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC Drivers
Kumar
 
Jdbc
Jdbc   Jdbc
Jdbc
Ishucs
 
Java Data Base Connectivity concepts.pptx
Java Data Base Connectivity concepts.pptxJava Data Base Connectivity concepts.pptx
Java Data Base Connectivity concepts.pptx
mukeshprasanth909
 
Final Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.pptFinal Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.ppt
TabassumMaktum
 
Jdbc
JdbcJdbc
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Mule jdbc
Mule   jdbcMule   jdbc
Jdbc
JdbcJdbc
Jdbc
Ishucs
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivitybackdoor
 
03-JDBC.pptx
03-JDBC.pptx03-JDBC.pptx
03-JDBC.pptx
HachaluHaile
 
Jdbc
JdbcJdbc
Java jdbc
Java jdbcJava jdbc
Java jdbc
Arati Gadgil
 
Jdbc new
Jdbc newJdbc new
Jdbc new
sumit kushwah
 
Java Web Programming Using Cloud Platform: Module 3
Java Web Programming Using Cloud Platform: Module 3Java Web Programming Using Cloud Platform: Module 3
Java Web Programming Using Cloud Platform: Module 3
IMC Institute
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)Maher Abdo
 

Similar to Spring framework DAO (20)

Enterprise Spring
Enterprise SpringEnterprise Spring
Enterprise Spring
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
 
JDBC
JDBCJDBC
JDBC
 
Introduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversIntroduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC Drivers
 
Jdbc
Jdbc   Jdbc
Jdbc
 
Java Data Base Connectivity concepts.pptx
Java Data Base Connectivity concepts.pptxJava Data Base Connectivity concepts.pptx
Java Data Base Connectivity concepts.pptx
 
Final Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.pptFinal Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.ppt
 
Jdbc
JdbcJdbc
Jdbc
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Mule jdbc
Mule   jdbcMule   jdbc
Mule jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivity
 
03-JDBC.pptx
03-JDBC.pptx03-JDBC.pptx
03-JDBC.pptx
 
Jdbc
JdbcJdbc
Jdbc
 
Java jdbc
Java jdbcJava jdbc
Java jdbc
 
Jdbc new
Jdbc newJdbc new
Jdbc new
 
Java Web Programming Using Cloud Platform: Module 3
Java Web Programming Using Cloud Platform: Module 3Java Web Programming Using Cloud Platform: Module 3
Java Web Programming Using Cloud Platform: Module 3
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)
 
JDBC.ppt
JDBC.pptJDBC.ppt
JDBC.ppt
 
Jdbc
JdbcJdbc
Jdbc
 

More from Anuj Singh Rajput

Web technology
Web technologyWeb technology
Web technology
Anuj Singh Rajput
 
Java script
Java scriptJava script
Java script
Anuj Singh Rajput
 
Html (hypertext markup language)
Html (hypertext markup language)Html (hypertext markup language)
Html (hypertext markup language)
Anuj Singh Rajput
 
Css
CssCss
Bootstrap
BootstrapBootstrap
Jsp session 13
Jsp   session 13Jsp   session 13
Jsp session 13
Anuj Singh Rajput
 
Jsp session 12
Jsp   session 12Jsp   session 12
Jsp session 12
Anuj Singh Rajput
 
Jsp session 11
Jsp   session 11Jsp   session 11
Jsp session 11
Anuj Singh Rajput
 
Jsp session 10
Jsp   session 10Jsp   session 10
Jsp session 10
Anuj Singh Rajput
 
Jsp session 9
Jsp   session 9Jsp   session 9
Jsp session 9
Anuj Singh Rajput
 
Jsp session 8
Jsp   session 8Jsp   session 8
Jsp session 8
Anuj Singh Rajput
 
Jsp session 7
Jsp   session 7Jsp   session 7
Jsp session 7
Anuj Singh Rajput
 
Jsp session 6
Jsp   session 6Jsp   session 6
Jsp session 6
Anuj Singh Rajput
 
Jsp session 5
Jsp   session 5Jsp   session 5
Jsp session 5
Anuj Singh Rajput
 
Jsp session 4
Jsp   session 4Jsp   session 4
Jsp session 4
Anuj Singh Rajput
 
Jsp session 3
Jsp   session 3Jsp   session 3
Jsp session 3
Anuj Singh Rajput
 
Jsp session 2
Jsp   session 2Jsp   session 2
Jsp session 2
Anuj Singh Rajput
 
Jsp session 1
Jsp   session 1Jsp   session 1
Jsp session 1
Anuj Singh Rajput
 
Servlet session 14
Servlet   session 14Servlet   session 14
Servlet session 14
Anuj Singh Rajput
 
Servlet session 13
Servlet   session 13Servlet   session 13
Servlet session 13
Anuj Singh Rajput
 

More from Anuj Singh Rajput (20)

Web technology
Web technologyWeb technology
Web technology
 
Java script
Java scriptJava script
Java script
 
Html (hypertext markup language)
Html (hypertext markup language)Html (hypertext markup language)
Html (hypertext markup language)
 
Css
CssCss
Css
 
Bootstrap
BootstrapBootstrap
Bootstrap
 
Jsp session 13
Jsp   session 13Jsp   session 13
Jsp session 13
 
Jsp session 12
Jsp   session 12Jsp   session 12
Jsp session 12
 
Jsp session 11
Jsp   session 11Jsp   session 11
Jsp session 11
 
Jsp session 10
Jsp   session 10Jsp   session 10
Jsp session 10
 
Jsp session 9
Jsp   session 9Jsp   session 9
Jsp session 9
 
Jsp session 8
Jsp   session 8Jsp   session 8
Jsp session 8
 
Jsp session 7
Jsp   session 7Jsp   session 7
Jsp session 7
 
Jsp session 6
Jsp   session 6Jsp   session 6
Jsp session 6
 
Jsp session 5
Jsp   session 5Jsp   session 5
Jsp session 5
 
Jsp session 4
Jsp   session 4Jsp   session 4
Jsp session 4
 
Jsp session 3
Jsp   session 3Jsp   session 3
Jsp session 3
 
Jsp session 2
Jsp   session 2Jsp   session 2
Jsp session 2
 
Jsp session 1
Jsp   session 1Jsp   session 1
Jsp session 1
 
Servlet session 14
Servlet   session 14Servlet   session 14
Servlet session 14
 
Servlet session 13
Servlet   session 13Servlet   session 13
Servlet session 13
 

Recently uploaded

Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 

Recently uploaded (20)

Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 

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
  • 23.
  • 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+” ’ ”;