Skip to content

Spring Framework Blog

Menu
  • Home
  • Spring
  • Spring Boot
  • About
Menu

Spring JDBC

Posted on June 10, 2024June 14, 2024 by wpadmin

Table of Contents

Toggle
  • JDBC
  • Configuration
  • Queries
  • Callbacks
  • Tests

JDBC

Configuration

JDBC Template requires a DataSource.

It’s thread-safe there the same object can be reused as a singleton.

Every-time a JdbcTemplate is used for executing a SQL query, it requests a connection from the datasource.

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/jdbc/core/JdbcTemplate.html

Queries

Return typeName
<>querygeneric query
<T>queryForObjectretrieves an object (can use RowMapper)
Map<String,Object>queryForMapretrieves just one object properties in a map.
List<Map<String, Object>>queryForListretrieves just multiple object properties in a map.
intupdateexecute insert, update or delete, returns number for rows affected
voidexecutepreferable method to execute DDL statements

Callbacks

RowMapper<T>mapRow<ResultSet, rowno>
ResultSetExtractor<T>extractData(ResultSet)
RowCallbackHandlerprocessRow(ResultSet)

Tests

@Sql is used to annotate a test class or test method to configure SQL scripts to be run against a given database during integration tests.

  @Test
@Sql({"/test-schema.sql", "/test-user-data.sql"})
void userTest() {
// run code that relies on the test schema and test data
}

@SqlConfig defines metadata that is used to determine how to parse and run SQL scripts configured with the @Sql annotation.

  @Test
@Sql(
scripts = "/test-user-data.sql",
config = @SqlConfig(commentPrefix = "`", separator = "@@")
)
void userTest() {
// run code that relies on the test data
}
  • Spring
  • Documentation
  • References
  • Toc
  • Books
  • Certification
  • AOP
  • Config
  • Java
  • Java core
  • JDBC
  • JPA
  • Rest
  • Security
  • Spring
  • Spring Boot
  • Spring Core
  • Spring Data
  • Spring MVC
  • Spring Rest
  • Spring Security
  • Tests
  • Transactions
  • Uncategorized

Recent Posts

  • Spring Annotations
  • Java Tests
  • Java operators
  • Java versions
  • Java Oracle Licenses
  • Configuration properties
  • MockMvc
  • Spring Security III
  • MVC Controller Method Params
  • JPA Methods
  • Transaction propagation and isolation
  • Spring JDBC
  • Spring Boot Auto-Configuration
  • Spring Resource interface
  • JSR 330 Standard Annotations
  • Spring Aware Interfaces
  • Spring Transactions
  • Spring Boot Core
  • MVC Rest
  • Spring Boot JPA
©2025 Spring Framework Blog | Built using WordPress and Responsive Blogily theme by Superb