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
18
votes
3 answers

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"=>"value3"') SELECT data -> 'key4' FROM hstore_test SELECT…
Aymer
  • 321
  • 1
  • 4
  • 11
18
votes
3 answers

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 need to read a bunch of records, and do some…
Adrian
  • 6,013
  • 10
  • 47
  • 68
17
votes
2 answers

how to copy data from file to PostgreSQL using JDBC?

I want to copy data from file to PostgreSQL DB using JDBC. I was using JDBC statement object to copy the file into DB. It is very slow. I came to know that we can also use copy out command to copy file to DB. But, how can I do that with JDBC. Even…
Reddy
  • 1,620
  • 6
  • 26
  • 33
17
votes
3 answers

JDBCTemplate queryForMap for retrieving multiple rows

Can I use queryForMap if there are multiple rows returned by the query. For a single row, the below code works fine. public Map retrieveMultipleRowsColumns(String deptName){ return jdbcTemplate.queryForMap("SELECT…
user182944
  • 7,897
  • 33
  • 108
  • 174
16
votes
6 answers

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 person set project = :project where id =…
AlexLiesenfeld
  • 2,872
  • 7
  • 36
  • 57
16
votes
3 answers

prevent sql injection in oracle "order by" part

To get some data I'm creating an sql query :) Of course there's some filtering and ordering parts. To get the result I use "NamedParameterJdbcTemplate" and when I need to add something to the "where" part, I use parameter map, to prevent…
Dainius
  • 1,765
  • 1
  • 17
  • 36
16
votes
4 answers

return a boolean - jdbcTemplate

I would like to return a boolean value using in this method: public Boolean isSizeOk(String transactionId){ String sqlQuery = "SELECT true FROM customer_pool WHERE id = "+ transactionID + " AND level = 13)"; //The next line is the problem. …
bdfios
  • 657
  • 6
  • 17
  • 29
16
votes
3 answers

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. I tried the following way to insert it, but I…
Brady Zhu
  • 1,305
  • 5
  • 21
  • 43
16
votes
4 answers

How can I get the autoincremented id when I insert a record in a table via jdbctemplate

private void insertIntoMyTable (Myclass m) { String query = "INSERT INTO MYTABLE (NAME) VALUES (?)"; jdbcTemplate.update(query, m.getName()); } When the above query inserts a record, the ID column in the table autoincrements. Is there a…
birdy
  • 9,286
  • 24
  • 107
  • 171
14
votes
3 answers

why spring jdbcTemplate batchUpdate insert row by row

I have 200K rows to be inserted in one single database table. I tried to use jdbcTemplate.batchUpdate in spring in order to do insertion 10,000 per batch. However, this process consumes too much time (7 mins for 200K rows). So on database side, I…
Ensom Hodder
  • 1,522
  • 5
  • 18
  • 35
14
votes
5 answers

A ResourcePool could not acquire a resource from its primary factory or source

I'm trying to connect to a database in Java, using jdbcTemplate and I'm gettin the error below. I have Googled for a long time and all solutions I found didn't solve my problem. I tried several different DBs (both SQLServer and MySQL) and none…
iomartin
  • 3,149
  • 9
  • 31
  • 47
14
votes
2 answers

update a row using spring jdbctemplate

I am new to spring. I am developing a CRUD application using spring jdbc template. I am done with insert and select. but in update am facing some problem. can anybody provide me a simple example of update and delete using jdbctemplate. thnks in…
smya.dsh
  • 651
  • 5
  • 10
  • 23
13
votes
3 answers

Unit testing a DAO class that uses Spring JDBC

I have several DAO objects that are used to retrieve information from a database and I really want to write some automated tests for them but I'm having a hard time figuring out how to do it. I'm using Spring's JdbcTemplate to run the actual query…
Alex Ciminian
  • 11,398
  • 15
  • 60
  • 94
13
votes
2 answers

Spring jdbcTemaplate how to send complete batch size to DB2 server in one shot?

While jdbcTemplate.batchUpdate(...) is running I can see DB row count is increased gradually(by running count(*) in the table), initially 2k then 3k and goes till 10k. 2k and 3k are not exact numbers sometimes I get 235 and then 4567. I was…
Vipin
  • 4,851
  • 3
  • 35
  • 65
13
votes
4 answers

How to set custom connection properties on DataSource in Spring Boot 1.3.x with default Tomcat connection pool

I need to set some specific Oracle JDBC connection properties in order to speed up batch INSERTs (defaultBatchValue) and mass SELECTs (defaultRowPrefetch). I got suggestions how to achieve this with DBCP (Thanks to M. Deinum) but I would like…
Marged
  • 10,577
  • 10
  • 57
  • 99