Skip to content

Accessing Relational Data using JDBC with Spring :: Learn how to access relational data with Spring.

Notifications You must be signed in to change notification settings

odrotbohm/gs-relational-data-access

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tags projects
spring-data
jdbc
spring-framework

This guide walks you through the process of accessing relational data with Spring.

What you’ll build

You’ll build an application using Spring’s JdbcTemplate to access data stored in a relational database.

Create a Customer object

The simple data access logic you will work with below below manages first and last names of customers. To represent this data at the application level, create a Customer class.

src/main/java/hello/Customer.java

link:complete/src/main/java/hello/Customer.java[role=include]

Store and retrieve data

Spring provides a template class called JdbcTemplate that makes it easy to work with SQL relational databases and JDBC. Most JDBC code is mired in resource acquisition, connection management, exception handling, and general error checking that is wholly unrelated to what the code is meant to achieve. The JdbcTemplate takes care of all of that for you. All you have to do is focus on the task at hand.

src/main/java/hello/Application.java

link:complete/src/main/java/hello/Application.java[role=include]

@SpringBootApplication is a convenience annotation that adds all of the following:

  • @Configuration tags the class as a source of bean definitions for the application context.

  • @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.

  • Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.

  • @ComponentScan tells Spring to look for other components, configurations, and services in the the hello package. In this case, there aren’t any.

The main() method uses Spring Boot’s SpringApplication.run() method to launch an application. Did you notice that there wasn’t a single line of XML? No web.xml file either. This web application is 100% pure Java and you didn’t have to deal with configuring any plumbing or infrastructure.

Spring Boot spots H2, an in-memory relational database engine, and automatically creates a connection. Because we are using spring-jdbc, Spring Boot automatically creates a JdbcTemplate. OUr @Autowired JdbcTemplate field will cause an instance to automatically get loaded and made available to us.

This Application class implements Spring Boot’s CommandLineRunner, which means it will execute the run() method after the application context is loaded up. First, you install some DDL using JdbcTemplate’s `execute method.

Then, you install some records in your newly created table using JdbcTemplate’s `update method. The first argument to the method call is the query string, the last argument (the array of Object s) holds the variables to be substituted into the query where the “?” characters are.

Note
Use ? for arguments to avoid SQL injection attacks by instructing JDBC to bind variables.

Finally you use the query method to search your table for records matching the criteria. You again use the “?” arguments to create parameters for the query, passing in the actual values when you make the call. The last argument in the query method is an instance of RowMapper<T>, which you provide. Spring’s done 90% of the work, but it can’t know what you want it to do with the result set data. So, you provide a RowMapper<T> instance that Spring will call for each record, aggregate the results, and return as a collection.

You should see the following output:

Creating tables
Inserting customer record for John Woo
Inserting customer record for Jeff Dean
Inserting customer record for Josh Bloch
Inserting customer record for Josh Long
Querying for customer records where first_name = 'Josh':
Customer[id=3, firstName='Josh', lastName='Bloch']
Customer[id=4, firstName='Josh', lastName='Long']

Summary

Congratulations! You’ve just used Spring to develop a simple JDBC client.

About

Accessing Relational Data using JDBC with Spring :: Learn how to access relational data with Spring.

Resources

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 87.7%
  • Shell 12.3%