In this post I’d like to show you how you can integrate the lightweight pure java database Java DB aka Derby into a NetBeans Rich-Client project. First of all you have to install all necessary software. The following technologies will be needed for this tutorial.
You don’t have to download the Java DB separately because it comes with NetBeans out of the box or if you download the current version of the Java SDK (1.6) you will find the database in the folder db (default settings) in your JDK installation folder.
The Example RCP
We will build a small RCP based on the NetBeans platform which allows you to store, list, edit and delete music albums in your embedded Java DB. Also you will be able to categorize your albums by genres which will be a separate table in the database.
The Java DB
First of all we have to set up the Java DB within NetBeans. The IDE should arleady have configured its own Java DB but of course you can reconfigure it so that you can use the database which comes with the Java 6 SDK. You just have to open the database settings which you will find under ‚Tools/Java DB Database/Settings‘.
In the Settings-Dialog you will be able to change the Java DB installation-folder and the folder in which you will store all of your databases. The best practice is to store the database in the User-Home directory so that there won’t be any platform dependent issues with the path syntax of your used OS.
Our RCP Sample-Application consists of a Module Suite, a Library Wrapper Module which holds the derby.jar (which is the embedded database) and a Module which contains the main application logic. Of course you can chose another project structure but the Library Wrapper Module is mandatory.
1. Module Suite
Simply create a new project under ‚File/New Project/NetBeans Modules/Module Suite‚ within NetBeans. Click yourself through the Wizard and configure the Module Suite’s properties. I named my Suite ‚Media Database‘.
2. Library Wrapper Module
The next step is to create the Wrapper Module for the embedded database. Select your Module Suite and open the folder hierarchy. Select the Modules folder and open the context menu by right clicking that folder. Press ‚Add New Library…‘ and select the derby.jar which you will find in the lib folder of your Derby installation directory.
Again configure all of your settings by clicking through the Wizard. I simply named my Module ‚Derby Embedded‘ and for the Code Base Name I configured ‚org.apache.derby‘.
3. Simple Module
The last Module we have to create is the Module which holds the application logic. Repeat the same steps of Point 2 but this time select ‚Add New…‘ when you right click your Module Suite’s Modules folder. Name your Module with a name of your choice. I named my Module ‚Music Album‘. Again click through the Wizard and configure you Module.
4. Declare Dependency
Your main Module will handle all the database functionality. So you have to configure a dependency on your Library Wrapper Module so that the main Module can call JDBC functions of the embedded Java DB Driver which the derby.jar contains. Right click your main Module and select ‚Properties‘. Under ‚Libraries‘ click ‚Add Dependency…‘ and select your Derby Library Wrapper Module. The first time the JDBC driver will be loaded the database will automatically start up.
Final Project Structure
Configure Java DB with Module Installer
In a NetBeans RCP Application you can implement functionality which should be executed depending on a Module’s Lifecycle by creating a Module Installer. This class contains several methods which allow you to implement such code and depending on the Module’s Lifecycle phase the code will be invoked.
Select your Main Module and open your Source Packages. Right click a package of your choice and select ‚New…/Module Installer‘ or ‚New…/Other…/Module Development/Module Installer‘. After creating the Installer the corresponding class will automatically open up in your editor.
I won’t rewrite the functionality of the Module Installer in this post because I’ve zipped my Media Database and put it onto my Windows Live SkyDrive Share. The RCP is about 7,80 MB and you can find it under the following address:
Just download and import it into your NetBeans IDE. You will find the Module Installer in the Music Album Module and there you will find the whole code. I think I’ve commented it in detail so that there shouldn’t be any questions.
Of course should you have some questions just post a comment and I’ll try to answer your question as good as I can. In the the next part of this tutorial I’ll add a Top Component which will contain the main logic to read, edit, add and delete new music albums.
So stay tuned and try out this tutorial. I’ll write the next part very soon so that you can test the Media Database on your workstation. Have a nice Sunday.