So your team just wants to create a java GUI project that requires a database. How do you minimize the hassle of configuring a database? For this reason I created this simple generic Micropost project to serve as a template to start out.
Link to Project: https://github.com/bruceoutdoors/MicropostsExample
This guide requires that you are already familiar with SQL and java GUI development.
Configuration and Setup
The only thing you need to run this project is Netbeans. Download the ZIP folder from the site, or git clone the repo if you know git. From there, you open up the project from Netbeans and click run.
Did I mention configuration? There is zero configuration. Even when you deploy your application as a JAR file, it is the same thing; it runs automatically. Just like that.
You need to understand a few things:
- Embedded databases
An embedded database is a database that do not need a server, and is embedded in an application. This means the application itself manages the database directly. Here, I am using Derby DB (or Java DB). JDK comes prepackaged with this, though to avoid trouble I have packaged that library into the project itself.
Notice, that when you launch the application a folder called “database” and a text file “derby.log” is created. These are the files you will remove should you want to start clean.
You can view the schema and data in the “database” folder from Netbeans. Under the services tab, right click on Databases and select New Connection…
Select Java DB (Embedded) as the JDBC driver and click next. Where you see JDBC URL, append the directory of the database folder. Leave the User Name and Password field blank. Click Test Connection and it should indicate “Connection Succeeded”.
Then click Next until you cannot click it anymore, followed by Finish. Now you can peep inside your embedded database, and execute queries as you please:
IMPORTANT NOTE: Derby DB embedded does not support multiple connections at once. If you connect to the database via Netbeans database service, then you can’t run your application. You have do disconnect (right-click the connection and disconnect it) from the database first before running your application again.
When you first create your database, you don’t always figure every possible entities and relationships in one shot. Inevitably, we would want to make incremental changes to add remove tables or modify existing tables, migrating the database to a different version. Chances are also that you don’t want to wipe out your existing data because of it. This is why migrations are created.
This project uses Flyway migration library. It is as simple as just using a series of SQL files with the prefix V1__blablabla.sql, V2__blablabla.sql and so on. These SQL files are then executed in that order. Therefore, when you make changes to your database, you simply add another migration file (or you can just modify a single migration and wipe out the database every time). If you are working together using a VC system like git, all your databases will be synchronized automatically to the latest structure with existing data intact.
Migrations are located in PROJECT_DIR/src/db/migrations:
Each time the application is launched, it automatically migrates the database to the latest version.
That is all you need to know. For everything else go figure on your own. Good luck!