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.

Filter by
Sorted by
Tagged with
194 votes
5 answers
223k views

How to execute IN() SQL queries with Spring's JDBCTemplate effectively?

I was wondering if there is a more elegant way to do IN() queries with Spring's JDBCTemplate. Currently I do something like that: StringBuilder jobTypeInClauseBuilder = new StringBuilder(); for(int i ...
Malax's user avatar
  • 9,516
131 votes
18 answers
332k views

Jdbctemplate query for string: EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0

I am using Jdbctemplate to retrieve a single String value from the db. Here is my method. public String test() { String cert=null; String sql = "select ID_NMB_SRZ from ...
Byron's user avatar
  • 3,873
108 votes
5 answers
107k views

JPA vs Spring JdbcTemplate [closed]

For a new project is JPA always the recommended tool for handling relational data or are there scenarios where Spring JdbcTemplate is a better choice? Some factors to consider in your response: new ...
aem999's user avatar
  • 17k
108 votes
6 answers
108k views

JdbcTemplate queryForInt/Long is deprecated in Spring 3.2.2. What should it be replaced by?

The queryforInt/queryforLong methods in JdbcTemplate are deprecated in Spring 3.2. I can't find out why or what is considered the best practice to replace existing code using these methods. A typical ...
Dan MacBean's user avatar
  • 1,915
93 votes
7 answers
317k views

Spring JDBC Template for calling Stored Procedures

What is the correct way to invoke stored procedures using modern day (circa 2012) Spring JDBC Template? Say, I have a stored procedure that declares both IN and OUT parameters, something like this: ...
adarshr's user avatar
  • 62.1k
82 votes
6 answers
206k views

How to execute INSERT statement using JdbcTemplate class from Spring Framework

In Spring, how can I insert data in table using JdbcTemplate. Can anyone please provide me a code sample for doing this.
user469999's user avatar
  • 2,161
63 votes
4 answers
69k views

identity from sql insert via jdbctemplate

Is it possible to get the @@identity from the SQL insert on a Spring jdbc template call? If so, how?
javamonkey79's user avatar
  • 17.6k
55 votes
14 answers
124k views

Spring Boot: Jdbc javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify

I am currently learning more about implementing JDBC and using databases in a Spring Boot webapp, and I encountered the following Stack Trace written in the bottom of the post. I have created a ...
DP Park's user avatar
  • 825
53 votes
4 answers
77k views

what is difference between ResultSetExtractor vs Rowmapper?

I worked on both row mapper and resultset extractor call back interfaces.I found difference i.e., 1.Row mapper can be processing per row basis.But Resultset extractor we can naviagte all rows and ...
user1127214's user avatar
  • 3,139
51 votes
2 answers
68k views

NamedParameterJdbcTemplate vs JdbcTemplate

I'm a beginner to Spring3.x , I'm learning Spring DAO support. I want to know the difference between NamedParameterJdbcTemplate and JdbcTemplate. Which one is the best by means of performance. And ...
Ashok's user avatar
  • 617
49 votes
4 answers
161k views

How to query for a List<String> in JdbcTemplate?

I'm using Spring's JdbcTemplate and running a query like this: SELECT COLNAME FROM TABLEA GROUP BY COLNAME There are no named parameters being passed, however, column name, COLNAME, will be passed by ...
birdy's user avatar
  • 9,486
47 votes
4 answers
178k views

How to get Map data using JDBCTemplate.queryForMap

How to load data from JDBCTemplate.queryForMap() as it returns the Map Interface? How is the query data maintained internally in the map? I tried to load it, but I got this exception: org....
user1127214's user avatar
  • 3,139
47 votes
4 answers
71k views

Spring JDBCTemplate VS Hibernate in terms of performance [closed]

In our project we have to decide between Spring JDBCTemplate and Hibernate. I want to know which is better in terms of performance and implementation and design. and how?
Bosco's user avatar
  • 3,975
44 votes
9 answers
186k views

How to set up datasource with Spring for HikariCP?

Hi I'm trying to use HikariCP with Spring for connection pool. I'm using jdbcTempLate and JdbcdaoSupport. This is my spring configuration file for datasource: <bean id="dataSource" class="com....
Abhinab Kanrar's user avatar
42 votes
5 answers
187k views

Using prepared statements with JDBCTemplate

I'm using the JDBC template and want to read from a database using prepared statements. I iterate over many lines in a .csv file, and on every line I execute some SQL select queries with corresponding ...
user avatar
40 votes
4 answers
102k views

using Spring JdbcTemplate - injecting datasource vs jdbcTemplate

As per Spring documentation, the steps to use Spring JdbcTemplate is as follows: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" ...
SGB's user avatar
  • 2,190
40 votes
6 answers
132k views

Best practice to select data using Spring JdbcTemplate

I want to know what is the best practice to select records from a table. I mentioned two methods below from that I want to know which one is best practice to select the data from a table using Spring ...
Annamalai Thangaraj's user avatar
39 votes
9 answers
98k views

Why Spring's jdbcTemplate.batchUpdate() so slow?

I'm trying to find the faster way to do batch insert. I tried to insert several batches with jdbcTemplate.update(String sql), where sql was builded by StringBuilder and looks like: INSERT INTO ...
user2602807's user avatar
  • 1,272
38 votes
2 answers
44k views

Multiple DataSource and JdbcTemplate in Spring Boot (> 1.1.0)

I would like to inject a specific JdbcTemplatein a Spring Boot project. I tried to follow this example for multiple DataSourceconfiguration : http://spring.io/blog/2014/05/27/spring-boot-1-1-0-m2-...
Xavier's user avatar
  • 423
38 votes
1 answer
44k views

Does Spring JDBC provide any protection from SQL injection attacks?

Spring's JdbcTemplate abstraction provides a lot of functionality, but can it be used in such a way that provides protection from SQL injection attacks? For example, like the kind of protection you ...
brabster's user avatar
  • 43.1k
34 votes
5 answers
121k views

Using Spring JdbcTemplate to extract one string

Can't seem to find a way to get one string from table using JdbcTemplate query. This is the table my sql returns: ID | STREET_NAME ------------------------ 1 | Elm street Now how am I supposed to ...
lkallas's user avatar
  • 1,376
33 votes
4 answers
89k views

Spring's JdbcTemplate and Transactions

When using JdbcTemplate, do I need to explicitly configure transactions? My code layout looks like the following: I will have a UserDao that will be injected into my UserService, and then my ...
loyalflow's user avatar
  • 14.6k
30 votes
5 answers
46k views

Execute SQL file from Spring JDBC Template

I'm trying to write a bit of code that reads a SQL file (multiple CREATE TABLE statements separated by ;) and executes all the statements. In pure JDBC, I could write: String sqlQuery = "CREATE ...
Ondrej Skalicka's user avatar
30 votes
4 answers
92k views

Does Spring's JdbcTemplate close the connection after query timeout?

I have set query timeout (getJdbcTemplate().setQueryTimeout(5)) in method with insert statement. What will happen after query timeout, does jdbc template close my connection?
user3073662's user avatar
29 votes
1 answer
164k views

passing different types of arguments to jdbctemplate query

I am trying to retrieve records from the database by using where clause with few different types of arguments. This is the simple method which I wrote where I am passing breedId and gender as an ...
Suleman khan's user avatar
  • 1,058
29 votes
5 answers
68k views

Inserting multiple rows using JdbcTemplate

How can I execute the following SQL in a scalable way using JdbcTemplate running on mySQL. In this case, scalable means: Only one SQL statement is executed on the server it works for any number of ...
Edward Dale's user avatar
  • 29.9k
27 votes
5 answers
70k views

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<trackerv3Livedata> getTrackerData() { return List<trackerv3Livedata> live = (List<...
user avatar
27 votes
2 answers
63k views

jdbctemplate count queryForInt and pass multiple parameters

How can I pass multiple parameters in jdbcTemplate queryForInt to get the count. I have tried this, Integer count = this.jdbcTemplate .queryForInt("select count(name) from table_name where ...
Shijin Bhaskar's user avatar
26 votes
5 answers
236k views

Return Type for jdbcTemplate.queryForList(sql, object, classType)

I'm executing a named query using jdbcTemplate.queryForList in the following manner: List<Conversation> conversations = jdbcTemplate.queryForList( SELECT_ALL_CONVERSATIONS_SQL_FULL, ...
Global Dictator's user avatar
26 votes
2 answers
59k views

JdbcTemplate "queryForObject" and "query" is deprecated in Spring. What should it be replaced by?

Query for object, Student student = return jdbcTemplate.queryForObject("select * from student_id = ?", new Object[] { studentId }, studentRowMapper); For query, List<Student> students ...
Thirumal's user avatar
  • 8,947
26 votes
4 answers
50k views

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 ...
ZeroOne's user avatar
  • 3,101
26 votes
2 answers
20k views

JdbcTemplate queryForList return value in case of no results

The question is pretty much summed up in the title. What will JdbcTemplate.queryForList() return when the query returns no results. Will it return an empty List or null value? I couldn't find a ...
Reins's user avatar
  • 1,109
25 votes
6 answers
66k views

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 ...
nclord's user avatar
  • 1,307
25 votes
4 answers
32k views

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 ...
Raevik's user avatar
  • 1,991
24 votes
3 answers
104k views

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....
Shervin Asgari's user avatar
24 votes
4 answers
19k views

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 ...
stivlo's user avatar
  • 84.5k
23 votes
4 answers
64k views

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<User>. How can I do this? I tried: List rows = getJdbcTemplate().queryforList("select ...
Blankman's user avatar
  • 264k
22 votes
3 answers
10k views

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()); ...
lobs's user avatar
  • 429
21 votes
5 answers
58k views

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 ...
lolo's user avatar
  • 17.6k
20 votes
10 answers
63k views

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....
itsadok's user avatar
  • 29.1k
20 votes
3 answers
14k views

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 ...
Horse Voice's user avatar
  • 8,218
20 votes
4 answers
12k views

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 ...
Steve McLeod's user avatar
  • 52.1k
19 votes
3 answers
42k views

Java Programming - Spring and JDBCTemplate - Use query, queryForList or queryForRowSet?

My Java (JDK6) project uses Spring and JDBCTemplate for all its database access. We recently upgraded from Spring 2.5 to Spring 3 (RC1). The project does not use an ORM like Hibernate nor EJB. If I ...
Adrian's user avatar
  • 6,051
19 votes
2 answers
30k views

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 ...
Shawn D.'s user avatar
  • 8,045
19 votes
3 answers
26k views

How to use PostgreSQL hstore/json with JdbcTemplate

Is there a way to use PostgreSQL json/hstore with JdbcTemplate? esp query support. for eg: hstore: INSERT INTO hstore_test (data) VALUES ('"key1"=>"value1", "key2"=>"value2", "key3"=>"...
Aymer's user avatar
  • 331
19 votes
3 answers
41k views

How to get generated ID after I inserted into a new data record in database using Spring JDBCTemplate?

I got a very common question when I was using Spring JDBCTemplate, I want to get the ID value after I inserted a new data record into database, this ID value will be referred to another related table. ...
Brady Zhu's user avatar
  • 1,365
19 votes
2 answers
24k views

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 ...
Mohan's user avatar
  • 3,903
18 votes
4 answers
32k views

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 ...
nwGCham's user avatar
  • 313
17 votes
3 answers
62k views

JDBCTemplate queryForMap for retrieving multiple rows [duplicate]

Can I use queryForMap if there are multiple rows returned by the query. For a single row, the below code works fine. public Map<String, Object> retrieveMultipleRowsColumns(String deptName){ ...
user182944's user avatar
  • 7,997
17 votes
6 answers
49k views

Spring JdbcTemplate / NamedParameterJdbcTemplate passing null value as a parameter value

I have an issue passing a null value to NamedParameterJdbcTemplate using MapSqlParameterSource of the spring framework. Anyone knows how to do this? Currently my code is : String sql = "update ...
AlexLiesenfeld's user avatar

1
2 3 4 5
41