Data Access using Spring JDBC Template
There are a number of options for selecting an approach to form the basis for your JDBC database access. Spring framework provides the following approaches for JDBC database access:
- JdbcTemplate
- NamedParameterJdbcTemplate
- SimpleJdbcTemplate
- SimpleJdbcInsert and SimpleJdbcCall
Spring – JDBC Template
In this article, we will discuss about Spring JDBC Template and how to configure the JDBC Template to execute queries. Spring JDBC Template provides a fluent API that improves code simplicity and readability and the JDBC Template is used to connect to the database and execute SQL Queries.
JDBC (Java Database Connectivity) is an application programming interface (API) that defines how a client may access a database. It is a data access technology used for Java database connectivity. It provides methods to query and update data in a database and is oriented toward relational databases. JDBC offers a natural Java interface for working with SQL. JDBC is needed to provide a “Pure Java” solution for application development. JDBC API uses JDBC drivers to connect with the database.
Types of JDBC Drivers
There are 4 types of JDBC Drivers.
- JDBC-ODBC Bridge Driver
- Native API Driver (partially Java driver)
- Network Protocol Driver (fully Java driver)
- Thin Driver (fully java driver)
Advantages of JDBC API
- Automatically creates the XML format of data from the database.
- It supports query and stored procedures.
- Almost any database for which ODBC driver is installed can be accessed.
Disadvantages of JDBC API
- Writing a lot of codes before and after executing the query, such as creating connection, creating a statement, closing result-set, closing connection, etc.
- Writing exception handling code on the database logic.
- Repetition of these codes from one to another database logic is time-consuming.
These problems of JDBC API are eliminated by Spring JDBC-Template. It provides methods to write the queries directly that saves a lot of time and effort.
Contact Us