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

Getting "Invalid column type" exception, while using NamedParameterJDBCTemplate for insertion

I am using below code while inserting a row into database(oracle 10g xe,jar: ojdbc14.jar) String sql = "INSERT INTO SPONSOR_TB(ID,NAME,INDUSTRY_TYPE,IS_REPORTING_SPONSOR,IS_NOT_SOLICITE)…
Ajeet Singh
  • 717
  • 2
  • 6
  • 13
9
votes
2 answers

How to cast resultset in rowmapper to an enum?

I have my rowmapper like: private static final class UserRowMapper implements RowMapper { User user = new User(); user.setId(rs.getInt("id")); user.setUserType( (UserType)rs.getInt("userType")); // ????? return…
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
9
votes
5 answers

Spring jdbcTemplate dynamic where clause

Is it possible to generate arbitrary where conditions SQL query through JDBC template? example: If I pass value for 1 parameter (only name) : search by name "select * from address where shopname = ?"; If I pass value for 2 parameter (name and city)…
minil
  • 6,895
  • 16
  • 48
  • 55
9
votes
3 answers

Spring JdbcTemplate - how to prepend every query for achieving multitenancy?

Setup I have an app using Spring 4.3, JdbcTemplate, Hibernate 5 and MySQL 8. I have implemented multitenancy in hibernate per schema where i switch schemas using using hibernates multitenancy mechanism - MultiTenantConnectionProvider and doing there…
Robert Niestroj
  • 15,299
  • 14
  • 76
  • 119
9
votes
4 answers

JDBCTemplate find if row exists

I am curious as to how I should use springs jdbctemplate class to determine if a record or row exists already in one of my tables?? I have tried int count = jdbcTemplate.queryForObject("select * from MyTable where…
j-money
  • 509
  • 2
  • 9
  • 32
9
votes
2 answers

How to pass list of values as a parameter to IN clause using jdbc template

I want to pass the car names as a bind variable (changes at runtimme) How to achieve that . Java Version 1.7 private JdbcTemplate jdbcTemplate; public Collection findAll(){ String sql = "SELECT NAME, YEAR, TYPE FROM CARS where NAME in…
Jan69
  • 1,109
  • 5
  • 25
  • 48
9
votes
2 answers

How to handle jdbc.queryForObject if it doesn't return a row

I would like to know how to properly use jdbc in my case. The saveLinkHistory column is a bit(1) type in mysql. public boolean getIsSavedLinkHistory(String name) { String sql = "select saveLinkHistory from users where name = ?"; …
Oleg
  • 1,479
  • 3
  • 21
  • 42
9
votes
2 answers

how to pass list parameter in IN clause using jdbcTemplate

I want to pass list values in IN clause using jdbcTemplate in mysql query. Like below, List listId= new ArrayList<>(); listId.add(1234L); listId.add(1235L); listId.add(1236L); String type ="A"; List result = new…
MMMMS
  • 2,179
  • 9
  • 43
  • 83
9
votes
2 answers

How exactly work the Spring RowMapper interface?

I am studying for the Spring Core certification and I have some doubts about how Spring handle the JDBC queries: So I know that I can obtain data from my DB tables in various ways depending on the type of data that I expect to obtain: 1) Query for…
user4353372
9
votes
3 answers

Use logback to log SQL parameters in Spring JdbcTemplate

I'm using Logback with Spring JdbcTemplate to log my SQL queries. My configurations contains next line: But this logs only query with wildcards ? without list of…
Maxim Kolesnikov
  • 5,075
  • 6
  • 38
  • 68
9
votes
4 answers

Method may fail to clean up stream or resource on checked exception -- FindBugs

I am using Spring JDBCTemplate to access data in database and its working fine. But FindBugs is pointing me a Minor issue in my code snippet. CODE: public String createUser(final User user) { try { final String insertQuery =…
Kiran
  • 20,167
  • 11
  • 67
  • 99
9
votes
4 answers

JdbcTemplate query close database connection

I use jpa with hibernate. I have following method: @Transactional public void myMethod(){ ... firstJDBCTemplateQuery(); secondJDBCTemplateQuery(); ... } firstJDBCTemplateQuery works, but it closes connection to database. When second…
Mariusz
  • 1,907
  • 3
  • 24
  • 39
9
votes
3 answers

Is there a way to intelligently set fetch size on JdbcTemplate?

I have query that return reports by a date range. If the date range is large, 50k rows may be returned. If the date range is very small, 10 records could be returned. I've found that setting the fetch size to 1000, when 50k rows are returned…
stilltrackin
  • 111
  • 1
  • 1
  • 7
9
votes
1 answer

Spring NamedParameterJDBCTemplate reuse of Prepared Statements

I am using the Spring NamedParameterJdbcTemplate to fetch some values from a table. For some reason, the query runs very slow in my Java app as opposed to running the same query on SQL Management Studio. I also noticed in the profiler, the prepared…
user320587
  • 1,347
  • 7
  • 29
  • 57
9
votes
2 answers

Bind parameters for ORDER BY in NamedParameterJDBCTemplate

I am trying to use NamedParameterJdbTemplate in a Spring MVC application. The issue is that the bind parameters do not seem to work (no sorting happens) when I include one of ORDER BY clauses listed below. However, a hard coded order by column name…
aprajitha
  • 345
  • 1
  • 7
  • 18