6

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

Mudassar
  • 3,135
  • 17
  • 22
user2004685
  • 9,548
  • 5
  • 37
  • 54

1 Answers1

11

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.

Bacteria
  • 8,406
  • 10
  • 50
  • 67