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
0
votes
2 answers

Simple jdbcTemplate.query() is supposed to return a list, but isn't

I'm using spring 4.0 (boot), and here is the code: String sql = "SELECT emp_id,name,role FROM employees"; List employees = jdbcTemplate.query(sql,(rs, rowNum)-> new Employee(rs.getInt("emp_id"),…
DP Park
  • 815
  • 1
  • 9
  • 19
0
votes
1 answer

EasyMock object throwing unexpected method call when method call is expected exactly as it executes

I'm running a JUnit test on a function returning a list of results for me and I'm trying to mock the database call being done with an autowired JdbcTemplate object. For the privacy of my company, I'm replacing all the fields and databases in my…
Jon Sansoucie
  • 521
  • 4
  • 18
0
votes
1 answer

How JDBCTemplate queryForObject works. Specifically, the new Object[] {} part

I have this piece of example code: public Post findById(Long id) { String sql = "select id, title, text from post where id = ?"; return template.queryForObject(sql, new Object[] {id}, getPostRowMapper()); } I don't understand what the new…
user3207874
  • 2,815
  • 2
  • 13
  • 18
0
votes
0 answers

How to switch database source repository during running application in Spring Boot

The requirement is - In my application, having more than 5 databases, those are source and target databases. through UI page, User have to choose source database and target database and based on this data we have to fetch records from one database…
0
votes
1 answer

Connect to a database using xml file - java

public static JdbcTemplate connectBDD() { DriverManagerDataSource ds = new DriverManagerDataSource(); ds.setDriverClassName("com.mysql.jdbc.Driver"); ds.setUrl("jdbc:mysql://localhost:8080/test"); ds.setUsername("root"); …
0
votes
1 answer

Parameter index out of range (1 > number of parameters, which is 0) in JDBCTemplate while retrieving count from with queryForObject() method

I am trying to get count of rows with jdbctemplate.queryForObject(query,Object[]{},Integer.class) method , i have defined query in mysql.properties file and passed a parameter to object[] but still getting exception, please help mysql.properties…
shreeramaute
  • 362
  • 3
  • 14
0
votes
1 answer

Using Integer Array in postgres with Spring-boot

I am attempting to accept from the browser a List and use this within a SQL query to a postgres database. I have the following code snippet that tries to show the function that I have made todo this. Some of the variables have been changed in case…
Whinis
  • 3
  • 5
0
votes
0 answers

Spring Boot: How to make single externalize JDBC datasource configuration work in differnt DAOImpl classes

I have a requirement to fetch DB username and password from Vault. So I have removed the default implementation (spring.datasource.url,spring.datasource.username,spring.datasource.password) and added the following code in DAOImpl class. Code …
0
votes
1 answer

Binding parameters for inserting constants in query?

Using bound parameters (using ?) in Prepared Statement is a popular strategy for inserting parameters in SQL queries at runtime. My question is, should this strategy be used for inserting constants into queries? I am talking about constants that are…
lebowski
  • 1,031
  • 2
  • 20
  • 37
0
votes
1 answer

Not all variable bound Exception Using JDBC template

I am using jbcTemplate and I am encountered ORA-01008 exception. package com.awzpact.prayas.service; import com.awzpact.uam.dao.BaseJdbcTemplate; import java.util.Iterator; import java.util.List; import java.util.Map; import…
user9634982
  • 565
  • 5
  • 24
0
votes
0 answers

Merge 2 Resultsets using JDBCTemplate

I'm modifying a stand alone Spring 3.0.6 application that uses JDBCTemplate. The main problem is that I have to extract information from two different Oracle databases for which I created 2 Datasources: Added the 2 datasources in an…
0
votes
1 answer

Kotlin to SAP HANA char set

I have Spring boot Kotlin application running on Tomcat 8.5 on JVM 1.8 and connecting to SAP HANA. I use JdbcTemplate.batchUpdate() for inserting data into tables. I have to deal with German alphabet and that causes some troubles. When…
Lukas Forst
  • 802
  • 1
  • 8
  • 25
0
votes
1 answer

Spring JDBCTemplate. Null pointer exception

I'm trying to set up a SpringMVC website from scratch, but I've hit a dead end. I'm using autowiring to instanciate JdbcTemplate with a DataSource, but somehow I'm getting a Null pointer exception. I'd appreciate your help with this. My AppConfig is…
IloneSP
  • 429
  • 2
  • 13
0
votes
0 answers

Spring jdbctemplate working on tomcat but gives error on WAS server

I am trying to connect to db2 using spring jdbc template. Everything is working fine when deployed on tomcat server. But the same application when I am trying to deploy on WAS server is not working and giving me the below…
zakib
  • 11
  • 3
0
votes
1 answer

ORA-22922: nonexistent LOB value while passing CLOB as part of a UDT Array

I am trying to pass an User defined type (UDT) as an input parameter to an Oracle Stored Procedure The UDT array - FILTER_EXPR_TBL: CREATE OR REPLACE TYPE schema.FILTER_EXPR_TBL AS TABLE OF schema.FILTER_EXPR_T The UDT member -…
1 2 3
99
100