Become a Master of Java Streams – Part 6: Creating a New Database Application Using Streams
Have you ever wanted to develop an “express” version of your database application? In this Hands-On Lab article, you will learn a truly easy and straightforward method. The entire Java domain model will be automatically generated for you. You just connect to your existing database and then start developing using Java streams. You will be able to create, for example, a new web application for your existing database in minutes.
This article is the last article in the series on How to Become a Master of Java Streams.
Part 1: Creating Streams
Part 2: Intermediate Operations
Part 3: Terminal Operations
Part 4: Database Streams
Part 5: Turn Joined Database Tables Into a Stream
Part 6: Creating a Database Application Using Streams
So far, you got to experience Speedment in the articles and through the exercises. For brevity, we did not include any descriptions on how to start from scratch but rather wanted you to get a glimpse of what using Java Streams with databases could look like. In this article, we’ll show you how to leverage Speedment for applications running against any of your databases. Setup only takes a few minutes but will save you vasts amounts of time due to the expressiveness of Streams and the provided type-safety.
Getting Started
To help you configure your project, Speedment provides a project Initializer. Once you fill out the details of your project, it provides you with a zip-file containing a pom.xml
with the needed dependencies and a Main.java
starter.
The Speedment Initializer can be used to configure a Speedment project.
Once you have clicked “download”, unzip the file and open the project in your IDE as a Maven project. In IntelliJ, the easiest way to do that is to choose File -> Open and then select the pom.xml-file in the unzipped project folder.
If you rather want to use Speedment in an existing project, configure your project via the Initializer to make sure you get all needed dependencies. Then simply merge the provided pom.xml with your current one and reimport Maven.
As you may recall from the previous articles, Speedment relies on an automatically generated Java domain model. Hence, before we can write our application, we need to generate the required classes. This is done using the Speedment Tool which is started by running mvn speedment:tool
in the terminal or by running the same target via the IDE:s built-in Maven menu.
Firstly, you will be asked to register for a free license and connect to your database. A free license can be used for all open-source databases (unlimited use) and commercial databases (up to 500 MB and doesn’t require any billing information).
A free license can be used with all open-source databases (unlimited) and commercial databases (up to 500 MB and does not require billing information.)
Once you complete the registration, you will be asked to provide credentials for your database (make sure you selected the correct DB-type in the initializer). Either use a local database of your own or run some tests with the Sakila database we used in the exercises.
Sakila Database Credentials
Type: MariaDB
Host: 35.203.190.83
Port: 3306
Database name: sakila
User: sakila
Password: sakila
Fill out the database credentials to connect to your data source. (Note: Speedment never stores your database password).
A click on the “Connect”-button will launch the Speedment Tool. It presents the database structure to the left-hand side and settings for the selected table or column on the right-hand side. In this case, the default settings are sufficient meaning we can go ahead and press “Generate” (If your application doesn’t require all the tables and/or columns you can disable these before generating).
The Speedment Tool visualizes the data structure and allows customizations of the generated code.
Next, Speedment will analyze the database metadata and generate the entire Java domain model. Once this process is completed you are ready to write your application. If you check out the Main.java-file, you will find a project starter containing something like this:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 | public class Main { public static void main( final String... args) { Speedment app = new MyApplicationBuilder() .withUsername( "your-dbms-username" ) .withPassword( "your-dbms-password" ) .build(); app.stop(); } } |
From here, you are ready to build your application using the examples we have provided in the previous articles. Thereby, we can close the circle by fetching a Manager for the Film table (a handle to the content of the film table) by typing:
1 | FilmManager films = app.getOrThrow(FilmManager. class ); |
1 | <br> |
Using the Manager we can now query our connected database as we have shown:
1 2 3 4 5 6 7 8 9 | List<Film> filmsTitleStartsWithA = films.stream() .filter(Film.TITLE.startsWith( "A" )) .sorted(Film.LENGTH) .collect(Collectors.toList()); filmsTitleStartsWithA: [ FilmImpl { filmId= 15 , title=ALIEN CENTER, …, rating=NC- 17 , length = 46 , FilmImpl { filmId= 2 , title=ACE GOLDFINGER, …, rating=G, length = 48 , … ] |
Exercises
This week there is no associated GitHub repo for you to play with. Instead, we encourage you to integrate Speedment in a new or an existing database application to try out your newly acquired skills.
Extra Exercise
When you are ready with your project, we encourage you to try out
HyperStream, especially if you have a large database and want to increase the reading performance.
HyperStream goes beyond Stream and adds in-JVM-memory capabilities which boost application speed by orders of magnitude. You only need to add a few lines of code in your existing pom.xml
and your Main.java
file:
1 2 3 4 5 6 | .withBundle(InMemoryBundle. class ) // add to the app builder ... // Load data from database into materialized view app.getOrThrow(DataStoreComponent. class ) .load(); |
1 | <br> |
Read more in the user-guide. The Stream API remains the same but performance is vastly increased.
Conclusion
During the past six weeks, we have demonstrated the usefulness of the Java Stream API and how it can be leveraged for writing type-safe database applications in pure Java. If you wish to learn more about Speedment, check out the user guide which also contains a more thorough guide on Java Streams.
Lastly – thank you for taking interest in our article series, it has been truly great to see that many of you have been following along with the provided exercises. Happy coding!
Authors
Per Minborg
Julia Gustafsson
Resources
Further reading about Speedment Stream JOINs
Speedment Manual
Speedment Initializer
Speedment on GitHub
Published on Java Code Geeks with permission by Per Minborg, partner at our JCG program. See the original article here: Become a Master of Java Streams – Part 6: Creating a New Database Application Using Streams Opinions expressed by Java Code Geeks contributors are their own. |