Table of Content

Sample Database Application: The Motive App

The Movie App is a simple command-line application with text menu using a relational database. It is a shopping application where customers can purchase movie media. The application is written in Java and accesses the database via JDBC. For the following steps, we assume you are at a Linux host and use the MariaDB.

Download Application

To download the source code and the sample database data dump, issue command:

wget https://github.com/huichen-cs/DBClassExamples/archive/refs/tags/v2025sp.zip

You should now have the vs2025sp.zip in your working directory. Next step is to extract files from it:

unzip v2025sp.zip

The files are extracted to the DBClassExamples-2025sp directory where the example application is at the MovieApp subdirectory. Now let’s go to the MovieApp subdirectory:

cd DBClassExamples-2025sp/MovieApp/

In this directory, you should locate a few files and directories, e.g.,

[user@sol26 MovieApp]$ ls
data  db_template.properties  README.md  src
[user@sol26 MovieApp]$

You should now create two directories:

mkdir lib bin

The purposes of these directories are as follows

  • src: the folder to maintain sources
  • lib: the folder to maintain dependencies
  • bin: the compiled output files, e.g., the class files go here
  • data: the example data in a MariaDB database dump is here.

Download MariaDB JDBC Driver

Assume that you are at the MovieApp directory and have already created the lib directory. Download the MariaDB JDBC driver:

wget https://dlm.mariadb.com/4234102/Connectors/java/connector-java-3.5.3/mariadb-java-client-3.5.3.jar -O lib/mariadb-java-client-3.5.3.jar

Import Sample Data to Database

Assume that you are at the MovieApp directory, and you have already set up the .my.cnf configuration file for your MariaDB database. To import the sample data in the data directory, run

mysql < data/movie_db_250327.sql

If you don’t have .my.cnf set up, you may have to run:

mysql -h DB_SERVER_IP_ADDRESS -u YOUR_DABASE_USERNAME -p YOUR_DATABASE < data/movie_db_250327.sql

after replacing the fields with your information. You should always validate that the database is successfully imported. You should find a number of relations whose names begin with P1.

Compile the Code

We assume that you have already created the bin directory. The class files will be generated in the bin directory. At the MovieApp directory, run:

javac -d bin src/*.java

You should now list the content of the bin directory and should locate a number of Java class files there.

Run the Application

First, create a database configuration file called db.properties in two steps:

cp db_template.properties db.properties

Using an editor to fill in your MariaDB information in the db.properties file, e.g., using nano

nano db.properties

Next, to run the application, at the MovieApp directory, issue:

java -cp bin:lib/mariadb-java-client-3.5.3.jar MovieApp

Explore Source Code

The application consists of a number of Java classes that you can locate in the src directory. The files falls in 4 groups:

  • Application entry point: MovieApp.java is the entry point of the application.
  • Business logic and application data model:
    • MovieDataModel.java: the data model for the application
    • There are a number of entity classes that are associated with database tables or query results:
      • Movie.java: represents movies.
      • Studio.java: represents movie studios.
      • MovieItem.java: movie media, such as DVD or Blu-ray disc items that customers can purchase.
      • Customer.java: represents customers.
      • ShoppingCart.java: represents shopping carts.
      • CartItem.java: an item in the shopping cart.
      • CustomerTransaction.java: a sales transaction for a customer.
      • Invoice.java: an invoice.
      • InvoiceItem.java: items on the invoice.
    • MovieView.java: the user interface of the application.
    • MovieController.java: the controller of the application.

Advice on Creating Your Own Applications

You can create your own application.

  1. Create a directory somewhere else, e.g., myapp at your home directory, e.g.,

    cd $HOME
    mkdir myapp
    
  2. Go to the directory, at the directory, create a few subdirectories:

    cd myapp
    mkdir bin lib data src
    
  3. Go to the src directory, and begin to write your own application. You can begin with these four classes – you should replace MyApp with something specific to your application.

    • MyApp.java: the entry point. You can copy and revise the MovieApp.java
    • MyAppDataModel.java: the data model. You can copy and revise the MovieDataModel.java. You may begin with an empty class file, and gradually add methods to it. Create entity classes as you go.
    • MyAppController.java: the controller class. You can copy and revise the MovieController.java. Again, You may begin with an empty class file, and gradually add methods to it.
    • MyAppView.java: the view class. You can copy and revise the MovieView.java. Likewise, You may begin with an empty class file, and gradually add methods to it.
  4. Don’t forget to reference the correct path of the JDBC driver or copy the driver to the lib directory.

  5. Have fun!