user3124773
user3124773

Reputation: 59

What is the benefit of using JDBC template?

What is difference between JDBC Template and Hibernate?

I read somewhere JDBC Template gives benefit when we are working with bulk operation, is it correct?

Upvotes: 4

Views: 3311

Answers (2)

NangSaigon
NangSaigon

Reputation: 1263

JDBCTemplate allows us to work with JDBC easily while HibernateTemplate allows us to work with Hibernate easily. For example, here is a method in JDBCTemplate:

<T> T   queryForObject(String sql, Object[] args, RowMapper<T> rowMapper)

You just need to prepare your SQL (passed via the sql parameter) and parameters (passed via args), define the RowMapper which converts the resultset to a object of type T. The JDBCTemplate will do the rest for you like: creating preparestatement, open the connection, execute the SQL code... If there is any error, the JDBC template translates it then wraps it in a DataAccessException etc. Generally speaking, the JDBCTemplate facilitates our operations with JDBC.

As with the JDBCTemplate, When we work with Hibernate, the HibernateTemplate makes our operations easy.

You can get more information about JDBCTemplate here

Upvotes: 1

Ramachandran.A.G
Ramachandran.A.G

Reputation: 4948

Main thing , elimination of boilerplate code. In the old days when there was JDBC calls to be made on serverless environment , you always had 4-5 steps. Load the driver (Class.forName) , Get the Connection , Create the statement/prepared statement , Execute the query and get the ResultSet , iterate the resultset and get the results , close all handles you opened. Try catch SQLExceptions and other checked exceptions you get.

This got abstracted quite a lot , the Spring tooling allows you to manage a lot of these using configuration , simplifies and eliminates duplicate code and eliminates a lot of JDBC Errors.

Upvotes: 1

Related Questions