SlideShare a Scribd company logo
DATA ACCESS WITH JDBC
HARSHIT CHOUDHARY
OUTLINE
 Spring Data Access
 DAO Support
 Introduction to Spring Framework JDBC
 Using JDBC Core Classes
 Controlling Database connections
HARSHIT CHOUDHARY
DAO SUPPORT IN SPRING
HARSHIT CHOUDHARY
INTRODUCTION
 The Data Access Object (DAO) is a pattern to access the data of the database
 DAO Support in Spring makes it easy to work with data access technologies in a consistent
way
 This allows to switch between different persistence technologies easily and without catching
technology specific exceptions
HARSHIT CHOUDHARY
CONSISTENT EXCEPTION HIERARCHY
 Spring provides a convenient translation from technology-specific exception to its own exception class
hierarchy.
 These exceptions wrap the original exception so there is never risk to loose any information
 DataAccessException is the root exception in Spring, which is a Runtime Exception and developers are
not forced to handle the exception
HARSHIT CHOUDHARY
SPRING EXCEPTION HIERARCHY
HARSHIT CHOUDHARY
ANNOTATION FOR CONFIGURING DAO CLASSES
 @Repository
 Provides exception translation
 Allows component scanning support
HARSHIT CHOUDHARY
DATA ACCESS WITH JDBC
HARSHIT CHOUDHARY
OVERVIEW
 Spring JDBC Framework allows developers to focus on core logic
 Spring handles all the low level details and repetitive tasks like opening and closing of connections,
exceptions, iteration of resultsets, transactions etc.
 The table in next slide shows what actions spring take care of and which actions are responsibility of
application developer
HARSHIT CHOUDHARY
SPRING JDBC – WHO DOES WHAT?
HARSHIT CHOUDHARY
Action Spring You
Define connection parameters. X
Open the connection. X
Specify the SQL statement. X
Declare parameters and provide
parameter values
X
Prepare and execute the statement. X
Set up the loop to iterate through
the results (if any).
X
Do the work for each iteration. X
Process any exception. X
Handle transactions. X
Close the connection, statement and
resultset.
X
SPRING DATA ACCESS APPROACHES
 JdbcTemplate –
 JdbcTemplate approach works for JDK 1.4 onwards and is widely used.
 It is used internally in other three approaches.
 handles the repetitive operations like opening and closing of connections and can execute select, insert, update statements, calls to stored procedures.
 This class is thread safe.
 NamedParameterJdbcTemplate –
 supports the use of named parameters in place of traditional place holders ( ? ) and works with JDK 1.4 onwards.
 SimpleJdbcTemplate-
 Uses both JdbcTemplate and NamedParameterJdbcTemplate approaches and provides additional support of Java 5 features etc.
 This approach requires JDK 5 onwards.
 Deprecated in Spring 4.x onwards
 SimpleJdbcInsert and SimpleJdbcCall –
 utilizes the database metadata and hence reduces the configuration overhead.
HARSHIT CHOUDHARY
PACKAGE HIERARCHY
 The framework consists of 4 packages
 Core
 Contains JDBCTemplate class and its various call back interfaces
Subpackages of Core
 Simple
 Contains the SimpleJdbcInsert and SimpleJdbcCall classes
 Namedparam
 Contains the NamedParameterJdbcTemplate class
 Datasource
 Utility class for easy DataSource access and various simple DataSource implementations
 Object
 Contains classes that represent RDBMS queries, updates and stored procedures as thread-safe reusable objects
 Support
 Provides SQLException translation functionality and some utility classes. Exceptions thrown during JDBC processing are translated to exceptions defined in
org.springframework.dao package
HARSHIT CHOUDHARY
JDBCTEMPLATE CLASS
 Central class in the JDBC core package
 Handles creation and release of resources, and statement creation and execution
 Executes SQL queries, update statements and stored procedure calls, perform iteration over ResultSet and
extraction of returned parameter values.
 Catches SQL Exceptions and translate them to related subclass of DataAccessException
 JdbcTemplate class requires a DataSource object which can be injected either programmatically or
declaratively
HARSHIT CHOUDHARY
QUERY (SELECT)
 Simple Query for getting the number of rows
 Simple query using BindVariable
 Querying and populating a single domain object
HARSHIT CHOUDHARY
QUERY (SELECT)
 Querying and populating a number of domain objects:
HARSHIT CHOUDHARY
ROWMAPPER INTERFACE
 RowMapper interface is used for mapping rows of a ResultSet on a per row basis to a custom object
 It need to be implemented to provide the actual logic of mapping of ResultSet to a custom object
 Defines one method:
public Object mapRow(ResultSet rs, int rowNum) throws SQLException
 SQLException will be caught and handled by the calling JdbcTemplate
HARSHIT CHOUDHARY
UPDATING (INSERT/UPDATE/DELETE)
 Insert Query
 Delete Query
 Update Query
HARSHIT CHOUDHARY
NAMEDPARAMETER JDBC TEMPLATE
HARSHIT CHOUDHARY
OVERVIEW
 The class adds support for programming JDBC statements using named parameters instead of the placeholders (?)
 It wraps a JdbcTemplate and delegates much of its task to wrapped JdbcTemplate
 Usage Example of NamedParameterJdbcTemplate
 For giving more than one Parameter
HARSHIT CHOUDHARY
Named
Parameter Class to provide the values
for the named parameters
SQLPARAMETERSOURCE INTERFACE
 SQLParameterSource interface is a source of named parameter values to a
NamedJdbcParameterTemplate.
 Implementations of SQLParameterSource
 MapSqlParameterSource
 An adapter around a java.util.Map, where the keys are the parameter names and the values
are the parameter values
 BeanPropertySqlParameterSource
 This class wraps an arbitrary Java Bean and uses the properties of the wrapped JavaBean as
the source of named parameter values
HARSHIT CHOUDHARY
USING JAVABEAN TO PROVIDE PARAMETERS
HARSHIT CHOUDHARY
Class to provide Java
Bean parameters
CONTROLLING DATABASE CONNECTIONS
HARSHIT CHOUDHARY
DATASOURCE
 Spring obtains a connection to the database through DataSource
 A DataSource is a generalized connection factory
 It allows container to hide connection pooling and transaction management issues from the
application code
 DataSource can be obtained from JNDI or third party connection pool
implementation can also be provided
 Popular implementations are
 Apache Jakarta Commons DBCP
 C3P0
HARSHIT CHOUDHARY
DRIVERMANAGERDATASOURCE
 Spring’s DataSource implementation
 Doesnot provide pooling and will perform poorly when multiple requests for a connection are made
 Should be used only for testing purpose
 Configuring in xml file
HARSHIT CHOUDHARY
Ad

More Related Content

What's hot (20)

Spring Core
Spring CoreSpring Core
Spring Core
Pushan Bhattacharya
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
Purbarun Chakrabarti
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
Dzmitry Naskou
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
Jeevesh Pandey
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
Knoldus Inc.
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
Rasheed Waraich
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
Emprovise
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
nomykk
 
Spring boot
Spring bootSpring boot
Spring boot
sdeeg
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
Joshua Long
 
Spring boot
Spring bootSpring boot
Spring boot
Pradeep Shanmugam
 
Maven ppt
Maven pptMaven ppt
Maven ppt
natashasweety7
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
Santosh Kumar Kar
 
Spring Boot
Spring BootSpring Boot
Spring Boot
HongSeong Jeon
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans
Hitesh-Java
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
tola99
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
Jeevesh Pandey
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
Dzmitry Naskou
 
Testing with Spring: An Introduction
Testing with Spring: An IntroductionTesting with Spring: An Introduction
Testing with Spring: An Introduction
Sam Brannen
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
Guo Albert
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
Dzmitry Naskou
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
Rasheed Waraich
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
nomykk
 
Spring boot
Spring bootSpring boot
Spring boot
sdeeg
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
Joshua Long
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans
Hitesh-Java
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
tola99
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
Jeevesh Pandey
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
Dzmitry Naskou
 
Testing with Spring: An Introduction
Testing with Spring: An IntroductionTesting with Spring: An Introduction
Testing with Spring: An Introduction
Sam Brannen
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
Guo Albert
 

Similar to Spring jdbc (20)

Enterprise Spring
Enterprise SpringEnterprise Spring
Enterprise Spring
Emprovise
 
Jdbc
JdbcJdbc
Jdbc
DeepikaT13
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
DrMeenakshiS
 
Spring db-access mod03
Spring db-access mod03Spring db-access mod03
Spring db-access mod03
Guo Albert
 
Introduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applicationsIntroduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applications
Fulvio Corno
 
Data access
Data accessData access
Data access
Joshua Yoon
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)
Maher Abdo
 
Jdbc
JdbcJdbc
Jdbc
Yamuna Devi
 
Jdbc[1]
Jdbc[1]Jdbc[1]
Jdbc[1]
Fulvio Corno
 
JDBC programming
JDBC programmingJDBC programming
JDBC programming
Fulvio Corno
 
Java jdbc
Java jdbcJava jdbc
Java jdbc
Arati Gadgil
 
Jdbc api
Jdbc apiJdbc api
Jdbc api
kamal kotecha
 
Overview Of JDBC
Overview Of JDBCOverview Of JDBC
Overview Of JDBC
Mindfire Solutions
 
JDBC PPT(4).pptx java Database Connectivity
JDBC PPT(4).pptx java Database ConnectivityJDBC PPT(4).pptx java Database Connectivity
JDBC PPT(4).pptx java Database Connectivity
dhanushoneplus00
 
JDBC
JDBCJDBC
JDBC
Balwinder Kumar
 
Jdbc 4.0 New Features And Enhancements
Jdbc 4.0 New Features And EnhancementsJdbc 4.0 New Features And Enhancements
Jdbc 4.0 New Features And Enhancements
scacharya
 
Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)
Luzan Baral
 
Mule jdbc
Mule   jdbcMule   jdbc
Mule jdbc
Rajarajan Sadhasivam
 
3.java database connectivity
3.java database connectivity3.java database connectivity
3.java database connectivity
web360
 
Java Database Connectivity by shreyash simu dbce.pptx
Java Database Connectivity by shreyash simu dbce.pptxJava Database Connectivity by shreyash simu dbce.pptx
Java Database Connectivity by shreyash simu dbce.pptx
ash909077
 
Enterprise Spring
Enterprise SpringEnterprise Spring
Enterprise Spring
Emprovise
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
DrMeenakshiS
 
Spring db-access mod03
Spring db-access mod03Spring db-access mod03
Spring db-access mod03
Guo Albert
 
Introduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applicationsIntroduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applications
Fulvio Corno
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)
Maher Abdo
 
JDBC PPT(4).pptx java Database Connectivity
JDBC PPT(4).pptx java Database ConnectivityJDBC PPT(4).pptx java Database Connectivity
JDBC PPT(4).pptx java Database Connectivity
dhanushoneplus00
 
Jdbc 4.0 New Features And Enhancements
Jdbc 4.0 New Features And EnhancementsJdbc 4.0 New Features And Enhancements
Jdbc 4.0 New Features And Enhancements
scacharya
 
Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)
Luzan Baral
 
3.java database connectivity
3.java database connectivity3.java database connectivity
3.java database connectivity
web360
 
Java Database Connectivity by shreyash simu dbce.pptx
Java Database Connectivity by shreyash simu dbce.pptxJava Database Connectivity by shreyash simu dbce.pptx
Java Database Connectivity by shreyash simu dbce.pptx
ash909077
 
Ad

Recently uploaded (20)

Automation Hour 1/28/2022: Capture User Feedback from Anywhere
Automation Hour 1/28/2022: Capture User Feedback from AnywhereAutomation Hour 1/28/2022: Capture User Feedback from Anywhere
Automation Hour 1/28/2022: Capture User Feedback from Anywhere
Lynda Kane
 
"Client Partnership — the Path to Exponential Growth for Companies Sized 50-5...
"Client Partnership — the Path to Exponential Growth for Companies Sized 50-5..."Client Partnership — the Path to Exponential Growth for Companies Sized 50-5...
"Client Partnership — the Path to Exponential Growth for Companies Sized 50-5...
Fwdays
 
"Rebranding for Growth", Anna Velykoivanenko
"Rebranding for Growth", Anna Velykoivanenko"Rebranding for Growth", Anna Velykoivanenko
"Rebranding for Growth", Anna Velykoivanenko
Fwdays
 
Hands On: Create a Lightning Aura Component with force:RecordData
Hands On: Create a Lightning Aura Component with force:RecordDataHands On: Create a Lightning Aura Component with force:RecordData
Hands On: Create a Lightning Aura Component with force:RecordData
Lynda Kane
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Rock, Paper, Scissors: An Apex Map Learning Journey
Rock, Paper, Scissors: An Apex Map Learning JourneyRock, Paper, Scissors: An Apex Map Learning Journey
Rock, Paper, Scissors: An Apex Map Learning Journey
Lynda Kane
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Datastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptxDatastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptx
kaleeswaric3
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Salesforce AI Associate 2 of 2 Certification.docx
Salesforce AI Associate 2 of 2 Certification.docxSalesforce AI Associate 2 of 2 Certification.docx
Salesforce AI Associate 2 of 2 Certification.docx
José Enrique López Rivera
 
"PHP and MySQL CRUD Operations for Student Management System"
"PHP and MySQL CRUD Operations for Student Management System""PHP and MySQL CRUD Operations for Student Management System"
"PHP and MySQL CRUD Operations for Student Management System"
Jainul Musani
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Automation Hour 1/28/2022: Capture User Feedback from Anywhere
Automation Hour 1/28/2022: Capture User Feedback from AnywhereAutomation Hour 1/28/2022: Capture User Feedback from Anywhere
Automation Hour 1/28/2022: Capture User Feedback from Anywhere
Lynda Kane
 
"Client Partnership — the Path to Exponential Growth for Companies Sized 50-5...
"Client Partnership — the Path to Exponential Growth for Companies Sized 50-5..."Client Partnership — the Path to Exponential Growth for Companies Sized 50-5...
"Client Partnership — the Path to Exponential Growth for Companies Sized 50-5...
Fwdays
 
"Rebranding for Growth", Anna Velykoivanenko
"Rebranding for Growth", Anna Velykoivanenko"Rebranding for Growth", Anna Velykoivanenko
"Rebranding for Growth", Anna Velykoivanenko
Fwdays
 
Hands On: Create a Lightning Aura Component with force:RecordData
Hands On: Create a Lightning Aura Component with force:RecordDataHands On: Create a Lightning Aura Component with force:RecordData
Hands On: Create a Lightning Aura Component with force:RecordData
Lynda Kane
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Rock, Paper, Scissors: An Apex Map Learning Journey
Rock, Paper, Scissors: An Apex Map Learning JourneyRock, Paper, Scissors: An Apex Map Learning Journey
Rock, Paper, Scissors: An Apex Map Learning Journey
Lynda Kane
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Datastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptxDatastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptx
kaleeswaric3
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Salesforce AI Associate 2 of 2 Certification.docx
Salesforce AI Associate 2 of 2 Certification.docxSalesforce AI Associate 2 of 2 Certification.docx
Salesforce AI Associate 2 of 2 Certification.docx
José Enrique López Rivera
 
"PHP and MySQL CRUD Operations for Student Management System"
"PHP and MySQL CRUD Operations for Student Management System""PHP and MySQL CRUD Operations for Student Management System"
"PHP and MySQL CRUD Operations for Student Management System"
Jainul Musani
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Ad

Spring jdbc

  • 1. DATA ACCESS WITH JDBC HARSHIT CHOUDHARY
  • 2. OUTLINE  Spring Data Access  DAO Support  Introduction to Spring Framework JDBC  Using JDBC Core Classes  Controlling Database connections HARSHIT CHOUDHARY
  • 3. DAO SUPPORT IN SPRING HARSHIT CHOUDHARY
  • 4. INTRODUCTION  The Data Access Object (DAO) is a pattern to access the data of the database  DAO Support in Spring makes it easy to work with data access technologies in a consistent way  This allows to switch between different persistence technologies easily and without catching technology specific exceptions HARSHIT CHOUDHARY
  • 5. CONSISTENT EXCEPTION HIERARCHY  Spring provides a convenient translation from technology-specific exception to its own exception class hierarchy.  These exceptions wrap the original exception so there is never risk to loose any information  DataAccessException is the root exception in Spring, which is a Runtime Exception and developers are not forced to handle the exception HARSHIT CHOUDHARY
  • 7. ANNOTATION FOR CONFIGURING DAO CLASSES  @Repository  Provides exception translation  Allows component scanning support HARSHIT CHOUDHARY
  • 8. DATA ACCESS WITH JDBC HARSHIT CHOUDHARY
  • 9. OVERVIEW  Spring JDBC Framework allows developers to focus on core logic  Spring handles all the low level details and repetitive tasks like opening and closing of connections, exceptions, iteration of resultsets, transactions etc.  The table in next slide shows what actions spring take care of and which actions are responsibility of application developer HARSHIT CHOUDHARY
  • 10. SPRING JDBC – WHO DOES WHAT? HARSHIT CHOUDHARY Action Spring You Define connection parameters. X Open the connection. X Specify the SQL statement. X Declare parameters and provide parameter values X Prepare and execute the statement. X Set up the loop to iterate through the results (if any). X Do the work for each iteration. X Process any exception. X Handle transactions. X Close the connection, statement and resultset. X
  • 11. SPRING DATA ACCESS APPROACHES  JdbcTemplate –  JdbcTemplate approach works for JDK 1.4 onwards and is widely used.  It is used internally in other three approaches.  handles the repetitive operations like opening and closing of connections and can execute select, insert, update statements, calls to stored procedures.  This class is thread safe.  NamedParameterJdbcTemplate –  supports the use of named parameters in place of traditional place holders ( ? ) and works with JDK 1.4 onwards.  SimpleJdbcTemplate-  Uses both JdbcTemplate and NamedParameterJdbcTemplate approaches and provides additional support of Java 5 features etc.  This approach requires JDK 5 onwards.  Deprecated in Spring 4.x onwards  SimpleJdbcInsert and SimpleJdbcCall –  utilizes the database metadata and hence reduces the configuration overhead. HARSHIT CHOUDHARY
  • 12. PACKAGE HIERARCHY  The framework consists of 4 packages  Core  Contains JDBCTemplate class and its various call back interfaces Subpackages of Core  Simple  Contains the SimpleJdbcInsert and SimpleJdbcCall classes  Namedparam  Contains the NamedParameterJdbcTemplate class  Datasource  Utility class for easy DataSource access and various simple DataSource implementations  Object  Contains classes that represent RDBMS queries, updates and stored procedures as thread-safe reusable objects  Support  Provides SQLException translation functionality and some utility classes. Exceptions thrown during JDBC processing are translated to exceptions defined in org.springframework.dao package HARSHIT CHOUDHARY
  • 13. JDBCTEMPLATE CLASS  Central class in the JDBC core package  Handles creation and release of resources, and statement creation and execution  Executes SQL queries, update statements and stored procedure calls, perform iteration over ResultSet and extraction of returned parameter values.  Catches SQL Exceptions and translate them to related subclass of DataAccessException  JdbcTemplate class requires a DataSource object which can be injected either programmatically or declaratively HARSHIT CHOUDHARY
  • 14. QUERY (SELECT)  Simple Query for getting the number of rows  Simple query using BindVariable  Querying and populating a single domain object HARSHIT CHOUDHARY
  • 15. QUERY (SELECT)  Querying and populating a number of domain objects: HARSHIT CHOUDHARY
  • 16. ROWMAPPER INTERFACE  RowMapper interface is used for mapping rows of a ResultSet on a per row basis to a custom object  It need to be implemented to provide the actual logic of mapping of ResultSet to a custom object  Defines one method: public Object mapRow(ResultSet rs, int rowNum) throws SQLException  SQLException will be caught and handled by the calling JdbcTemplate HARSHIT CHOUDHARY
  • 17. UPDATING (INSERT/UPDATE/DELETE)  Insert Query  Delete Query  Update Query HARSHIT CHOUDHARY
  • 19. OVERVIEW  The class adds support for programming JDBC statements using named parameters instead of the placeholders (?)  It wraps a JdbcTemplate and delegates much of its task to wrapped JdbcTemplate  Usage Example of NamedParameterJdbcTemplate  For giving more than one Parameter HARSHIT CHOUDHARY Named Parameter Class to provide the values for the named parameters
  • 20. SQLPARAMETERSOURCE INTERFACE  SQLParameterSource interface is a source of named parameter values to a NamedJdbcParameterTemplate.  Implementations of SQLParameterSource  MapSqlParameterSource  An adapter around a java.util.Map, where the keys are the parameter names and the values are the parameter values  BeanPropertySqlParameterSource  This class wraps an arbitrary Java Bean and uses the properties of the wrapped JavaBean as the source of named parameter values HARSHIT CHOUDHARY
  • 21. USING JAVABEAN TO PROVIDE PARAMETERS HARSHIT CHOUDHARY Class to provide Java Bean parameters
  • 23. DATASOURCE  Spring obtains a connection to the database through DataSource  A DataSource is a generalized connection factory  It allows container to hide connection pooling and transaction management issues from the application code  DataSource can be obtained from JNDI or third party connection pool implementation can also be provided  Popular implementations are  Apache Jakarta Commons DBCP  C3P0 HARSHIT CHOUDHARY
  • 24. DRIVERMANAGERDATASOURCE  Spring’s DataSource implementation  Doesnot provide pooling and will perform poorly when multiple requests for a connection are made  Should be used only for testing purpose  Configuring in xml file HARSHIT CHOUDHARY