Questions tagged [spring-jdbc]

Spring JDBC is a part of Data access layer provided by Spring. The Spring Framework takes care of all the low-level details that can make JDBC such a tedious API to develop with such as database connections, creating prepare statements, process exceptions etc.,

Spring JDBC is a part of Data access layer provided by Spring. The Spring Framework takes care of all the low-level details that can make such a tedious API to develop with such as database connections, creating prepare statements, process exceptions etc.,

The following are the approaches to form the basis of JDBC database access.

JdbcTemplate is the classic Spring JDBC approach and the most popular. This "lowest level" approach and all others use a JdbcTemplate under the covers, and all are updated with Java 5 support such as generics and varargs.

NamedParameterJdbcTemplate wraps a JdbcTemplate to provide named parameters instead of the traditional JDBC ? placeholders. This approach provides better documentation and ease of use when you have multiple parameters for an SQL statement.

SimpleJdbcTemplate combines the most frequently used operations of JdbcTemplate and NamedParameterJdbcTemplate.

SimpleJdbcInsert and SimpleJdbcCall optimize database metadata to limit the amount of necessary configuration. This approach simplifies coding so that you only need to provide the name of the table or procedure and provide a map of parameters matching the column names. This only works if the database provides adequate metadata. If the database doesn't provide this metadata, you will have to provide explicit configuration of the parameters.

RDBMS Objects including MappingSqlQuery, SqlUpdate and StoredProcedure requires you to create reusable and thread-safe objects during initialization of your data access layer. This approach is modeled after JDO Query wherein you define your query string, declare parameters, and compile the query. Once you do that, execute methods can be called multiple times with various parameter values passed in.

2410 questions
21
votes
3 answers

Timeouts connecting to a Postgres database on Amazon RDS from Azure

I get the following exception in my application after leaving a database connection idle for some amount of time: ... An I/O error occured while sending to the backend.; nested exception is org.postgresql.util.PSQLException: An I/O error occured…
Cleber Goncalves
  • 1,936
  • 3
  • 18
  • 23
21
votes
5 answers

Clean way to externalize long (+20 lines sql) when using spring jdbc?

I want to externalize some large queries in my application to properties\sql\xml files. However I was wondering if anyone has some recommendations as to how achieve this in a clean fashion. Most results recommend using an ORM framework but this…
Jack Dans
  • 241
  • 1
  • 2
  • 5
20
votes
10 answers

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.execute( "INSERT INTO lob_table (id, a_blob)…
itsadok
  • 28,822
  • 30
  • 126
  • 171
19
votes
2 answers

ResultSet vs RowSet: Which one to choose and when?

So I'm aware of some relative differences i.e., the ResultSet has an 'open connection' to the database whereas a RowSet works in a 'disconnected' fashion. But that's pretty much what I understand (may be incorrect): My question is then this - under…
PhD
  • 11,202
  • 14
  • 64
  • 112
19
votes
4 answers

Spring Boot JDBC Template SQL Log

I am trying log SQL queries with params for Spring Boot JDBC but it is not printing the details in log.I am using Spring Boot 1.5.8 version.Please help me to solve…
sunleo
  • 10,589
  • 35
  • 116
  • 196
19
votes
2 answers

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 be deprecated in Spring 3.1. What is the…
Mohan
  • 3,893
  • 9
  • 33
  • 42
18
votes
9 answers

Spring JDBC Could not load JDBC driver class [oracle.jdbc.driver.OracleDriver]

I wonder if any one could help me with this. I encountered an issue when I tried writing code with Spring JDBC. When I ran the server, I got the message like I mentioned in the title. I have google it and someone said that you should import…
David Dai
  • 1,095
  • 2
  • 8
  • 11
17
votes
2 answers

When to use Map and SqlParameterSource in namedParameterJdbcTemplate?

String SQL = "INSERT INTO Employee (name, age, salary) VALUES (:name,:age,:salary)"; Map namedParameters = new HashMap(); namedParameters.put("name", name); namedParameters.put("age", age); namedParameters.put("salary",…
Sreepad Chitragar
  • 183
  • 1
  • 3
  • 12
16
votes
11 answers

Spring boot "Hikari Pool Shutdown" early error

ERROR: 2018-11-12 18:25:31.221 INFO 3028 --- [ Thread-3] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2018-11-12 18:25:31.223 INFO 3028 --- [ Thread-3] com.zaxxer.hikari.HikariDataSource :…
Shale
  • 338
  • 1
  • 2
  • 10
16
votes
3 answers

Accessing AWS RDS using IAM Authentication and Spring JDBC (DataSource and JdbcTemplace)

I am not able to figure out how to implement this. Any help and/or pointers will be greatly appreciated. Currently, my Java/Spring application backend is deployed on EC2 and accessing MySQL on RDS successfully using the regular Spring JDBC setup.…
Gauzy
  • 711
  • 3
  • 13
  • 26
16
votes
2 answers

Spring's Stored Procedure - results coming back from procedure always empty

I am using Spring's JdbcTemplate and StoredProcedure classes. I am having trouble getting the stored procedure class to work for me. I have a stored procedure on an oracle database. Its signature is CREATE OR REPLACE PROCEDURE…
aos
  • 187
  • 1
  • 2
  • 7
15
votes
1 answer

Transactions in spring boot testing not rolled back

I have an integrations test class for my UserController. The contents of the following class are: // imports... @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @RunWith(SpringRunner.class) @Transactional @Rollback public…
mmjmanders
  • 1,533
  • 2
  • 13
  • 32
15
votes
2 answers

What does @Transactional do?

I know this is probably a duplicate and, ironically, before I started reading here and there about it, I thought I knew what it was for (needless to say but I'll still say it, please correct me where I am wrong): It relieves the programmer of…
osh
  • 1,191
  • 4
  • 13
  • 21
15
votes
3 answers

JDBC Template - One-To-Many

I have a class that looks like this. I need to populate it from two database tables, which are also shown below. Is there any preferred way to do this? My thought is to have a service class to select a List<> via a ResultSetExtractor from a DAO. …
EdgeCase
  • 4,719
  • 16
  • 45
  • 73
15
votes
2 answers

Multiple one-to-many relations in Spring JDBC

I am using Spring JDBC and I am a bit unsure on how to work with multiple one-to-many relations (or many-to-many). In this case I am injecting a repository into one of my resultsetextractors so that I can retrieve its associations. Is this the way…
LuckyLuke
  • 47,771
  • 85
  • 270
  • 434