Skip to content

Commit 4bc10ec

Browse files
committed
Convert to asciidoctor
1 parent f64ca71 commit 4bc10ec

File tree

4 files changed

+100
-370
lines changed

4 files changed

+100
-370
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ build/
3232
dependency-reduced-pom.xml
3333

3434
mem*.db
35+
36+
README.html

README.adoc

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
:spring_boot_version: 0.5.0.M6
2+
:DataSource:
3+
:toc:
4+
:icons: font
5+
:source-highlighter: prettify
6+
:project_id: gs-relational-data-access
7+
This guide walks you through the process of accessing relational data with Spring.
8+
9+
== What you'll build
10+
11+
You'll build an application using Spring's `JdbcTemplate` to access data stored in a relational database.
12+
13+
== What you'll need
14+
15+
include::https://raw.github.com/spring-guides/getting-started-macros/master/prereq_editor_jdk_buildtools.adoc[]
16+
17+
18+
:jump_ahead: Create a Customer object
19+
include::https://raw.github.com/spring-guides/getting-started-macros/master/how_to_complete_this_guide.adoc[]
20+
21+
22+
[[scratch]]
23+
== Set up the project
24+
25+
include::https://raw.github.com/spring-guides/getting-started-macros/master/build_system_intro.adoc[]
26+
27+
include::https://raw.github.com/spring-guides/getting-started-macros/master/create_directory_structure_hello.adoc[]
28+
29+
30+
include::https://raw.github.com/spring-guides/getting-started-macros/master/create_both_builds.adoc[]
31+
32+
`build.gradle`
33+
// AsciiDoc source formatting doesn't support groovy, so using java instead
34+
[source,java]
35+
----
36+
include::initial/build.gradle[]
37+
----
38+
39+
include::https://raw.github.com/spring-guides/getting-started-macros/master/bootstrap_starter_pom_disclaimer.adoc[]
40+
41+
42+
[[initial]]
43+
== Create a Customer object
44+
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.
45+
46+
`src/main/java/hello/Customer.java`
47+
[source,java]
48+
----
49+
include::complete/src/main/java/hello/Customer.java[]
50+
----
51+
52+
53+
== Store and retrieve data
54+
55+
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.
56+
57+
`src/main/java/hello/Application.java`
58+
[source,java]
59+
----
60+
include::complete/src/main/java/hello/Application.java[]
61+
----
62+
63+
In this example you set up a JDBC {DataSource}[`DataSource`] using Spring's handy `SimpleDriverDataSource`. Then, you use the `DataSource` to construct a `JdbcTemplate` instance.
64+
65+
NOTE: `SimpleDriverDataSource` is a convenience class and **not** intended for production.
66+
67+
After you configure `JdbcTemplate`, it's easy to start making calls to the database.
68+
69+
First, you install some DDL using `JdbcTemplate`'s `execute` method.
70+
71+
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.
72+
73+
NOTE: Use `?` for arguments to avoid http://en.wikipedia.org/wiki/SQL_injection[SQL injection attacks] by instructing JDBC to bind variables.
74+
75+
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.
76+
77+
include::https://raw.github.com/spring-guides/getting-started-macros/master/build_an_executable_jar_mainhead.adoc[]
78+
include::https://raw.github.com/spring-guides/getting-started-macros/master/build_an_executable_jar_with_both.adoc[]
79+
80+
81+
include::https://raw.github.com/spring-guides/getting-started-macros/master/run_the_application_with_both.adoc[]
82+
83+
You should see the following output:
84+
85+
....
86+
Creating tables
87+
Inserting customer record for John Woo
88+
Inserting customer record for Jeff Dean
89+
Inserting customer record for Josh Bloch
90+
Inserting customer record for Josh Long
91+
Querying for customer records where first_name = 'Josh':
92+
Customer[id=3, firstName='Josh', lastName='Bloch']
93+
Customer[id=4, firstName='Josh', lastName='Long']
94+
....
95+
96+
97+
== Summary
98+
Congratulations! You've just used Spring to develop a simple JDBC client.

README.ftl.md

-83
This file was deleted.

0 commit comments

Comments
 (0)