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
39
votes
5 answers

Best practice to select data using Spring JdbcTemplate

I want to know what is the best practice to select records from a table. I mentioned two methods below from that I want to know which one is best practice to select the data from a table using Spring JdbcTemplate. First example try { String sql…
Annamalai Thangaraj
  • 522
  • 1
  • 5
  • 10
39
votes
4 answers

using Spring JdbcTemplate - injecting datasource vs jdbcTemplate

As per Spring documentation, the steps to use Spring JdbcTemplate is as follows:
SGB
  • 2,118
  • 6
  • 28
  • 35
38
votes
2 answers

Multiple DataSource and JdbcTemplate in Spring Boot (> 1.1.0)

I would like to inject a specific JdbcTemplatein a Spring Boot project. I tried to follow this example for multiple DataSourceconfiguration : http://spring.io/blog/2014/05/27/spring-boot-1-1-0-m2-available-now My code does compile and run, but only…
Xavier
  • 423
  • 1
  • 4
  • 8
37
votes
1 answer

Does Spring JDBC provide any protection from SQL injection attacks?

Spring's JdbcTemplate abstraction provides a lot of functionality, but can it be used in such a way that provides protection from SQL injection attacks? For example, like the kind of protection you would get using PreparedStatement with properly…
brabster
  • 42,504
  • 27
  • 146
  • 186
37
votes
9 answers

Why Spring's jdbcTemplate.batchUpdate() so slow?

I'm trying to find the faster way to do batch insert. I tried to insert several batches with jdbcTemplate.update(String sql), where sql was builded by StringBuilder and looks like: INSERT INTO TABLE(x, y, i) VALUES(1,2,3), (1,2,3), ... ,…
user2602807
  • 1,232
  • 2
  • 15
  • 28
30
votes
5 answers

Using Spring JdbcTemplate to extract one string

Can't seem to find a way to get one string from table using JdbcTemplate query. This is the table my sql returns: ID | STREET_NAME ------------------------ 1 | Elm street Now how am I supposed to get the value of STREET_NAME. SQL always returns…
lkallas
  • 1,310
  • 5
  • 22
  • 36
30
votes
4 answers

Spring's JdbcTemplate and Transactions

When using JdbcTemplate, do I need to explicitly configure transactions? My code layout looks like the following: I will have a UserDao that will be injected into my UserService, and then my Controllers will make calls on methods in my…
loyalflow
  • 14,275
  • 27
  • 107
  • 168
29
votes
4 answers

Does Spring's JdbcTemplate close the connection after query timeout?

I have set query timeout (getJdbcTemplate().setQueryTimeout(5)) in method with insert statement. What will happen after query timeout, does jdbc template close my connection?
user3073662
  • 293
  • 1
  • 3
  • 4
28
votes
5 answers

Execute SQL file from Spring JDBC Template

I'm trying to write a bit of code that reads a SQL file (multiple CREATE TABLE statements separated by ;) and executes all the statements. In pure JDBC, I could write: String sqlQuery = "CREATE TABLE A (...); CREATE TABLE B…
Ondrej Skalicka
  • 3,046
  • 9
  • 32
  • 53
28
votes
1 answer

passing different types of arguments to jdbctemplate query

I am trying to retrieve records from the database by using where clause with few different types of arguments. This is the simple method which I wrote where I am passing breedId and gender as an arguments. public List
Suleman khan
  • 1,038
  • 4
  • 14
  • 34
27
votes
5 answers

Inserting multiple rows using JdbcTemplate

How can I execute the following SQL in a scalable way using JdbcTemplate running on mySQL. In this case, scalable means: Only one SQL statement is executed on the server it works for any number of rows. Here's the statement: INSERT INTO myTable…
Edward Dale
  • 29,597
  • 13
  • 90
  • 129
26
votes
2 answers

jdbctemplate count queryForInt and pass multiple parameters

How can I pass multiple parameters in jdbcTemplate queryForInt to get the count. I have tried this, Integer count = this.jdbcTemplate .queryForInt("select count(name) from table_name where parameter1 = ? and parameter2 = ?", new…
Shijin Bhaskar
  • 381
  • 1
  • 4
  • 9
26
votes
2 answers

JdbcTemplate queryForList return value in case of no results

The question is pretty much summed up in the title. What will JdbcTemplate.queryForList() return when the query returns no results. Will it return an empty List or null value? I couldn't find a definitive answer from the documentation. Thanks in…
Reins
  • 1,109
  • 1
  • 17
  • 35
25
votes
5 answers

Return Type for jdbcTemplate.queryForList(sql, object, classType)

I'm executing a named query using jdbcTemplate.queryForList in the following manner: List conversations = jdbcTemplate.queryForList( SELECT_ALL_CONVERSATIONS_SQL_FULL, new Object[] {userId, dateFrom,…
Global Dictator
  • 1,539
  • 9
  • 24
  • 37
25
votes
2 answers

JdbcTemplate "queryForObject" and "query" is deprecated in Spring. What should it be replaced by?

Query for object, Student student = return jdbcTemplate.queryForObject("select * from student_id = ?", new Object[] { studentId }, studentRowMapper); For query, List students = return jdbcTemplate.query("select * from class_room_id = ?",…
Thirumal
  • 8,280
  • 11
  • 53
  • 103