Simple Java Database Swing Application
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.
Each video has its own project, with the exception of the first, and the viewers can download or view all code from the provided links.
The tutorial comprises the following
Part 1 - Overview: Provides an overview about this tutorial and the application being developed. No projects are associated with this video.
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. (GitHub)
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. (GitHub)
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. (GitHub)
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. (GitHub)</li>
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.
- Spring Source Tool Suite: https://spring.io/tools3/sts/all
- Maven Repository: http://mvnrepository.com/
- H2 Database: http://www.h2database.com/html/main.html
- Flyway: http://flywaydb.org/
- SLF4J Logging Facade: http://www.slf4j.org/
- Log4J Logger: http://logging.apache.org/log4j/1.2/
- JUnit: http://junit.org/
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.
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.
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: sun.misc.Launcher$AppClassLoader@1174ec5)
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.
How we can download the maven?
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.
V1__initial_db.sqlhas 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.sqlis 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@example.com or through LinkedIn.