Git Product home page Git Product logo

gs-relational-data-access's Introduction

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]

In this example you set up a JDBC [DataSource] using Spring’s handy SimpleDriverDataSource. Then, you use the DataSource to construct a JdbcTemplate instance.

Note
SimpleDriverDataSource is a convenience class and not intended for production.

After you configure JdbcTemplate, it’s easy to start making calls to the database.

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.

gs-relational-data-access's People

Contributors

btalbott avatar cbeams avatar dsyer avatar gregturn avatar habuma avatar joshlong avatar royclarkson avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.