Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring SimpleJdbcInsert vs JdbcTemplate

I have a requirement where I have to insert a row into the Database and get the Key (Identity) back. I thought of using SimpleJdbcInsert for this. I am passing the Object of JdbcTemplate to my SimpleJdbcInsert and executing method executeAndReturnKey().

The same can be done using update() method of JdbcTemplate by setting PreparedStatement instead of Parameters Map.

I just want to know if JdbcTemplate is better in terms of performance and should I be using it over SimpleJdbcInsert? If so then what is the reason for it's superior performance?

Note: I'm not inserting a Batch of Records but a single record only.

Thanks

like image 531
user2004685 Avatar asked Sep 12 '15 10:09

user2004685


People also ask

What is the difference between JdbcTemplate and SimpleJdbcTemplate?

The SimpleJdbcTemplate has all the features of old JdbcTemplate and also support some features of Java 5 i.e varargs and autoboxing. It best suited when you need not to access all the feature of JdbcTemplate. It has a simpler API and basically construct to support java 5.

What is difference between Namedjdbctemplate and JdbcTemplate?

Functionally, there's no difference between Spring's JdbcTemplate and it's variant, NamedParameterJdbcTemplate except for : NamedParameterJdbcTemplate provides a better approach for assigning sql dynamic parameters instead of using multiple '?' in the statement.

Which is faster JPA or JdbcTemplate?

JdbcTemplate will most likely be faster when talking about pure query execution, because a JPA implementation will do more stuff: Parse JPQL (assuming you are using that) creating a SQL query out of that. executing it.

Which is better JdbcTemplate or hibernate?

Hibernate makes a lot of assumptions and forces you to think and code in a certain way. Using JdbcTemplate is easier because it's just a very thin wrapper around JDBC itself. The price here is that you will write thousands of lines of really boring code. Also, you will find that SQL strings are really hard to maintain.

What is spring JDBC-template?

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. 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:

How to insert a query using spring JDBC?

Following example will demonstrate how to insert a query using Spring JDBC. We'll insert one record in Student Table using SimpleJdbcInsert object. jdbcInsert − SimpleJdbcInsert object to insert record in student table. jdbcTemplateObject − StudentJDBCTemplate object to read student object in database.

What is simplejdbcinsert in JDBC?

SimpleJdbc classes provide an easy way to configure and run SQL statements. These classes use database metadata to build basic queries. So, SimpleJdbcInsert and SimpleJdbcCall classes provide an easier way to run insert and stored procedure calls. 5.1.

What are the classes in spring JDBC?

All the classes in Spring JDBC are divided into four separate packages: core — the core functionality of JDBC. Some of the important classes under this package include JdbcTemplate, SimpleJdbcInsert, SimpleJdbcCall and NamedParameterJdbcTemplate.


1 Answers

SimpleJdbcInsert vs JdbcTemplate

From docs.spring.io

JdbcTemplate is the classic Spring JDBC approach and the most popular. This "lowest level" approach and all others use a JdbcTemplate under the covers.

Note :all others use a JdbcTemplate under the covers

SimpleJdbcInsert optimize database metadata to limit the amount of necessary configuration. This approach simplifies coding so that you only need to provide the name of the table or procedure and provide a map of parameters matching the column names. This only works if the database provides adequate metadata. If the database doesn’t provide this metadata, you will have to provide explicit configuration of the parameters.

If you are going to use A SimpleJdbcInsert,then also the actual insert is being handled using JdbcTemplate. So definitely in terms of performance SimpleJdbcInsert can not be better that JdbcTemplate.

So performance wise you can not be beneficial using SimpleJdbcInsert.

But perform insert operations in to multiple tables by using SimpleJdbcInsert is having definitely better capability then JdbcTemplate class.There may be some situation in which you want to insert data in lot of tables and you may like to do less coding.In these situations, using of SimpleJdbcInsert can be a very good option.See this example to understand that.

like image 143
Bacteria Avatar answered Nov 15 '22 05:11

Bacteria