How To integrate Java DB into a NetBeans RCP | Part 1

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‘.

Derby 01

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.

RCP Structure

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

Derby 02

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:

 

http://cid-710855daf5933a1f.skydrive.live.com/embedrowdetail.aspx/Weblog/CodeSnippets

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.

Cheers

Über sageniuz

https://about.me/ClausPolanka
Dieser Beitrag wurde unter NetBeans veröffentlicht. Setze ein Lesezeichen auf den Permalink.

Eine Antwort zu How To integrate Java DB into a NetBeans RCP | Part 1

  1. ViKARLL schreibt:

    Dear Sir,
     
    After a six day search as to why I cannot get Netbeans 6.0.1 to work with Derby Embedded Mode, I came across your great tutorial which is the only, if not the best, teaching material as far as the clarity and ease of following  is concerned. Even the NetBeans official examples seems to have completely ignored the Embedded Mode with NetBeans 6 and if you read the forums, you\’ll find scores of people struggling with the connection of NB 6.X to the Java DB.
     
    But unfortunately, I could not complete your tutorial owing to the following two obstacles.
     
    1) At the stage of adding dependencies at Step 4, the main module (Media Database; in our case)
        > properties > Libraries  does  not give a provision to add dependencies. When I select Libraries, the dialog box is populated with some other information, also carrying the following message in Red lettering at the bottom;
    “Module Java Profiler (Java EE Projects Support) in cluster profiler 2 depends on an unknown module named org.netbeans.api.web.webmodule”
                      
    This happens only with the Main Module. When you check with the other two modules ‘Derby Embedded’ and ‘Music Album’ those allow you to add dependencies under the Libraries property.
     
    2) I use “NetBeans 6.0.1” with  “jdk-6u6-windows-i586-p”.
        I tried to give the same environment your site has mentioned, i.e., by installing  JDK 1.4 and
    NetBeans 6.0 but then, NetBeans installation aborts giving an error message saying that it needs JDK 1.5 or higher.
     
     Your advice on this is highly appreciated please !
     
    Thanking you with best wishes,
     
    ViKARLL

Schreibe einen Kommentar

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s