Thursday, June 12, 2008

Swinglets 0.1 Beta Released

I have just released the first beta version of the Swinglets project on

This project is a collection of Java Swing components developed by me. In this release, only two components JClock and JRuler is provided. I have few other components in development which will be released soon. This is not a stable release, so expect lots of bug and an unstable API. By sharing this project with the community, I intend to build and grow this library.

Users are requested to use this library and send your feedback. Even better, join this project and submit your improvements.

Please note that this library depends on the jscience library from This library is not included with this project. You need to separately place it in the classpath.

Thursday, April 17, 2008

Integrating JavaDB with Netbeans modules - Series 3

In this last part of the series, I will show how to save and retrieve data from our embedded database.

  1. Create a new class called DBConect under the package org.embedded.db. Replace with the following code:
    1. package org.embedded.db;
    2. import java.sql.Connection;
    3. import java.sql.DriverManager;
    4. import java.sql.ResultSet;
    5. import java.sql.SQLException;
    6. import java.sql.Statement;
    7. import java.util.Properties;
    8. import org.openide.util.Exceptions;

    9. public class DBConect {

    10. Connection con = null;

    11. public DBConect() {
    12. Properties prop = System.getProperties();
    13. prop.put("derby.system.home",
      System.getProperty("user.home") + "/" + ".mesh");
    14. try {
    15. //start the database and get a connection
    16. con = DriverManager.
      getConnection("jdbc:derby:mesh_db", prop);
    17. } catch (SQLException e) {
    18. Exceptions.printStackTrace(e);
    19. }
    20. }

    21. public ResultSet executeQuery(String sql) throws SQLException {
    22. Statement statement = con.createStatement();
    23. return statement.executeQuery(sql);
    24. }

    25. public int executeUpdate(String sql) throws SQLException {
    26. Statement statement = con.createStatement();
    27. return statement.executeUpdate(sql);
    28. }
    29. }
  2. Now you can use this class to query the database or save data to the database using the two functions provided. This is a simple class. You can extend/modify it to do more complicated tasks.

Friday, April 11, 2008

Integrating JavaDB with Netbeans modules - Series 2

In my last post, we created MyDbModule project. In this part, I will tell you how to start the embedded database when our module gets loaded.

  1. Expand the Source Packages folder of the embedded-db project. Right-click on org.embedded.db package and select New > Java Class.Name the class as Startup. Then click on Finish.
  2. This will be our startup class when the module is loaded. To make that happen we need to do two things. First, extend it with the NetBeans API class ModuleInstall. ModuleInstall exposes two methods for child classes: restored() and closed(). restored() is called when the module is loaded for the first time. and closed() is called when the module is unloaded. To use this API, add two module dependencies to the module. Right click on embedded-db project and select Properties. Select Libraries. On the right hand side, click Add Dependency... Select Module System API and Utilities API and then clcik Ok. The two APIs will be added to the project. Then click OK again on the Project Properties Dialog box. Second, open the Module manifest file ( from the Important Files folder of the module. Add the following line to it:

    OpenIDE-Module-Install: org/embedded/db/Startup.class

    This will register our class as the startup class when the module is loaded.

  3. Now replace the Startup code with the following code:

    1. package org.embedded.db;
    2. import java.sql.Connection;
    3. import java.sql.DriverManager;
    4. import java.sql.SQLException;
    5. import java.util.Properties;
    6. import org.openide.modules.ModuleInstall;
    7. import org.openide.util.Exceptions;

    8. public class Startup extends ModuleInstall {

    9. @Override
    10. public void restored() {
    11. super.restored();
    12. Properties prop = System.getProperties();
    13. prop.put("derby.system.home",
      System.getProperty("user.home") + "/" + ".mydb");
    14. Connection con = null;
    15. try {
    16. //start the database and get a connection
    17. con = DriverManager.
      getConnection("jdbc:derby:my_db", prop);
    18. } catch (SQLException ex) {
    19. if (ex.getErrorCode() == 40000) {
    20. //database does not exist. create it now
    21. try {
    22. con = DriverManager.
      getConnection("jdbc:derby:my_db;create=true", prop);
    23. //create the schema
    24. } catch (SQLException ex1) {
    25. }
    26. }
    27. } finally {
    28. try {
    29. if (con != null && !con.isClosed()) {
    30. con.close();
    31. }
    32. } catch (SQLException ex) {
    33. }
    34. }
    35. }

    36. @Override
    37. public void close() {
    38. try {
    39. super.close();
    40. DriverManager.getConnection("jdbc:derby:;shutdown=true");
    41. } catch (SQLException ex) {
    42. if (!ex.getSQLState().equals("XJ015")) {
    43. Exceptions.printStackTrace(ex);
    44. }
    45. }
    46. }
    47. }

    In line 13, we call the base class restored() function to perform the default operation. Then we customize according to our requirements. In line 15, we set the JavaDB home directory where the database will be stored, which is the user's home directory under '.mydb' directory. In line 19, we try to connect to our embedded database with the name 'my_db'. First time, as the database is non-existent, it will throw an exception which will be caught by our handler. In line 24, we create the database by using the special connection string:


    In line 25, we can create our database tables as required.

    To shutdown the database when our application is closed, we override the closed() method. Shutting down the JavaDB engine is the same as getting connection except we pass the 'shutdown=true' parameter with the connection string as shown in line 43. Note the ':;' characters after 'jdbc:derby'. While shutting down, JavaDB throws an SQLException with SQL state code 'XJ015'. We need to ignore this exception which is done in line 45.

    That's it. We have created a module which loads and/or creates our database on startup and shuts it down when the application is closed.

    In the next and final blog, I will show how to connect to the database and save/retrieve data.

Wednesday, April 9, 2008

Integrating JavaDB with Netbeans modules - Series 1

Here, I outline the series of steps to integrate JavaDB a.k.a Apache Derby with your Netbeans module. JavaDB acts as the embedded database for our Netbeans module. The network server features of JavaDB will not be utilized. I will also show how to configure the database so that it is stored in a custom location rather than the default.

Note: In trying out this guide, it is assumed that you are familiar with the Netbeans Platform and related concepts such as module, suite, etc. This guide is based on the latest Netbeans 6.1 Beta platform.
  1. Create a module suite with the name 'MyDbModule'. You can give any name you want. If you want to add JavaDB to an existing module suite project, skip this step.

  2. Right click on the project and select Properties. Select Libraries. In the Platform Modules section, uncheck all modules except platform8. We don't want other modules for now and if necessary, they can be added later.Click Ok when done.
  3. Add a new module to this suite. (If you already have a module, skip this step)
    1. Expand the suite tree node.
    2. Right click on Modules node and select Add New... You will see the following dialog:
    3. Type the name of the module as 'embedded-db'. Choose a location if required. Click Next. You will now see the following dialog:
    4. Type Code Name Base as 'org.embedded.db'. Keep the rest as default. You may change the module display name.
    5. Click Finish.
    6. Your new module is added to the suite 'MyDbModule'.
  4. Now add a dependency for JavaDB to this new/existing module. JavaDB is installed with Java SE 6. If you do not already have JavaDB, please install it first.
    1. Expand 'embedded-db' node.
    2. Right-click on Libraries and select 'Add New Library...'
    3. Browse for the JavaDB jar files. For a default installation in Linux, you will find it in /opt/sun/javadb/lib. For a default Windows installation, it will be in C:/Program Files/Sun/JavaDB/lib. Select derby.jar and then click Next.
    4. Give the project name as 'JavaDBLib'. Then click next.
    5. Set code name base as 'org.embedded.javadb'. Leave the rest as it is. Then click Finish. The library is added to the suite as well as a dependent library to 'embedded-db' project.
So far, we have created a new module suite, added a module and a JavaDB library wrapper to it. In the next part, we will dive into coding and see how to start up our database in our own location . Also, we will connect to the database using Java Persistence API and do some simple queries.