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.

No comments:

Post a Comment