Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NamedParameterJdbcTemplate vs JdbcTemplate

I'm a beginner to Spring3.x , I'm learning Spring DAO support. I want to know the difference between NamedParameterJdbcTemplate and JdbcTemplate. Which one is the best by means of performance. And when to go for NamedParameterJdbcTemplate and when to go for JdbcTemplate.

like image 887
Ashok Avatar asked May 03 '13 12:05

Ashok


People also ask

What is the difference between JdbcTemplate and NamedParameterJdbcTemplate?

Spring - NamedParameterJdbcTemplate Example 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.

What is the use of NamedParameterJdbcTemplate?

NamedParameterJdbcTemplate class is a template class with a basic set of JDBC operations, allowing the use of named parameters rather than traditional '?' placeholders. This class delegates to a wrapped JdbcTemplate once the substitution from named parameters to JDBC style '?' placeholders is done at execution time.

Is JdbcTemplate deprecated?

JdbcTemplate queryForInt() is Deprecated - Mkyong.com.

What is the difference between JdbcTemplate and SimpleJdbcTemplate?

In JdbcTemplate query(), you need to manually cast the returned result to desire object type, and pass an Object array as parameters. In SimpleJdbcTemplate, it is more user friendly and simple. Note: The SimpleJdbcTemplate isn't a replacement for JdbcTemplate, it's just a java5-friendly supplement to it.


2 Answers

When you use JdbcTemplate you give it SQL that has a ? placeholder for each parameter you want substituted into the SQL. When you assign parameters in the code you have to pass in arguments in an array and they get used in the order in which they appear in the array, like this:

Object[] args = new Object[] {"x", "y"}; String sql = "select * from foo where a = ? and b = ?"; jdbcTemplate.query(sql, args, resultSetExtractor); 

so the SQL that gets run is select * from foo where a = 'x' and b = 'y'.

NamedParameterJdbcTemplate allows you to assign names to the parameter placeholders and pass in a map so the template can match the map names to the placeholders. So your code would look like:

String sql = "select * from foo where a = :mya and b = :myb"; Map<String, Object> argMap = new HashMap<String, Object>(); argMap.put("mya", "x"); argMap.put("myb", "y"); namedParameterJdbcTemplate.query(sql, argMap, resultSetExtractor); 

generating the same SQL as the first example.

Running the query is the time-intensive part, the performance of the argument insertion is a non-issue.

The idea is that matching the arguments by name is less error-prone than having to specify them in a particular order. In real-life applications I've worked on, typically we store the SQL in a separate file from the DAO code, and it may be easy to accidentally get the parameters in the wrong order.

like image 89
Nathan Hughes Avatar answered Sep 28 '22 10:09

Nathan Hughes


There's no measurable difference performance. The NamedParameterJdbcTemplate is a convenience that allows you to use named parameters. If you're really curious take a look at the source code which is readily available for download. I find that reading the source code gives me more confidence in the answers I get on the forums.

like image 25
Xenson Avatar answered Sep 28 '22 09:09

Xenson