Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Second Iteration of Shape Calculator, Part 2: The App

DZone's Guide to

Second Iteration of Shape Calculator, Part 2: The App

The finale to the second iteration of a command-line calculator using web-based technologies.

· Java Zone
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

Implement, Deploy, Test App

We are one step closer to our goals of employing our "reusable component" (or service) in a few web-based technologies.

Let's complete the second iteration by implementing a command-line app.  This application will depend on the "shape-calca-jpa-hibernate" project that we constructed and tested in the previous article.

New Project

I chose to create yet another new project in Eclipse and named it "shape-calc-app-depends-on-shape-calc-jpa-hibernate".

I then copied over all of the existing files from the app we just completed, "shape-calc-app-1".

You will need to change the project's POM to have a dependency to the "shape-calc-jpa-hibernate" (this is our service , our calculator, our reusable component), rather than the dependency to "shape-calc-p5".

Main Class Enhancements

I won't go into any code details of the app.  You can review the code for yourself.  It very straight-forward, simple.   We are just utilizing more of the underlying service's operations, exposing them in menu form.

You can review all of this new project's changes by going here: https://github.com/elicorrales/shape-calc-cmd-line-app-dep-on-jpa-hibernate/tree/master/shape-calc-app-depends-on-shape-calc-jpa-hibernate

Deploy and Test

Let's go ahead and do a Maven clean install and then refresh the project folder.

Drill down into the "target" folder and you will see the "jarDir", and also a "lib" sub-folder. Open that and you should see the dependency jar: shape-calc-jpa-hibernate-0.0.1-SNAPSHOT.jar

If you don't already know how to export an executable jar with all the dependencies, see the previous article.  There IS one difference, from the previous article — the destination.  Make sure it says something like so: C:\cygwin64\home\the.transporter\shape-calc-app-dep-on-jpa-hibernate

Now let's open up a Cygwin terminal, and do an "ls -l" (listing), just as we have already done before, and you should see the old command-line app, as well as the one we just exported.

Image title

Before we attempt to run the app, let's open another Cygwin terminal, and start up the database, just like we did before. You should be able to recall the command by using the up-arrow at the Cygwin command-prompt.  (mysqld --console)

Open yet another Cygwin terminal.  Again look for the previous command, this time it would be the MySQL client (mysql --host=127.0.0.1 --user=root).

We want to start with a clean slate to demonstrate some of Hibernate's workings, based on our settings in the "application.properties" resource file of our project.

At the "mysql>" prompt, enter "show databases;"

You will most likely see a list, and it includes our "shapecalc" database.

At the "mysql>" prompt, enter "use shapecalc;"

At the next "mysql>" prompt, enter "show tables;"

If you followed on and actually did everything in our previous article, you should see this:

Image title

Let's delete those tables.

Enter these commands, and see these results:

Image title

Ok, NOW we are ready to try our latest app.

Switch over to the second Cygwin terminal, the one where you did the directory listing and saw the latest app.

Enter this command to run the app: java -jar shape-calc-app-dep-on-jpa-hibernate/jarDir/shape-calc-app-dep-on-jpa-hibernate.jar  (this is all on line). Right away I noticed something that I forgot.  (Did you catch it?)

We had greatly reduced logging in our first iteration of the calculator and command-line app pair of projects. But in my run here, I see lots of Debug logging. 

I forgot to copy over the "logback.xml" from our first app's "src/main/resources" folder.

If you remembered, great.  I have to re-build, re-export, and re-try the app.

When I re-tried it, I still got some start-up logging, but I am not sure where it's originating from. Since it just occurs during the initial startup, let's ignore it and move on.

However, when I interact with the app menu features, such as asking to "View All Requests", I get SQL statements.  That is Hibernate, of course.

Image title

If we wish to turn that off, we need open the shape-calc-jpa-hibernate project (not the app), go to src/main/resources, and edit the application.properties. Change the line "hibernate.show_sql=" to false.

Image title

Then re-build, re-build, and export the app project as well.

I have a Run Configuration for shape-calc service (not the app) because I don't want to wait for all the tests to run on this particular install (skip tests).

I'm sure we could have structured these parent-child projects with Maven to better automate changes, but I want to move on and not spend any more time than necessary.

If you ran the latest app once already, (I did when I noticed the logging), then let's take a look at the database. Go to the Cygwin terminal with the client session, and hit the up-arrow to recall the "show tables;" command.

It is not showing "Empty set (0.00 sec)" as before. The two tables are present.

And so, Hibernate uses the information we have given it, such as the various annotations in our entity classes, and contructed those tables.

If you try the following, you should see this:

Image title

And that matches what we have in our CalculationResult entity class.

Switch to the Cygin window where we ran our app. Recall and re-run the command.

Anyway, you can play with the app, and verify the changes with the Cygwin window running the database client.

Next Phase

We are done (for now) with command-line applications. The next iteration will be to web-enable our calculator, our reusable component or service.

To be continued in the next article!

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
java 8 ,maven ,cygwin ,mysql ,hibernate

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}