Chapter 3. DAO and JDBC in Spring

In the previous chapter, you explored the concept of Inversion of Control (IoC). We then explored concepts such as, Spring Core Container, BeanFactory, and ApplicationContext, and then you learned how to implement them. We looked at Dependency Injection (DI) in Spring and their types: setter and constructor. We also wired beans using setter- and constructor-based Dependency Injection for the different data types.

In this chapter, we will cover the Data Access Object (DAO) design pattern. We will look at the simplified spring JDBC abstraction framework. We will implement the JDBC code using the Spring JDBC support and discuss how Spring manages DataSource that you can use in your applications. We will discuss data support in the Spring application.

When we talk about the Spring data support, it's specifically for the purpose of your application interacting with the data or the database, and you can typically write the Java code that interacts with the database. There are a few things that you have to do irrespective of what code you are going to write. You need to open the connection, manage the transaction, and then close the connection to write some boilerplate code. The whole point of using the Spring data support is that you can do away with all the extra boilerplate code and the code that you write specifically for the business case and the business problem that you want to resolve.

When we talk about writing a code that interacts with the database in Java, there are numerous ways we can do that. It could be as simple as JDBC or it could be some kind of framework, such as Hibernate or iBATIS. Spring supports lots of these technologies. The Spring JDBC module provides a kind of an abstraction layer and all the tedious JDBC code that we would otherwise have to write is provided by the JDBC module, which is in the Spring Framework.

The topics covered in this chapter are listed as follows:

  • Overview of database
  • The DAO design pattern
  • JDBC without Spring
  • The Spring JDBC packages
  • JDBC with Spring
  • What is JdbcTemplate
  • The JDBC batch operation in Spring
  • Calling the stored procedure

Overview of database

Databases are everywhere, but you never see them. They are concealed behind the tools and services that you use every day. Ever wondered where Facebook, Twitter, and Tumbler store their data? The answer is a database. Where does Google keep the details of the pages that it indexes from the Internet and where are the contacts stored in your mobile phone? Again, the answer is a database. In the information system, databases do most of the work that we do in our day-to-day lives. So, what is a database?

A database is a place where we store data. Databases are organized and structured. All the data that we store in the database fits into the database structure. Flat-file databases are simple databases. They store data in columns and rows.

Let's look at an Employee table:

Employee ID

First name

Last name

Age

Contact number

1

Ravi

Soni

28

+91-9986XXXXXX

2

Shree

Kant

22

+91-9986XXXXXX

Let's think about a simple database that the Human Resource (HR) has used to store his/her employee details. This database contains the name, address, birth date, and contact number of each employee. If the HR hires a new employee and would like to add the employee details to the database, then the HR will store the employee's first name, last name, address, date of birth, and mobile number in the database. The employee details that the HR writes down are stored in the fields of his employee address database. Each row is called a record, and each of the rows holds the information about the different employees in his/her employee address database. So, unlike a paper employee address book, the HR can carry out employee-related operations on his/her stored database. They can use the search option to find a particular employee's details.

In almost any business these days, there is a database or a collection of databases, and these are the main pieces of the backend infrastructure. Database is nothing but collection of data. There are different kinds of databases, such as Oracle, PostgreSQL, MySQL, and so on. The database software is called a relational database management system (RDBMS) and its instance is called a database engine. The database server is a machine that runs the database engine. We refer to the RDBMS, when we mention the term database throughout this book.

Tip

Refer to Appendix B, Apache Derby Database, to set up the Apache Derby database.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.211.231