Questions tagged [jdbctemplate]

The JdbcTemplate class is a key part of the Spring Framework JDBC abstraction. It takes care of opening and closing connections, translating exceptions etc. while offering a simple API to execute SQL commands.

The JdbcTemplate class is the central class in the JDBC core package.

  • It handles the creation and release of resources, which helps you avoid common errors such as forgetting to close the connection.

  • It performs the basic tasks of the core JDBC workflow such as statement creation and execution, leaving application code to provide SQL and extract results.

  • The JdbcTemplate class executes SQL queries, update statements and stored procedure calls, performs iteration over ResultSets and extraction of returned parameter values.

  • It also catches JDBC exceptions and translates them to the generic, more informative, exception hierarchy defined in the org.springframework.dao package.

[Source: Spring Reference -> 13.2.1 JdbcTemplate]

For named parameters, use the JDBC template provided by the framework – the NamedParameterJdbcTemplate.

This wraps the JbdcTemplate and provides an alternative to the traditional syntax using “?” to specify parameters. Under the hood, it substitutes the named parameters to JDBC “?” placeholder and delegates to the wrapped JDCTemplate to execute the queries:

Using JdbcTemplate, batch operations can be executed via the batchUpdate() API, BatchPreparedStatementSetter. You also have the option of batching operations with the NamedParameterJdbcTemplate – batchUpdate() API.

Reference: JdbcTemplate javadocs

2002 questions
193
votes
5 answers

How to execute IN() SQL queries with Spring's JDBCTemplate effectively?

I was wondering if there is a more elegant way to do IN() queries with Spring's JDBCTemplate. Currently I do something like that: StringBuilder jobTypeInClauseBuilder = new StringBuilder(); for(int i = 0; i < jobTypes.length; i++) { Type jobType…
Malax
  • 9,436
  • 9
  • 48
  • 64
129
votes
18 answers

Jdbctemplate query for string: EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0

I am using Jdbctemplate to retrieve a single String value from the db. Here is my method. public String test() { String cert=null; String sql = "select ID_NMB_SRZ from codb_owner.TR_LTM_SLS_RTN where id_str_rt =…
Byron
  • 3,833
  • 9
  • 34
  • 39
108
votes
5 answers

JPA vs Spring JdbcTemplate

For a new project is JPA always the recommended tool for handling relational data or are there scenarios where Spring JdbcTemplate is a better choice? Some factors to consider in your response: new database schema vs pre-existing schema and…
aem999
  • 16,923
  • 4
  • 22
  • 12
108
votes
6 answers

JdbcTemplate queryForInt/Long is deprecated in Spring 3.2.2. What should it be replaced by?

The queryforInt/queryforLong methods in JdbcTemplate are deprecated in Spring 3.2. I can't find out why or what is considered the best practice to replace existing code using these methods. A typical method: int rowCount =…
Dan MacBean
  • 1,915
  • 3
  • 15
  • 21
92
votes
7 answers

Spring JDBC Template for calling Stored Procedures

What is the correct way to invoke stored procedures using modern day (circa 2012) Spring JDBC Template? Say, I have a stored procedure that declares both IN and OUT parameters, something like this: mypkg.doSomething( id OUT int, name IN…
adarshr
  • 61,315
  • 23
  • 138
  • 167
76
votes
6 answers

How to execute INSERT statement using JdbcTemplate class from Spring Framework

In Spring, how can I insert data in table using JdbcTemplate. Can anyone please provide me a code sample for doing this.
user469999
  • 2,101
  • 4
  • 24
  • 30
63
votes
4 answers

identity from sql insert via jdbctemplate

Is it possible to get the @@identity from the SQL insert on a Spring jdbc template call? If so, how?
javamonkey79
  • 17,443
  • 36
  • 114
  • 172
54
votes
14 answers

Spring Boot: Jdbc javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify

I am currently learning more about implementing JDBC and using databases in a Spring Boot webapp, and I encountered the following Stack Trace written in the bottom of the post. I have created a simple Employee model, and I am trying to execute some…
DP Park
  • 815
  • 1
  • 9
  • 19
52
votes
4 answers

what is difference between ResultSetExtractor vs Rowmapper?

I worked on both row mapper and resultset extractor call back interfaces.I found difference i.e., 1.Row mapper can be processing per row basis.But Resultset extractor we can naviagte all rows and return type is object. Is there any difference other…
user1127214
  • 3,109
  • 7
  • 26
  • 30
48
votes
2 answers

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…
Ashok
  • 587
  • 1
  • 4
  • 5
47
votes
4 answers

How to get Map data using JDBCTemplate.queryForMap

How to load data from JDBCTemplate.queryForMap() as it returns the Map Interface? How is the query data maintained internally in the map? I tried to load it, but I got this exception: org.springframework.dao.IncorrectResultSizeDataAccessException:…
user1127214
  • 3,109
  • 7
  • 26
  • 30
46
votes
4 answers

How to query for a List in JdbcTemplate?

I'm using Spring's JdbcTemplate and running a query like this: SELECT COLNAME FROM TABLEA GROUP BY COLNAME There are no named parameters being passed, however, column name, COLNAME, will be passed by the user. Questions Is there a way to have…
birdy
  • 9,286
  • 24
  • 107
  • 171
46
votes
4 answers

Spring JDBCTemplate VS Hibernate in terms of performance

In our project we have to decide between Spring JDBCTemplate and Hibernate. I want to know which is better in terms of performance and implementation and design. and how?
Bosco
  • 3,835
  • 6
  • 25
  • 33
44
votes
9 answers

How to set up datasource with Spring for HikariCP?

Hi I'm trying to use HikariCP with Spring for connection pool. I'm using jdbcTempLate and JdbcdaoSupport. This is my spring configuration file for datasource:
Abhinab Kanrar
  • 1,532
  • 2
  • 20
  • 46
42
votes
5 answers

Using prepared statements with JDBCTemplate

I'm using the JDBC template and want to read from a database using prepared statements. I iterate over many lines in a .csv file, and on every line I execute some SQL select queries with corresponding values. I want to speed up my reading from the…
user321068
1
2 3
99 100