SlideShare a Scribd company logo
1 of 27
Java & JEE Training
Session 34 – Design Patterns, JDBC Best Practices
Page 1Classification: Restricted
Agenda
• Deployment Descripter
• Configuring and Mapping a Servlet
• The flow of the demo web apps
• JDBC Best practices
• Design Patterns
Page 2Classification: Restricted
Quick review of previous session…
• Session management using Servlet technology
• Cookies
• Hidden Form Fields
• URL rewriting
• HttpSession (Preferred)
Page 3Classification: Restricted
What we covered till now in Servlets…
• Servlet API – GenericServlet and HttpServlet
• Servlet LifeCycle
• Servlet Collaboration using response.sendRedirect()
• Forwarding and Including using RequestDispatcher
• Parameters and Attributes
• Session Tracking / Session Management
Deployment Descriptor
– A detailed look
Page 5Classification: Restricted
Configuring and Mapping a Servlet
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>mypackage.ControlServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
Examples of URL patterns:
Xyz.htm
/Hello
/Hello.do
Page 6Classification: Restricted
Servlet init parameters
<servlet>
<servlet-name>controlServlet</servlet-name>
<servlet-class>com.jenkov.butterfly.ControlServlet</servlet-class>
<init-param>
<param-name>myParam</param-name>
<param-value>paramValue</param-value>
</init-param>
</servlet> servletConfig.getInitParameter("myParam");
Page 7Classification: Restricted
Servlet load-on-startup
<servlet>
<servlet-name>controlServlet</servlet-name>
<servlet-class>com.jenkov.webui.ControlServlet</servlet-class>
<init-param><param-name>container.script.static</param-name>
<param-value>/WEB-INF/container.script</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
• If not specified OR if values is negative, it is left to the container and it can
load when it wants. Typically it loads on first request to the servlet.
• used to control when the servlet container should load the servlet. If set, it
loads the servlet as soon as the container starts.
• Lower numbers are loaded first.
Page 8Classification: Restricted
Context Parameters
<context-param>
<param-name>myParam</param-name>
<param-value>the value</param-value>
</context-param>
The flow of the demo web apps
Page 10Classification: Restricted
Flow of the demo app
CLIENT SERVER
/index.html
Sends back index.html (HTML
Code downloaded)
/Login
Login successful HTML
+ session id
/MoreInfo with parameters +
Session ID
Page with more information
/Logout
Page with Logout Success
Message
JDBC Best Practices
Page 12Classification: Restricted
Best Practice # 1
Use PreparedStatement
• Why?
• Prevents SQL Injection attacks.
• How?
• SQL Queries and precompiled, and bind variables are used.
Page 13Classification: Restricted
Best Practice # 2
Use Connection Pooling
• Why?
• Creating JDBC Connection takes relatively longer time, thus increasing
overall response time.
• How?
• Cache JDBC Connection in pool application and reuse the connection from
the pool.
Page 14Classification: Restricted
Best Practice # 3
Use JDBC Batch Update
• Why?
• Reduces number of database roundtrip which result in significant
performance gain for insertion and update queries.
• How?
• JDBC API provides addBatch() method to add SQL queries into batch and
executeBatch() to send batch queries for execution.
Page 15Classification: Restricted
Best Practice # 4
Use Bind Variables instead of String Concatenation
• Why?
• Better performance, prevent SQL Injection
• How?
• Use bind variables and setInt() and setString() and similar methods.
Page 16Classification: Restricted
Best Practice # 5
Always close Statement, PreparedStatement and Connection.
• Why?
• JDBC Connection and other classes are costly resources.
• How?
• Close all resources in finally block.
Page 17Classification: Restricted
Best Practice # 6
Use standard SQL statement and avoid using DB specific query until
necessary
• Why?
• Ensures portability of code.
• How?
• Use standard SQL and not database specific features.
Page 18Classification: Restricted
Best Practice # 7
Use Appropriate Design Patterns
• Why?
• Ensures maintainability of code.
• How?
• Use Factory pattern to create Connections or get connections from the
pool. Use DAO pattern for database access logic. Use Value Object to
return records from the DB.
Page 19Classification: Restricted
Best Practice # 8
Separate Service Layer from Database Access (DAO) Layer
• Why?
• Ensures separation of concerns, enhances usability and modularity of the
code. Easier to maintain transactions.
• How?
• Separate the DAO classes from the service classes. Use the service classes
to implement transactions.
Design Patterns
Value Object, DAO, Factory Pattern
Page 21Classification: Restricted
Value Object
• Used to compound related characteristics to form a meaningful object (i.e.
object of value)
e.g. To describe currency, “1000” “USD” make sense when compounded
together to form an object.
public class Money{
int amount;
String currency;
}
Page 22Classification: Restricted
Data Access Object
Value Object
in DAO
pattern
Page 23Classification: Restricted
Factory pattern
Page 24Classification: Restricted
Design Pattern Exercise
• Exercise: Create a class that can be instantiated
• max of 5 times.
• Max once.
Page 25Classification: Restricted
Topics to be covered in next session
• Design Patterns
• Flow of the Demo App
• Value objects
• Data Access object
• Factory pattern
Page 26Classification: Restricted
Thank you!

More Related Content

What's hot

WebLogic Developer Webcast 1: JPA 2.0
WebLogic Developer Webcast 1: JPA 2.0WebLogic Developer Webcast 1: JPA 2.0
WebLogic Developer Webcast 1: JPA 2.0
Jeffrey West
 

What's hot (18)

Session 25 - Introduction to JEE, Servlets
Session 25 - Introduction to JEE, ServletsSession 25 - Introduction to JEE, Servlets
Session 25 - Introduction to JEE, Servlets
 
Session 29 - Servlets - Part 5
Session 29 - Servlets - Part 5Session 29 - Servlets - Part 5
Session 29 - Servlets - Part 5
 
Session 28 - Servlets - Part 4
Session 28 - Servlets - Part 4Session 28 - Servlets - Part 4
Session 28 - Servlets - Part 4
 
Session 26 - Servlets Part 2
Session 26 - Servlets Part 2Session 26 - Servlets Part 2
Session 26 - Servlets Part 2
 
Session 40 - Hibernate - Part 2
Session 40 - Hibernate - Part 2Session 40 - Hibernate - Part 2
Session 40 - Hibernate - Part 2
 
JSP - Part 2 (Final)
JSP - Part 2 (Final) JSP - Part 2 (Final)
JSP - Part 2 (Final)
 
Spring - Part 4 - Spring MVC
Spring - Part 4 - Spring MVCSpring - Part 4 - Spring MVC
Spring - Part 4 - Spring MVC
 
Jsp & Ajax
Jsp & AjaxJsp & Ajax
Jsp & Ajax
 
JSP - Part 1
JSP - Part 1JSP - Part 1
JSP - Part 1
 
ATG Architecture
ATG ArchitectureATG Architecture
ATG Architecture
 
Java Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP BasicJava Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP Basic
 
Java Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet AdvancedJava Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet Advanced
 
Overview of atg framework
Overview of atg frameworkOverview of atg framework
Overview of atg framework
 
WebLogic Developer Webcast 1: JPA 2.0
WebLogic Developer Webcast 1: JPA 2.0WebLogic Developer Webcast 1: JPA 2.0
WebLogic Developer Webcast 1: JPA 2.0
 
Lecture 2: Servlets
Lecture 2:  ServletsLecture 2:  Servlets
Lecture 2: Servlets
 
Enterprise java unit-3_chapter-1-jsp
Enterprise  java unit-3_chapter-1-jspEnterprise  java unit-3_chapter-1-jsp
Enterprise java unit-3_chapter-1-jsp
 
Entity Framework Overview
Entity Framework OverviewEntity Framework Overview
Entity Framework Overview
 
Java Web Programming [1/9] : Introduction to Web Application
Java Web Programming [1/9] : Introduction to Web ApplicationJava Web Programming [1/9] : Introduction to Web Application
Java Web Programming [1/9] : Introduction to Web Application
 

Similar to Session 34 - JDBC Best Practices, Introduction to Design Patterns

Similar to Session 34 - JDBC Best Practices, Introduction to Design Patterns (20)

Max Yekaterynenko - Magento Architecture, Next Steps
Max Yekaterynenko - Magento Architecture, Next StepsMax Yekaterynenko - Magento Architecture, Next Steps
Max Yekaterynenko - Magento Architecture, Next Steps
 
What Is Angular 2 | Angular 2 Tutorial For Beginners | Angular Training | Edu...
What Is Angular 2 | Angular 2 Tutorial For Beginners | Angular Training | Edu...What Is Angular 2 | Angular 2 Tutorial For Beginners | Angular Training | Edu...
What Is Angular 2 | Angular 2 Tutorial For Beginners | Angular Training | Edu...
 
Managing Data in Jakarta EE Applications
Managing Data in Jakarta EE ApplicationsManaging Data in Jakarta EE Applications
Managing Data in Jakarta EE Applications
 
jdbc
jdbcjdbc
jdbc
 
Asp.Net MVC 5 in Arabic
Asp.Net MVC 5 in ArabicAsp.Net MVC 5 in Arabic
Asp.Net MVC 5 in Arabic
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 1...
 Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 1... Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 1...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 1...
 
Struts 2 - Introduction
Struts 2 - Introduction Struts 2 - Introduction
Struts 2 - Introduction
 
Jsp
JspJsp
Jsp
 
10 J D B C
10  J D B C10  J D B C
10 J D B C
 
Jdbc
JdbcJdbc
Jdbc
 
Java EE 8 Web Frameworks: A Look at JSF vs MVC
Java EE 8 Web Frameworks: A Look at JSF vs MVCJava EE 8 Web Frameworks: A Look at JSF vs MVC
Java EE 8 Web Frameworks: A Look at JSF vs MVC
 
Java- JDBC- Mazenet Solution
Java- JDBC- Mazenet SolutionJava- JDBC- Mazenet Solution
Java- JDBC- Mazenet Solution
 
Session 41 - Struts 2 Introduction
Session 41 - Struts 2 IntroductionSession 41 - Struts 2 Introduction
Session 41 - Struts 2 Introduction
 
Session 2: SQL Server 2012 with Christian Malbeuf
Session 2: SQL Server 2012 with Christian MalbeufSession 2: SQL Server 2012 with Christian Malbeuf
Session 2: SQL Server 2012 with Christian Malbeuf
 
Mvc15 (1)
Mvc15 (1)Mvc15 (1)
Mvc15 (1)
 
Professional Services Insights into Improving Sitecore XP
Professional Services Insights into Improving Sitecore XPProfessional Services Insights into Improving Sitecore XP
Professional Services Insights into Improving Sitecore XP
 
Work with data in ASP.NET
Work with data in ASP.NETWork with data in ASP.NET
Work with data in ASP.NET
 
BITM3730Week13.pptx
BITM3730Week13.pptxBITM3730Week13.pptx
BITM3730Week13.pptx
 
005428052.pdf
005428052.pdf005428052.pdf
005428052.pdf
 
005432796.pdf
005432796.pdf005432796.pdf
005432796.pdf
 

More from PawanMM

More from PawanMM (16)

Session 48 - JS, JSON and AJAX
Session 48 - JS, JSON and AJAXSession 48 - JS, JSON and AJAX
Session 48 - JS, JSON and AJAX
 
Session 46 - Spring - Part 4 - Spring MVC
Session 46 - Spring - Part 4 - Spring MVCSession 46 - Spring - Part 4 - Spring MVC
Session 46 - Spring - Part 4 - Spring MVC
 
Session 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOPSession 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOP
 
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based ConfigurationSession 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
 
Session 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI BeansSession 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI Beans
 
Session 42 - Struts 2 Hibernate Integration
Session 42 - Struts 2 Hibernate IntegrationSession 42 - Struts 2 Hibernate Integration
Session 42 - Struts 2 Hibernate Integration
 
Session 38 - Core Java (New Features) - Part 1
Session 38 - Core Java (New Features) - Part 1Session 38 - Core Java (New Features) - Part 1
Session 38 - Core Java (New Features) - Part 1
 
Session 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise JavaSession 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise Java
 
Session 23 - JDBC
Session 23 - JDBCSession 23 - JDBC
Session 23 - JDBC
 
Session 22 - Java IO, Serialization
Session 22 - Java IO, SerializationSession 22 - Java IO, Serialization
Session 22 - Java IO, Serialization
 
Session 21 - Inner Classes
Session 21 - Inner ClassesSession 21 - Inner Classes
Session 21 - Inner Classes
 
Session 20 - Collections - Maps
Session 20 - Collections - MapsSession 20 - Collections - Maps
Session 20 - Collections - Maps
 
Session 19 - Review Session
Session 19 - Review SessionSession 19 - Review Session
Session 19 - Review Session
 
Session 18 - Review Session and Attending Java Interviews
Session 18 - Review Session and Attending Java InterviewsSession 18 - Review Session and Attending Java Interviews
Session 18 - Review Session and Attending Java Interviews
 
Session 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, SetsSession 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, Sets
 
Session 16 - Collections - Sorting, Comparing Basics
Session 16 - Collections - Sorting, Comparing BasicsSession 16 - Collections - Sorting, Comparing Basics
Session 16 - Collections - Sorting, Comparing Basics
 

Recently uploaded

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 

Session 34 - JDBC Best Practices, Introduction to Design Patterns

  • 1. Java & JEE Training Session 34 – Design Patterns, JDBC Best Practices
  • 2. Page 1Classification: Restricted Agenda • Deployment Descripter • Configuring and Mapping a Servlet • The flow of the demo web apps • JDBC Best practices • Design Patterns
  • 3. Page 2Classification: Restricted Quick review of previous session… • Session management using Servlet technology • Cookies • Hidden Form Fields • URL rewriting • HttpSession (Preferred)
  • 4. Page 3Classification: Restricted What we covered till now in Servlets… • Servlet API – GenericServlet and HttpServlet • Servlet LifeCycle • Servlet Collaboration using response.sendRedirect() • Forwarding and Including using RequestDispatcher • Parameters and Attributes • Session Tracking / Session Management
  • 6. Page 5Classification: Restricted Configuring and Mapping a Servlet <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>helloServlet</servlet-name> <servlet-class>mypackage.ControlServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>helloServlet</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> </web-app> Examples of URL patterns: Xyz.htm /Hello /Hello.do
  • 7. Page 6Classification: Restricted Servlet init parameters <servlet> <servlet-name>controlServlet</servlet-name> <servlet-class>com.jenkov.butterfly.ControlServlet</servlet-class> <init-param> <param-name>myParam</param-name> <param-value>paramValue</param-value> </init-param> </servlet> servletConfig.getInitParameter("myParam");
  • 8. Page 7Classification: Restricted Servlet load-on-startup <servlet> <servlet-name>controlServlet</servlet-name> <servlet-class>com.jenkov.webui.ControlServlet</servlet-class> <init-param><param-name>container.script.static</param-name> <param-value>/WEB-INF/container.script</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> • If not specified OR if values is negative, it is left to the container and it can load when it wants. Typically it loads on first request to the servlet. • used to control when the servlet container should load the servlet. If set, it loads the servlet as soon as the container starts. • Lower numbers are loaded first.
  • 9. Page 8Classification: Restricted Context Parameters <context-param> <param-name>myParam</param-name> <param-value>the value</param-value> </context-param>
  • 10. The flow of the demo web apps
  • 11. Page 10Classification: Restricted Flow of the demo app CLIENT SERVER /index.html Sends back index.html (HTML Code downloaded) /Login Login successful HTML + session id /MoreInfo with parameters + Session ID Page with more information /Logout Page with Logout Success Message
  • 13. Page 12Classification: Restricted Best Practice # 1 Use PreparedStatement • Why? • Prevents SQL Injection attacks. • How? • SQL Queries and precompiled, and bind variables are used.
  • 14. Page 13Classification: Restricted Best Practice # 2 Use Connection Pooling • Why? • Creating JDBC Connection takes relatively longer time, thus increasing overall response time. • How? • Cache JDBC Connection in pool application and reuse the connection from the pool.
  • 15. Page 14Classification: Restricted Best Practice # 3 Use JDBC Batch Update • Why? • Reduces number of database roundtrip which result in significant performance gain for insertion and update queries. • How? • JDBC API provides addBatch() method to add SQL queries into batch and executeBatch() to send batch queries for execution.
  • 16. Page 15Classification: Restricted Best Practice # 4 Use Bind Variables instead of String Concatenation • Why? • Better performance, prevent SQL Injection • How? • Use bind variables and setInt() and setString() and similar methods.
  • 17. Page 16Classification: Restricted Best Practice # 5 Always close Statement, PreparedStatement and Connection. • Why? • JDBC Connection and other classes are costly resources. • How? • Close all resources in finally block.
  • 18. Page 17Classification: Restricted Best Practice # 6 Use standard SQL statement and avoid using DB specific query until necessary • Why? • Ensures portability of code. • How? • Use standard SQL and not database specific features.
  • 19. Page 18Classification: Restricted Best Practice # 7 Use Appropriate Design Patterns • Why? • Ensures maintainability of code. • How? • Use Factory pattern to create Connections or get connections from the pool. Use DAO pattern for database access logic. Use Value Object to return records from the DB.
  • 20. Page 19Classification: Restricted Best Practice # 8 Separate Service Layer from Database Access (DAO) Layer • Why? • Ensures separation of concerns, enhances usability and modularity of the code. Easier to maintain transactions. • How? • Separate the DAO classes from the service classes. Use the service classes to implement transactions.
  • 21. Design Patterns Value Object, DAO, Factory Pattern
  • 22. Page 21Classification: Restricted Value Object • Used to compound related characteristics to form a meaningful object (i.e. object of value) e.g. To describe currency, “1000” “USD” make sense when compounded together to form an object. public class Money{ int amount; String currency; }
  • 23. Page 22Classification: Restricted Data Access Object Value Object in DAO pattern
  • 25. Page 24Classification: Restricted Design Pattern Exercise • Exercise: Create a class that can be instantiated • max of 5 times. • Max once.
  • 26. Page 25Classification: Restricted Topics to be covered in next session • Design Patterns • Flow of the Demo App • Value objects • Data Access object • Factory pattern