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

Why is my jdbcTemplate query returning no rows?

I have an API built with spring-boot. The request sends JSON to the end point which build a request object. The request object sends its list of Product objects to a method to update the productDimensions object within the Product. In order to get…
boo
  • 129
  • 1
  • 4
  • 12
0
votes
0 answers

Error using postgres JSONB query with jdbctemplate

PostgresSql Query SELECT json_data FROM employee where json_data -> 'employee' @> '{"name":"Aman"}' This query works fine when run in postgres. But when run with jdbctemplate, it throws an error. Java Code String sql="SELECT json_data FROM employee…
0
votes
1 answer

How to process db select result partially in JAVA

in my case i have, lets say 500k of select result. What i would like to do in JAVA code is to select this result partially, about 1k of result at once, without selectint whole list of results. Then use each 1k part of result to do batch insert. The…
snowflake
  • 77
  • 1
  • 8
0
votes
1 answer

Springboot jdbc/jpa

I have a query which needs to be executed. There were tons of examples of rowmapper all over so I took some brief idea from there. So I have a huge query which has joins of multiple tables and it extracts some data. Depending on the data it…
Anant Majhi
  • 45
  • 1
  • 6
0
votes
0 answers

Spring Boot Oracle Rest JdbcTemplate Gradle app with in-memory H2 for unit tests

I'm building a Spring Boot REST app for an existing Oracle database, and due to multiple factors, am constrained to using JdbcTemplate instead of JPA. I'm trying to use an in-memory H2 database for the unit tests, but am having difficulty with its…
ktwd
  • 77
  • 1
  • 9
0
votes
2 answers

Return different type of object based on condition using Java

I've three queries which is SELECT NAME FROM EMP WHERE EMPID=100; // THIS RETURNS A STRING SELECT DEPTID FROM EMP WHERE EMPID=100; // THIS RETURNS AN INT SELECT EMPID FROM DEPT WHERE DEPTID=101; // THIS RETURNS A LIST Now I'm trying to write a…
Syed
  • 2,471
  • 10
  • 49
  • 89
0
votes
0 answers

How to fix 'com.mchange.v2.resourcepool.TimeoutException' when using C3P0

I used c3p0-0.9.1.1.jar. The Database is Vertica. There are up to 12 concurrent threads. So i set minPoolSize to 12, maxPoolSize to 24, and initialPoolSize to 12. I have a connection problem. Mosttime time it works fine, but sometime it does not.…
Ryan Yin
  • 1
  • 2
0
votes
0 answers

How to print mysql error code number if exception occured, While using Jdbctemplate in spring core java

I am using JdbcTemplate and trying to create a table if not exist by checking MySQL error code 1146 But I am not able to check the error code.
Vikalp
  • 71
  • 1
  • 10
0
votes
0 answers

Why getJdbcTemplate().query returns null list after upgrading mySql?

I have recently upgraded from Java 6 to 8, mySql 5.5 to 5.7 and Spring MVC 2 to 5.0.9. I have following code, which worked before upgrade. But now if i try it returns list of Map with one entry, but Map is blank. But the truth is, database has one…
Aakash Patel
  • 549
  • 5
  • 19
0
votes
0 answers

JDBC template Batch exception handling

I am using Spring JDBC template to insert batches to MySql DB. Since I use &rewriteBatchedStatements=true, If I have a primary key violation it will fail the entire batch. The problem is I cannot know which record is the evil one who caused the…
Ido Barash
  • 4,856
  • 11
  • 41
  • 78
0
votes
1 answer

Throttling JDBCTemplate Logs

I am using Spring JDBCTemplate for executing SQL queries. When the log level org.springframework.jdbc.core.JdbcTemplate is set to DEBUG, the SQL statement is logged each time the query is fired. The problem is that I have some queries that get fired…
Soham
  • 126
  • 1
  • 6
0
votes
0 answers

Meeting "java.sql.SQLException: Invalid column index" when using jdbcTemplate to execute a SQL statement without argument.

I have code like following: public class InspectDaoImpl extends JdbcDaoSupport{ ... public ShiftTime getShiftTime(){ String sql = ""; ...//Only for assigning value to sql, it will be seen in exception stack trace. …
萝莉w
  • 177
  • 11
0
votes
1 answer

Spring MVC project architecture doubts - DAO, Models, Services

I am struggling with finding best practices for architecture of my Spring project. Currently, our company has large database schema which is used across many projects, and we are using only a subset, more precisely 10 tables. Our application is…
0
votes
2 answers

Not able to insert large string in Sybase DB

I am trying to store large(more than 20000 characters) html string in the Sybase DB in TEXT datatype but getting two problems. When I try to insert manually, only 8192 characters are inserted. When I try through jdbctemplate, I get error as "error…
Newinjava
  • 972
  • 1
  • 12
  • 19
0
votes
0 answers

Reading SQL query from external file in DAO

In my spring boot application I have my query in an external file (sql.properties) and reading it in DAO @Value("${queries.selectValue}") private String selectQuery; Is this the right way ? Any security issues here
jramapurath
  • 221
  • 1
  • 8