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
32
votes
4 answers

Spring is losing connection to the DB and does not recover or reconnect

I have a spring-boot application on the same host as the Maria DB and both are running fine for some time. But between 12 hours and 2 days it seems that the spring boot application looses the connection to the database (stacktrace) and does not…
Vad1mo
  • 5,156
  • 6
  • 36
  • 65
29
votes
5 answers

Difference Between Spring JDBC Vs Plain JDBC?

What is the main difference between Spring JDBC VS JDBC?
user1127214
  • 3,109
  • 7
  • 26
  • 30
28
votes
1 answer

spring-jdbc vs spring-data-jdbc and what are they supporting

I'm curious what is the difference between the spring-jdbc (what I missing in the newest spring release) and spring-data-jdbc. Is there a difference or just a renaming (in the repositories I don't see this)? And is there somewhere described what…
PaulEdison
  • 897
  • 1
  • 15
  • 36
27
votes
3 answers

Could not autowire. There is more than one bean of 'DataSource' type

I'm trying to Autowire a database by @Autowired private DataSource dataSource; I have one datasource in my application.yml spring: profiles: active: dev --- spring: profiles: dev datasource: driverClassName: org.mariadb.jdbc.Driver …
g3blv
  • 3,877
  • 7
  • 38
  • 51
26
votes
2 answers

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 parameter1 = ? and parameter2 = ?", new…
Shijin Bhaskar
  • 381
  • 1
  • 4
  • 9
25
votes
4 answers

How to do multiple inserts in database using spring JDBC Template batch?

I need to insert thousands of records in the database at one go. I am using spring JDBC template in my application. Below is the code I have written so far which executes all inserts at one go. So, if I ahve 10,000 users they are inserted at one go.…
ajm
  • 12,863
  • 58
  • 163
  • 234
25
votes
4 answers

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 normal SQL this would be trivial: select * from…
ZeroOne
  • 3,041
  • 3
  • 31
  • 52
24
votes
2 answers

How to manage database connection pool in spring jpa?

I am using spring-boot in my web application and use spring-jpa to read/write from/to my database. It works very well but I want to understand how to manage the database connections. Below is my properties configuration for…
Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523
23
votes
3 answers

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. List alleXmler =…
Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143
23
votes
3 answers

Spring: How to use KeyHolder with PostgreSQL

Recently migrated to POSTGRESQL, I am trying to obtain the uniquely generated key on creating a new entry into the db table. The table screenstable looks like this: CREATE TABLE screenstable ( id serial NOT NULL, screenshot bytea, CONSTRAINT…
Mono Jamoon
  • 4,437
  • 17
  • 42
  • 64
23
votes
2 answers

How to use a list of string in NamedParameterJDBCTemplate to get results

Experimenting with Spring-JDBC. I am using this as reference. I am trying to get a list of actors who have the same last name. Running this code gave me the desired results: public List getActorsWithSameLastName(String lastName, …
Mono Jamoon
  • 4,437
  • 17
  • 42
  • 64
22
votes
5 answers

Commit on jdbcTemplate or DataSource

I wanted to do commit and rollback using jdbcTemplate. My question is based on this thread How do I commit or rollback, should I do it on jdbcTemplate like jdbcTemplate.commit(); jdbcTemplate.rollback(); Or there are some other ways to achieve…
Akhil
  • 217
  • 2
  • 3
  • 7
22
votes
2 answers

What is JdbcDaoSupport used for?

In Spring, when we are inserting into the database, we can use JdbcDaoSupport or not. My question is, what are the advantages in using it and in which circumstances we should use it?
khateeb
  • 5,265
  • 15
  • 58
  • 114
21
votes
1 answer

How do I get Spring Boot to automatically reconnect to PostgreSQL?

I am running Spring Boot connecting to a PostgreSQL database. I have verified that data is written to the database if Spring Boot is started after the database. spring.datasource.url = jdbc:postgresql://localhost/dbname spring.datasource.username =…
mattm
  • 5,851
  • 11
  • 47
  • 77
21
votes
8 answers

Set default schema = SOMETHING in oracle using Spring Boot and Spring JDBC

I am working now with oracle and spring jdbc but I don't want to use the schema in my sql statements: Example: Select * from SCHEMA.table Is there any way to set default schema in application.properties or application.yml?