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
25
votes
4 answers

IncorrectResultSetColumnCountException: Incorrect column count: expected 1, actual 38

I am using JdbcTemplate to retrieve a Bean from the db. Here is my method: public List getTrackerData() { return List live = (List) jdbcTemplate.queryForList("select * from…
user4894180
25
votes
4 answers

How to search string LIKE 'something%' with Java Spring Framework?

I've got a MySQL table with Foos. Each Foo has a numeric non-unique code and a name. Now I need to find if any Foo with one of certain codes happens to have a name that starts with a given string. In normal SQL this would be trivial: select * from…
ZeroOne
  • 3,041
  • 3
  • 31
  • 52
24
votes
4 answers

Multiple JdbcTemplate instances or not?

From what I understand, both DataSource and JdbcTemplates are threadsafe, so you can configure a single instance of a JdbcTemplate and then safely inject this shared reference into multiple DAOs (or repositories). Also DataSourceshould be a Spring…
stivlo
  • 83,644
  • 31
  • 142
  • 199
24
votes
6 answers

JDBCTemplate set nested POJO with BeanPropertyRowMapper

Given the following example POJO's: (Assume Getters and Setters for all properties) class User { String user_name; String display_name; } class Message { String title; String question; User user; } One can easily query a…
nclord
  • 1,287
  • 1
  • 16
  • 17
24
votes
3 answers

Using a prepared statement and variable bind Order By in Java with JDBC driver

I'm using jdbcTemplate to make JDBC connections to a mySQL DB prepared statements to protect myself as much as possible from SQL injection attacks in need to accept requests from the user to sort the data on any of a dozen different columns the…
Raevik
  • 1,945
  • 9
  • 32
  • 53
23
votes
3 answers

java.sql.SQLException: Invalid column name

I cannot figure out why I am getting "Invalid column name" here. We have tried a variant of the sql directly in Oracle, and it works fine, but when I try it using jdbcTemplate then something is wrong. List alleXmler =…
Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143
22
votes
3 answers

keyHolder.getKey() return null

Why code from "Spring in action 5" don't work (keyHolder.getKey() return null, but entity is saved in DB)? private long savePizzaInfo(Pizza pizza) { pizza.setCreatedAt(new Date()); PreparedStatementCreator psc = new…
lobs
  • 429
  • 5
  • 14
21
votes
4 answers

Return a list, I already have a rowmapper implementation

In my UserDao I want to return a list of users. I already have a UserRowMapper that implements RowMapper. How can I do this? I tried: List rows = getJdbcTemplate().queryforList("select * from users"); for(Map row : rows) { } But wasn't sure…
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
21
votes
5 answers

Roll back A if B goes wrong. spring boot, jdbctemplate

I have a method, 'databaseChanges', which call 2 operations: A, B in iterative way. 'A' first, 'B' last. 'A' & 'B' can be Create, Update Delete functionalities in my persistent storage, Oracle Database 11g. Let's say, 'A' update a record in table…
lolo
  • 17,392
  • 9
  • 25
  • 49
20
votes
10 answers

Spring JdbcTemplate - Insert blob and return generated key

From the Spring JDBC documentation, I know how to insert a blob using JdbcTemplate final File blobIn = new File("spring2004.jpg"); final InputStream blobIs = new FileInputStream(blobIn); jdbcTemplate.execute( "INSERT INTO lob_table (id, a_blob)…
itsadok
  • 28,822
  • 30
  • 126
  • 171
20
votes
3 answers

What are template classes in Spring Java? Why are they called templates? For example jdbc-template, jms-template etc

I'm new to Java. I've only been programming it for about a year. What does Spring mean by the use of templates? In Spring, there is jdbc-templates, jms-templates etc.. What are template classes in java? Are they a special kind of design pattern or…
Horse Voice
  • 8,138
  • 15
  • 69
  • 120
20
votes
4 answers

How can I cancel a long-running query using Spring and JDBCTemplate?

The JDBC java.sql.Statement class has a cancel() method. This can be called in another thread to cancel a currently running statement. How can I achieve this using Spring? I can't find a way to get a reference to a statement when running a query.…
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
19
votes
2 answers

JdbcTemplate and SimpleJdbcTemplate

I am new to Spring 3.0 . For DAO access i have choosen SpringJDBC. SpringJDBC provides JDBC Template and SimpleJDBCTemplate . Which one is best. I read in some of the forum SimpleJDBCTemplate going to be deprecated in Spring 3.1. What is the…
Mohan
  • 3,893
  • 9
  • 33
  • 42
18
votes
2 answers

Can I set a JDBC timeout for a single query?

I have a web app on Tomcat, which handles DB connection pooling, and using Spring JDBCTemplate for executing queries. It's been requested that I implement a status page which will be monitored by a heartbeat process to determine if everything is…
Shawn D.
  • 7,895
  • 8
  • 35
  • 47
18
votes
4 answers

How to implement RowMapper using java lambda expression

I had a working RowMapper code that maps database table row in to a java object. I wanted to change the implementation using lambda expression. However, I am always getting error; Code snippet as follows; String queryString = "select * from person…
nwGCham
  • 313
  • 1
  • 3
  • 16