How to integrate Java DB into a NetBeans RCP | Part 2

In my last post we created a NetBeans RCP application which I’ve called ‚Media Database‘. Up to now we’ve only integrated the Java DB in our application via a NetBeans Library Wrapper Module. We’ve also created a ’normal‘ Module in which we will implement our ‚business‘ logic. We already created a NetBeans Module Installer (in above Module) which tests if all tables (albums, genres) have already been created in the Java DB and if not then automatically creates them. It also automatically shuts down the Java DB if the application is closed.

Because we don’t use an Object-Relational Mapping tool to get all data out of the database we encapsulate the whole database communication into one Java Class which I’ve called com.live.spaces.sageniuz.musicalbum.dal.DataModel. In this class you will find the following five methods:

  • deleteAlbum(Album album)
  • getAlbums() : Vector<Album>
  • getGenres() : Vector<Genre>
  • insertAlbum(Album album)
  • updateAlbum(Album album)

I won’t explain each function in detail. You will find the whole application on my Windows Live SkyDrive Share.

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

The next step was to create my corresponding two model-classes:

  • com.live.spaces.sageniuz.musicalbum.model.Album
  • com.live.spaces.sageniuz.musicalbum.model.Genre

These two classes are simple JavaBeans which represents the corresponding relations from the database. The last step is to create our view. You have to create a NetBeans TopComponent which shows the music albums from the database in a JTable. I’ve created a new package which I’ve called com.live.spaces.sageniuz.musicalbum.view. You then have to right click your package and select ‚New…/Window Component…/‘ or ‚New…/Other…/Module Development/Window Component‘ to create a new NetBeans TopComponent.

Click yourself through the Wizard and configure the TopComponent as you like. I’ve chosen the window position ‚explorer‘ and the TopComponent should open on Application Start. For the prefix I’ve chosen ‚Albums‘. After the Wizard has finished a lot of new tabs will open up. Select the AlbumsTopComponent tab and design your window like the following screenshot shows.

Derby 03

The big gray rectangle is the JTable. Just select a JTable in your NetBeans Palette and drag it onto your TopComponent. You will see view as you can see in the screenshot. The reason is because I’ve changed the model property of the JTable. Just select the JTable and switch to your property view under the NetBeans Palette.  There you will find an entry ‚model‘. Click the gray button on the second column in that row. A new window will pop up which allows you to enter the custom code. Enter the following: this.model.

Now switch to the Source view of your TopComponent by selecting the corresponding button on the upper left corner of your TopComponent. Here you should add all the functionality which I’ve implemented in my project. Please have a look on my Windows Live SkyeDrive Share. There you will find my version of this application.

To sum up the provided functionality of my project: In the TopComponent I’ve implemented an inner class which provides the data model for my JTable. You will find it at the end of the TopComponent class. I’ve called it ‚AlbumTableModel‘. This model will be filled if the TopComponent opens up. You’ll find the appropriate code in the overridden method ‚componentOpened()‘.

In this method all data will be selected from the database and mapped into the model. Then an event will be thrown that the JTable has changed. The model to which the data is mapped has been declared and instantiated in the top section of the class.

  • private AlbumTableModel model = new AlbumTableModel();

This model is the model which you previously have customized in the JTable’s property view. When the application starts up and the TopComponent is opened, the JTable will show the data from the Album-relation of the Java DB. Then I’ve implemented the following three methods:

  • newAlbumActionPerformed(ActionEvent evt)
  • editAlbumActionPerformed(ActionEvent evt)
  • deleteAlbumActionPerformed(ActionEvent evt)

These operations provide the functionality for each of the buttons of you TopComponent. Please have a look at the code. There you will find something like:

  • AlbumEditDialog.editAlbum(this.model.getRow(this.albums.getSelectedRow()));

The AlbumEditDialog is a simple JPanel but the implementation of the panel is part of the next post. You can add the three mentioned methods to the actionperformed() method of the buttons by right clicking the buttons and select ‚Events/Action/actionPerformed‘. This method will automatically be opened and you just need to call the corresponding operation e.g. ‚New..‘ => newAlbumActionPerformed().

Sum up

In this post we added the Java Class which provides the main database communication (DataModel), the Model Classes (Album, Genre) and the view (AlbumTopComponent). Next time I’ll show you how to add the JPanel (AlbumEditdialog) and what the application should look like and how it should work after finishing this tutorial.

Cheers

Über sageniuz

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

2 Antworten zu How to integrate Java DB into a NetBeans RCP | Part 2

  1. Ricardo schreibt:

    Thank you for this tuturial.I will try to use it well, but i think I will need some help.Thanks

  2. Ricardo schreibt:

    Thank you for this tuturial.I will try to use it well, but i think I will need some help.Thanks

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