Storing Objects and Classes
Page 5 of 5

Storing Classes in the Database

NOTE: This is an advanced topic.

You can store application logic in a database and then have Cloudscape and your application load classes from the database instead of from the user's class path. Application logic, which can be used by both Cloudscape and your application, includes Java class files and other resources. In the JBMSTours application, the City class is an example of application-side logic that is used by both Cloudscape and the application.

Storing application code simplifies application deployment, because it reduces the potential for problems with a user's class path.

To store classes in a database, you first must package up the class files (and any resources used by the application) into a jar file. Then you load the jar file into the database with a special utility and enable Cloudscape to load classes from a database. Finally, you use a special bootstrap mechanism for executing the application so that it uses the Cloudscape class loader.

In this section, you will package up the class files in the JBMSTours package into a jar file and try to run the application using the Cloudscape class loader. You will no longer need %CLOUDSCAPE_INSTALL%/demo/programs/tours in your class path to run the JBMSTours application, because all application logic will now be stored in the database.

Create a Jar File for the JBMSTours Package and Subpackages

You can use the standard jar utility to create a jar file for the JBMSTours package.

  1. Open a command window.
  2. Change directories to %CLOUDSCAPE_INSTALL%/demo/programs/tours.

    You must be in the directory that contains the package in order to preserve the package names within the jar file.

  3. Issue the jar command to package up all the class files in JBMSTours into a jar file called tours.jar. tours.jar will be the jar file's physical name (it will have another name within Cloudscape).

    This command jars up all .class files in the JBMSTours package directory and all subpackages.

    jar cf tours.jar JBMSTours/*.class JBMSTours/serializabletypes/*.class JBMSTours/inserters/*.class JBMSTours/vti/jdbc1_2/*.class JBMSTours/triggers/*.class JBMSTours/aggregates/*.class

NOTE: The jar utility recognizes forward slashes on all platforms.

  1. Move this jar file to the your_tutorial_home directory.
  2. Change directories to the your_tutorial_home directory.

Add the Jar File to the Database as ToursLogic

You will add the jar file to the toursDB database, giving it a Cloudscape name when you do so. This name will be ToursLogic. You will modify it with APP, the schema name.

Cloudview makes it easy to work with jar files.

  1. Start Cloudview as described in Start Cloudview in the Home Directory. Then open a connection to toursDB as described in Open a Connection to toursDB.
  2. Select the Jar Files icon.
  3. Choose New->Jar file from the Edit menu.
  4. Type ToursLogic in the Name box.
  5. APP is the correct schema.
  6. Click on the navigate button to find tours.jar, then select it and click Open.
  7. Make sure the add to classpath button is selected.

    This enables database class loading for the jar file by setting a database property.

  8. Click OK.

    A copy of the jar file is now stored in the database under name ToursLogic.

  9. Exit Cloudview.

Alter the Value of Your Class Path

When loading classes, Cloudscape always looks in the class path first, then in the jar files stored in the database. To make sure Cloudscape loads files from the database, remove the %CLOUDSCAPE_INSTALL%/demo/programs/tours directory from your class path.

  1. Open the setclasspath script that you learned about in Customize Your setclasspath Script and that you have been running periodically throughout this tutorial.
    • Windows users should open setclasspath.bat.
    • UNIX users should open setclasspath.sh.
    • UNIX users who are working in cshell should open setclasspath.csh.
  2. Alter the script by removing %CLOUDSCAPE_INSTALL%/demo/programs/tours from the command that sets your class path.
  3. Save the script.
  4. Run the script as instructed in Running the Script.
  5. Verify that you actually removed the appropriate package by trying to run one of the applications:

    java JBMSTours.GenerateReport

    You should get the dreaded "can't find class" message, but this time, that's what you want!

    Can't find class JBMSTours/GenerateReport

Run JBMSTours.GenerateReport with Class Loading from the Database

Once you enable database class loading (which you did in Add the Jar File to the Database as ToursLogic) and remove loadable classes from the class path, Cloudscape will load the classes from the database. Cloudscape provides an application bootstrap program that forces Cloudscape class loading for your application. The program is called COM.cloudscape.util.DBClassLoad.

  1. Start JBMSTours.GenerateReport with the bootstrap program:

    java -Dcloudscape.system.home= your_tutorial_system
        COM.cloudscape.util.DBClassLoad jdbc:cloudscape:toursDB
        JBMSTours.GenerateReport