Generic Java GUI Netbeans Project with Embedded Database

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

2015-11-30 15_48_30-New notification

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.

That’s it.

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.

Prerequisite Knowledge

You need to understand a few things:

  1. Embedded databases
  2. Migrations

Embedded Database

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.

derby-logo-web

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…

netbeans 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”.

JDBC Url

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:

netbeans database service

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.

Migrations

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.

flyway-logo-tm

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:

2015-12-04 14_46_55-Search

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!

Advertisements

One thought on “Generic Java GUI Netbeans Project with Embedded Database

  1. Pingback: Converting a Java Project to Use JPA | Bruceoutdoors Blog of Blots

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s