Java Reference
In-Depth Information
Chapter 5
Data Access with Lambdas
In the last chapter, we saw how lambdas enable us to work with files and streams. In this chapter, we will look
at interacting with the database. Way back in chapter 1, we saw how lambdas can help Spring's JDBCTemplate
by making it easy to implement its callback interfaces. In chapter 3, we were introduced to streams, and
in chapter 4, we saw how streams can make it easier to work with file data. In this chapter, we will see
the specifics of working with Java's data access structures (the java.sql package). This will give us an
opportunity to meet another new data structure in Java 8's standard library: the spliterator.
If we are going to work with data access, we need to be working with some non-trivial database.
In this chapter, we will be working with an H2 in-memory database containing an index of all the words in
all the works of Shakespeare. These words are organized by line number. This means that we have a text
table, a line table, a word table, and a line_word table joining line and word . The UML diagram for the
tables looks like this:
text
+ id : int primary key auto_increment
+ name : varchar unique
+ year : int
line
1-to-many
+ id : int primary key auto_increment
+ text_id : int
+ offset : int
word
1-to-many
+ id : int primary key auto_increment
+ value : varchar unique
line_word
1-to-many
+ id : int primary key auto_increment
+ line_id : int
+ word_id : int
+ offset : int
This database was loaded from the texts provided by Project Gutenberg at
http://www.gutenberg.org/ebooks/100 . In total, there are 38 texts (the sonnets are grouped as a single
text). There are about 113,000 lines, about 36,000 distinct words, and almost a million words used. While this
is still a relatively tiny database by corporate standards, it is big enough to slow down processing and warrant
some special handling. We will walk through loading the database in Appendix A, where we will contrast
imperative, object-oriented, and postfunctional approaches. For the sake of this chapter, we will start with
a populated database and move through there.
 
Search WWH ::




Custom Search