Sunday, February 4, 2018

Choosing an approach for JDBC in Spring

Spring JDBC provides multiple approaches in working with data access logic. It provides the following approaches to work with Data access logic; JdbcTemplate, NamedParameterJdbcTemplate, Simple JdbcTemplate, SimpleJdbcInsert, Mapping Sql Operations as sub classes.

1) JdbcTemplate – is the classic Spring JDBC approach and the most popular. All the other class in Spring JDBC will extends from this.
2) NamedParameterJdbcTemplate – This is a wrapper on JDBCTemplate and allows you to work with Named Parameters instead of (?) place holders. If you use (?) in you SQL Query, it would be tough to understand and track the values for those, so NamedParameterJdbcTemplate allows you to declare : NamedParameter in the sql and provides methods to replace those : params while executing the query.
3) SimpleJdbcTemplate – This combines the most frequently used operations of JdbcTemplate and NamedParameterJdbcTemplate.
4) SimpleJdbcInsert and SimpleJdbc Call – In this one it optimizes the amount of coding and only you need to provide the table on which you want to perform in Insert and need to provide Map of values representing the table columns. These classes rely on Database Table meta data in performing the operations.
5) Mapping SQL Operations as Sub classes (Using SqlQuery and SqlUpdate classes) – In this approach you will create reusable sub-classes by declaring the query and their parameter types once and compile it. And you can execute various operations on it by passing different values. This is similar to how you work with JDO.

Spring JDBC Framework classes has been distributed across four packages those are namely core, datasource, object and support. The JdbcTemplate has been declared in core package and all the datasource related classes has been declare in datasource package. Support and object contains some util and helper classes to make your jdbc work.

Thanks for reading. If you like this post please follow us for more updates about technology related updates.

No comments: