Simple Java Database Swing Application

3 years ago by in Articles, Design, How To, Introduction, Maven, Swing, Tutorial Tagged: , , , , , , , ,

This tutorial demonstrates how to develop a simple Java application that connects with a database and provides a user interface using a series of videos compiled as a playlist (Youtube). The application is kept very simple while making use of various technologies, such as Maven (Homepage). This tutorial is targeted to college or university students who studied Java but lack experience and would like to do something productive. The main idea of this tutorial is not learn Java, but to help the viewers to gain experience on various technologies that are commonly used in industry but not necessarily taught in schools or courses. One of the greatest challenges a new developer has when starting a project from scratch is finding his or her way and make use of the right technology. The playlist presented by this tutorial shows how to build a Java application that connects to a database and provides a user interface in a step-by-step fashion.

All code listed below is available at: http://java-creed-examples.googlecode.com/svn/introduction/Simple Java Database Swing Application/. Each video has its own project, with the exception of the first, and the viewers can download or view all code from the above link.

Overview

The tutorial comprises the following

  1. Part 1 – Overview: Provides an overview about this tutorial and the application being developed. No projects are associated with this video.
  2. Part 2 – Create Project: Creates a blank Maven project using the provided wizard.
  3. Part 3 – Connect with Database: Describes how to manage the database libraries through Maven and establishes a database connection.
  4. Part 4 – DataSource and Flyway: Introduces the concept of DataSource (Java Doc) and Flyway (Homepage), a third party library that manages database migrations. This video demonstrates how to manage the database updates while the application develops and evolves with minimal effort.
  5. Part 5 – Logging: Logging is fundamental part of any somewhat complex application as this can be used to provide detailed information about a problem. This video focuses on two things. It shows how to make use of logs using a popular logging facade. Then it shows how to configure logging of third party libraries to work with the current logging framework.
  6. Part 6 – Domain: So far we only dealt with technologies but very little about the project. This video starts taking about the business logic related to the
  7. Part 7 – Testing: This video shows how to automate testing using JUnit (Homepage) test cases.
  8. Part 8 – Load From Database: So far we only saved data into the database. This video demonstrates how to load data from the database and how to test this new functionality.
  9. Part 9 – Update and Delete: This video demonstrates how to update an existing record and how to delete one too.
  10. Part 10 – Simple User Interface: This video shows how to build a simple user interface using Java Swing.
  11. Part 11 – Integration: Finally, the user interface is connected with the business logic. This video shows how to connect the user interface code with the domain code and complete the application.
  12. Part 12 – Packaging and Distribution: Once the program is ready, we need to package it and send it to our friends to use. This video shows how to create an executable JAR which we can share with others.

The application shown in this tutorial is developed with Spring Tool Suite, also referred to as STS, (Download). The viewers are free to use any integrated development environment, also referred to as IDE, (Wiki), they prefer. In such event, please note that some of the commands or shortcuts may not work on a different IDE and some things may be organised differently. If you are new to Java, it is highly recommended to use the same IDE as the one used in the videos as this will simplifies your learning experience. Please note that by no means we are saying that the other IDE are not good or that STS is better than all others. For more information about how to setup your environment please follow the Setting up the Environment tutorial.

Resources and Technologies

Following is a list of resources technologies used in this tutorial. These are shown in the order they appear.

  1. Spring Source Tool Suite: http://spring.io/tools/sts/all
  2. Maven Repository: http://mvnrepository.com/
  3. H2 Database: http://www.h2database.com/html/main.html
  4. Flyway: http://flywaydb.org/
  5. SLF4J Logging Facade: http://www.slf4j.org/
  6. Log4J Logger: http://logging.apache.org/log4j/1.2/
  7. JUnit: http://junit.org/

Tutorial

Please note that the videos are recorded in HD (1080p). Make sure to use the correct resolution to make the best out of these videos as shown below.

Videos Resolution

Videos Resolution

FAQs

  1. I am not sure how to find path of the first argument in getConnection() method. Could you explain it please? I do not know if I should even call it a path?
    The first argument of the getConnection() method is the database URL. Here we are using H2 database (Homepage) which is saved in a local path relative to the project. The path is “target/db“, where “target” is the folder and “db” is the database name. Say that you want to save the database under the directory called “db” with the name: “app“. Then the database connection URL will be “jdbc:h2:db/app“. Please make sure that this directory exists as otherwise you may get an error.
  2. First, I use netbeans as my IDE and do not know where I should locate my sql file. I have put the sql file everywhere but I keep getting this error

    com.googlecode.flyway.core.api.FlywayException: Unable to determine URL for classpath location: db/migration (ClassLoader: [email protected])
    

    how can I fix it?

    Where are you saving the db/migration folder structure? Are you using Maven?

    This path needs to be in your class path. Say you package is “com.mypkg”, then the folder “db” needs to be where the folder “com” is. If you are using Maven, the “db/migration” folders can be organised as shown in the video.

  3. How we can download the maven?
    Please follow the tutorial titled: Setting Up The Environment (Tutorial), which shows you how to configure your environment including Maven. Maven is descibed at Youtube – Maven.
  4. Where is the h2 database live exactly? Does it live inside the hard-disk or in the net? Usually when connecting to MySQL database for example I use J/connector and a port 3308 to get a connection visa the local host.
    H2 is a pure Java database which can be embedded within a program. In our example we are not installing a database but simply embedding it as part of our program. Therefore our code include a database. The tables and data are saved as files in a relative location to our project/program. Our database is not actively listening on a particular port like MySQL. Furthermore, only one program can work with such database.
  5. V1__initial_db.sql has an unknown type. I can’t open it with eclipse as it’s shown in your computer?
    Can you please confirm that you are using Maven and that your project is correctly configured? The file V1__initial_db.sql is a simple text file and you should be able to open it.

Should you have any queries, please do not hesitate to contact me at: [email protected] or through Google+.

Albert Attard

Albert Attard is a Java passionate and technical lead at a research group. You can find him on . Over the past years Albert worked on various Java projects including traditional server/client applications, modular applications, large data handling applications and concurrent data manipulation applications to name a few. He has a BSc degree from the University of London (Homepage) and an MSc Information Security with the same university. His MSc thesis (Book) received the 2012 SearchSecurity.co.UK award (Website).

One Response to “Simple Java Database Swing Application”


Maz
January 27, 2017 Reply

Hi There,

I am trying to follow along with this tutorial but it woud be reallly helpful if the code was available to download. I am stuck on part 8. I am getting a java.lang.NullPointerException but I can not find the reason why. I am pretty sure I have followed the code as shown in the tutorial but it would be must easier to detect the error if I could compare the code. Your code link does not work. Is this an inactive tutorial?

Leave a Comment