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
13
votes
1 answer

how to bind a list of tuples using Spring JDBCTemplate?

I have some queries like this: List listOfIntegers = Arrays.asList(new Integer[] {1, 2, 3}); List objects = namedParameterJdbcTemplate.query("select * from bla where id in ( :ids )", Collections.singletonMap("ids", listOfIntegers), …
12
votes
2 answers

Does Spring's JdbcTemplate close the connection if an exception is thrown?

When Spring catches an SQLException, does it close the prepared statement, result set, and/or connection before throwing it's own DataAccessException (runtime) exception? I have a developer who wants to create an AOP aspect to catch these exceptions…
aakoch
  • 1,164
  • 1
  • 9
  • 18
12
votes
2 answers

Spring JDBC: Returning 0 or 1 rows

I tried to google this question but could not find: Is there a recommended method in Spring jdbcTemplate which should be used when we expect 0 or 1 rows to be returned. queryForObject() will throw exception when no rows returned. queryForList() will…
JavaTec
  • 965
  • 3
  • 15
  • 30
12
votes
2 answers

Java Spring - RowMapper ResultSet - Integer/null values

I have a Java SE 8 Spring 4.1.6-RELEASE application, where I am implementing the org.springframework.jdbc.core.RowMapper interface, and I had some questions about the java.sql.ResultSet interface that is passed in its T mapRow(ResultSet rs, int…
anonymous
  • 657
  • 1
  • 8
  • 21
12
votes
7 answers

SQL state [99999]; error code [17004]; Invalid column type: 1111 With Spring SimpleJdbcCall

Hi All I am using spring simple JDBC template to call the oracle procedure the below are my code. The procedure create or replace PROCEDURE get_all_system_users( pi_client_code IN VARCHAR2, po_system_users OUT T_SYSTEM_USER_TAB, po_error_code …
Krushna
  • 5,059
  • 5
  • 32
  • 49
12
votes
3 answers

What is proper way to use PreparedStatementCreator of Spring JDBC?

As per my understanding the use of PreparedStatement in Java is we can use it multiple times. But I have some confusion using PreparedStatementCreator of Spring JDBC. For example consider following code, public class SpringTest { JdbcTemplate…
Jignesh Dhua
  • 1,471
  • 1
  • 14
  • 31
11
votes
1 answer

Spring (MVC) SQL injection avoidance?

I am wondering how Spring MVC handles SQL injections (and other security issues: XSS, code [javascript] injection, etc). I'm talking mostly about escaping the values that are added to DBs and such. I can't seem to find any answer because every time…
BogdanSorlea
  • 462
  • 2
  • 5
  • 15
11
votes
2 answers

Spring @Cacheable methods with lists

I'm using latest Ehcache in my Spring 4.1.4 application. What I have is: class Contact{ int id; int revision; } @Cacheable("contacts") public List getContactList(List contactIdList) { return…
JavaDesire
  • 119
  • 1
  • 4
11
votes
2 answers

Instantiating a JdbcTemplate from a java.sql.Connection

I want to obtain a JdbcTemplate in my Java code. I've already got a working java.sql.Connection. To create a new JdbcTemplate it would normally need an instance of the javax.sql.DataSource interface. Is it somehow possible to obtain a new…
user321068
11
votes
5 answers

Using Spring's KeyHolder with programmatically-generated primary keys

I am using Spring's NamedParameterJdbcTemplate to perform an insert into a table. The table uses a NEXTVAL on a sequence to obtain the primary key. I then want this generated ID to be passed back to me. I am using Spring's KeyHolder…
sma
  • 9,449
  • 8
  • 51
  • 80
11
votes
5 answers

Why does Spring's JDBC Templates doesn't use the tables default value

I have a table in MYSQL and I am using JDBC Templates to do an insert into that table. One of the columns has a default value, and I am not specifying it in the Map parameters map. I am getting an exception Column 'colName' cannot be…
Gleeb
  • 10,773
  • 26
  • 92
  • 135
11
votes
3 answers

What does JdbcTemplate do when RowMapper returns null?

I am using the JdbcTemplate.query(sql, args, rowMapper) method call to return a list of objects. There are some cases where I want to skip a row and not add it to the list that I return. In these cases, I've thought of two solutions: Have RowMapper…
ktm5124
  • 11,861
  • 21
  • 74
  • 119
11
votes
1 answer

Accessing grails/hibernate generated SQL for domain class

In my grails (1.3.7) application, I am using JDBC Template for bulk import of 1000s of records from a CSV file (since it's much faster than using vanilla GORM/hibernate, as you would expect). e.g. class Book { String title } and // For each CSV…
Jon Burgess
  • 2,035
  • 2
  • 17
  • 27
10
votes
5 answers

How to use SELECT IN clause in JDBCTemplates?

This is my first experience with JDBCTemplates and I ran into a case where I need to use a query that looks like this: SELECT * FROM table WHERE field IN (?) How do I do that? I already tried passing a list/array value but that didn't do the trick,…
Chepech
  • 5,258
  • 4
  • 47
  • 70
10
votes
2 answers

JdbcTemplate and inet data type

One of my table has the column type as inet. And when I try to perform insert operation using String as the type for inet column, it's saying "column "ip" is of type inet but expression is of type character varying:", which is perfectly valid…
Amit Goyal
  • 454
  • 4
  • 10