previous section demonstrated how the CREATE OR REPLACE AND COMPILE command is used to deploy the Java code. The CREATE OR REPLACE AND COMPILE command is useful when you would like to create a Java stored procedure from a small piece of Java code. However, if your Java code includes several Java classes, using the loadjava and dropjava command-line utilities included in Oracle Database 10g is a better choice. You can run these utilities by using the following commands:
loadjava –resolve –user <UserName>/<Password> <Java Classes or
Libraries in jar files>
dropjava –resolve –user <UserName>/<Password> <Java Classes or
Libraries in jar files>
However, you need to properly set up the Java CLASSPATH before you run these utilities, which can be complicated in some environments. To simplify this, you can use Oracle JDeveloper 10g to perform this deployment.
Using the previous example, you can create a Java class in a file called XSDDBBuilder.java and create a Loadjava and Java Stored Procedures deployment profile by right-clicking on the project name in Oracle JDeveloper and selecting New. In the pop-up dialog box, you then need to select General | Deployment Profiles | Loadjava and Java Stored Procedures. After inputting the deployment filename, click OK, and the dialog box will ask you to specify the classes and libraries that need to be loaded to the Oracle JVM. You can select Java classes and libraries under the Files Groups | Project Output | Filters option. However, before you can select the libraries in Filters, you need to add the directories storing the libraries by clicking the Add button in the dialog box under the Files Groups | Project Output | Contributor option. For example, if you want to load a new version of xmlparserv2.jar into Oracle JVM, you can add the $ORACLE_HOME/lib directory to the Contributor option. You then need to select the Java classes and libraries in the Filter option.
After the deployment file has been created, you can compile and load the Java classes into the database by right-clicking on the filename and selecting Deploy. You can deploy the Java classes to any Oracle database connections that you have set up in Oracle JDeveloper, as discussed in Chapter 13.
Note | Make sure you use xmlparserv2.jar in the $ORACLE_HOME/lib directory when compiling the Java classes in Oracle JDeveloper because it is the library that is loaded into Oracle JVM by default. Otherwise, you need to make sure the libraries used both inside and outside the Oracle database are consistent. Any inconsistency will result in ORA-29532: Java Call Terminated by Uncaught Java Exception: java.lang.NoSuchMethodError. |
Using Oracle JDeveloper 10g, you now do not have to set up Java CLASSPATH when loading the Java classes into the Oracle database. Additionally, you can use Oracle JDeveloper to create PL/SQL specifications. You just need to right-click on the name of the created deployment profile of the Java stored procedure and choose Add Stored Procedures. You can select any available static Java methods to create respective Java stored procedures. Selecting the build() method in the oracle.xml.sample.schema.XSDDBBuilder class as an example, you can set the properties for the PL/SQL procedure as shown in Figure 27-1.
Figure 27-1: SQL call specification for a Java stored procedure
In this example, the PL/SQL xsd_build(xsd IN VARCHAR2) specification is created. After you click OK, you will see the name, xsd_build, listed under the deployment file. To review the generated PL/SQL code, shown next, right-click on the procedure name and choose Preview SQL Statement:
CREATE OR REPLACE FUNCTION xsd_build(xsd IN VARCHAR2) RETURN VARCHAR2
AUTHID CURRENT_USER
AS LANGUAGE JAVA NAME
'oracle.xml.sample.schema.XSDDBBuilder.build(oracle.sql.CHAR)
return java.lang.String';
By using Oracle JDeveloper 10g, you can avoid the typographical errors, which may be problematic when debugging Java stored procedures.