Introduction to Spring JdbcTemplate

This is replacement of JDBC code. In this chapter will see how we can perform different crud operations using JdbcTemplate. We will be creating examples of how to create and read data from DB using Spring JDBC provided JdbcTemplate.

We will be using the artifact spring-boot-starter-jdbc provided by Spring Boot to configure our Spring JDBC configurations.

  1. Spring JdbcTemplate is the most important class in Spring JDBC package.

  2. JDBC produces a lot of boiler plate code, such as opening/closing a connection to a database, handling sql exceptions etc. It makes the code extremely cumbersome and difficult to read.

  3. Implementing JDBC in the Spring Framework takes care of working with many low-level operations (opening/closing connections, executing SQL queries, etc.).

  4. When working with the database in the Spring Framework, we only need to define the connection parameters from the database and register the SQL query, the rest of the work for us is performed by Spring.

  5. JDBC in Spring has several classes (several approaches) for interacting with the database. The most common of these is using the JdbcTemplate class. This is the base class that manages the processing of all events and database connections.

  6. The JdbcTemplate class executes SQL queries, iterates over the ResultSet, and retrieves the called values, updates the instructions and procedure calls, “catches” the exceptions, and translates them into the exceptions defined in the org.springframwork.dao package.

  7. Instances of the JdbcTemplate class are thread-safe. This means that by configuring a single instance of the JdbcTemplate class, we can then use it for several DAO objects.