6.4 Loading SQLJ Programs into the Database
The loadjava command-line utility may be used to load Java class files into the database. The class files may be loaded individually, or packed into a JAR file using the jar command-line utility and loaded together. The syntax for the loadjava command-line utility is provided in Appendix B. This section uses only a few simple options of the loadjava command-line utility, but they should be enough for most of your needs. First, I describe how to load the class files individually, and then I show how to pack the classes into a JAR file and load that file into the database.
6.4.1 Loading Individual Class Files
The loadjava command-line utility can be used to load one or more Java class files into the database. The following loadjava command loads the three previously generated class files into the fundamental_user schema. (Note that this is one long command that just happens to wrap to four separate lines in the printed book.)
loadjava -user fundamental_user/fundamental_password -resolve ServerExample1.class ServerExample1_SJProfileKeys.class ServerExample1_SJProfile0.class
The loadjava command-line utility will connect to the database using the username and password specified in the -user option, and then load the specified class files into the database schema. The connection to the database uses the Oracle JDBC OCI driver by default. The -resolve option is used to resolve class references immediately, rather than waiting until each class is invoked for the first time. This is optional, but it saves some time when the class methods are first run.
6.4.2 Loading JAR Files
A JAR file, or Java Archive, is a collection of class files placed into one file using the jar utility. The jar command-line utility may also compress the class files as it builds the JAR file. The JAR file can then be loaded into the database using the loadjava command-line utility. The following command places the three class files referenced in the previous example into a compressed JAR file named ServerExample1.jar:
jar -cvf ServerExample1.jar ServerExample1*.class
The -c option indicates that a new archive is to be created, -v produces verbose output, and -f specifies the name of the JAR file. Compression of JAR files is done by default.
| As of Oracle8i Version 8.1.6, you can load compressed JAR files into the database; if you are using an older version, you must use the -0 (dash-zero) option in the jar command-line utility to prevent compression of the class files. Older versions of Oracle do not accept compressed JAR files. |
|
After creating a JAR file, you load it into the database using the loadjava command-line utility. The following command loads the ServerExample1.jar file into the fundamental_user schema:
loadjava -user fundamental_user/fundamental_password -resolve ServerExample1.jar
If your database is running on a remote machine, you can specify a database URL for the remote database. By default, the Oracle JDBC OCI8 driver (the OCI driver in Oracle9i) is used to make the connection to the database. The following example illustrates the use of a database URL and the use of the Oracle JDBC Thin driver (the -thin option specifies the Thin driver to access the database):
loadjava -thin -user fundamental_user/fundamental_password@localhost:1521:ORCL -resolve ServerExample1.jar
Although this example uses localhost to specify the local machine, you can specify a remote machine in the URL. See Chapter 3 for full details on database URLs and the Thin driver. After this loadjava command has completed, it will have created a number of Java class objects in the database schema.
6.4.3 The Database Java Class Objects
The database Java class objects created by the loadjava command-line utility are identified by either a full name or a short name. The full name is the fully qualified name of the Java package and class with the dots (.) replaced by forward slashes (/). For example, if the Java package and class name is my.first.class.MyClass, then the full name of the database object would be my/first/class/MyClass. If the full name exceeds 31 characters, contains illegal characters, or contains characters that cannot be converted to the database character set, then a short name is automatically generated by the database to uniquely identify the object. The object is then stored in the database with the associated short name.
| Because I didn't use a package name in ServerExample1.sqlj, you won't see a package name in the examples in this section. |
|
To examine the details of your database Java class objects, you can query the user_objects database view. The query in the following example selects the object name and type for the Java classes owned by the current user:
SQL> SELECT object_name, object_type 2 FROM user_objects 3 WHERE object_type = 'JAVA CLASS'; OBJECT_NAME OBJECT_TYPE ------------------------------ ------------- ServerExample1 JAVA CLASS ServerExample1_SJProfile0 JAVA CLASS ServerExample1_SJProfileKeys JAVA CLASS
As you can see from this example, the three class files were successfully loaded into the database. However, you cannot call the methods in the ServerExample1 class directly from the database; you must first create PL/SQL wrappers around the Java methods. This is the publishing process that I referred to earlier in the chapter. You have to do this because the Oracle database doesn't directly run your Java methods: it indirectly calls your Java methods using PL/SQL, and the methods are then run by the integrated JServer JVM. Think of it as the wrappers providing the bridge between the database and the JServer JVM.
6.4.4 Removing Java Classes from the Database
Now that you've seen how to load classes into the database, you should learn how to remove those classes when necessary. The dropjava command-line utility is used to drop Java objects from the database. Any objects loaded into the database using loadjava should also be dropped using dropjava to ensure that the objects are removed from the database correctly. The syntax for dropjava is provided in Appendix B.
The dropjava command-line utility accepts .sqlj, .class, .jar, and .ser files, and drops the database Java class objects associated with them. The database Java class objects should be dropped using dropjava in the same way that they were loaded using loadjava. For example, to drop the objects loaded earlier in this chapter that were associated with the file ServerExample1.jar, use the following command:
dropjava -user fundamental_user/fundamental_password ServerExample1.jar