There are various data access technologies to perform persistence operations in Java enterprise applications. JDBC is among the most popular one. Spring provides data access operations performed with JDBC using three main approaches:

  1. Using Template Method pattern based utility classes, namely JdbcTemplate and NamedParameterJdbcTemplate, to perform JDBC operations more easily by removing repetitive data access code blocks in the application, properly handling resource cleanups, and so on.
  2. Using database meta data to simplify queries using classes such as SimpleJdbcInsert and SimpleJdbcCall. That way you need to provide only a table or stored procedure name and a map of parameters corresponding to column names to perform an SQL operation.
  3. Using MappingSqlQuery, SqlUpdate, and StoredProcedure classes to represent database operations as reusable Java objects so that you can use them over and over again by providing only different query parameters each time.

To start working with JDBC from Spring application, you first need to obtain the data- base connection. Spring offers two ways to obtain database connections within the JDBC API. Data source or the DriverManager.

The DriverManager is the legacy approach and DataSource is newer one. It is recommended to use the new DataSource facility to connect to databases and other resources. DataSource facility has several advantages over DriverManager facility like DataSource increases portability, enables connection pooling and distributed transactions, the DriverManager does not allow such techniques.

If you’re using Maven project configuration, you need to add the following Spring dependencies elements into your pom.xml:

Maven dependency

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.2.1.RELEASE</version>
</dependency>

<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>

Gradle dependency

'org.springframework:spring-jdbc:4.2.1.RELEASE'
'commons-dbcp:commons-dbcp:1.4'

The core class of Spring’s JDBC support is JdbcTemplate. It simplifies the use of JDBC and helps to avoid common errors. JdbcTemplate can be used to execute SQL queries or insert, update, and delete statements. It executes core JDBC work flow, initiating iteration over ResultSets and catching JDBC exceptions and translating them to the generic, more informative DataAccessException hierarchy defined by Spring. Application code only needs to provide SQL and ResultSet processing logic if necessary.

Now that we understood the basics of Spring JDBC, in the next post we will continue with creating our first Spring JDBC application.

This article is about: JDBC,

Nilanchala

A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.