Before you can call Java methods from within your PL/SQL programs you will need to do the following:
Install a Java Development Kit ( JDK(TM)) 1.1.5 or above (check the Oracle documentation for the latest information about version support; some features of Java 1.2 are not, at the time of publication, supported inside Oracle).
Build your Java classes and code elements, and then compile them into .class and .jar files.
Set privileges on your Oracle schema.
You will need to install a Java Development Kit ( JDK) 1.1.5 or above. You can do this by downloading a JDK from the Javasoft web site; Oracle also provides a JDK with Oracle8 i . Here is the Javasoft URL:
http://www.javasoft.com/products/index.html |
You will need to make sure to set the CLASSPATH so that the Java compiler (javac) can locate any references to your classes -- and to the Oracle classes. Visit the following URL to get more information about CLASSPATH:
http://www.javasoft.com/products/jdk/1.2/docs/tooldocs/win32/classpath.html#env var |
You can see an online version of the Java language specification at:
http://java.sun.com/docs/books/jls/html/index.html |
Many PL/SQL developers (including the author) have never worked with an object-oriented language of any kind, so coming up to speed on Java can be a bit of a challenge. In the short time in which I have studied and used Java, I have come to these conclusions:
It doesn't take long to get a handle on the syntax needed to build simple classes in Java.
It's not very difficult at all to start leveraging Java inside PL/SQL.
Writing real object-oriented applications using Java requires significant rethinking for PL/SQL developers.
It would be impossible to offer a comprehensive primer on Java in this chapter. There are many (many, many, many) books available on various aspects of Java, and a number of them are excellent. I would recommend that you check out the following:
Exploring Java , by Patrick Niemeyer and Joshua Peck (O'Reilly & Associates). One of more than a dozen O'Reilly books on Java, this text is a nice introduction, from the standpoint of both syntax and concepts.
The Java Programming Language, Second Edition, by Ken Arnold and James Gosling (Addison Wesley). James Gosling is the creator of Java, so you'd expect the book to be helpful. It is. Written in clear, simple terms, it gives you a strong grounding in the language.
Thinking in Java , by Bruce Eckel (Prentice Hall). A very readable and creative approach to explaining object-oriented concepts. If you liked the feel of my Oracle PL/SQL Programming , you will definitely enjoy Thinking in Java .
In Section 9.9, "Examples , later in this chapter, as I demonstrate how to call Java methods from within PL/SQL, I will also take you step by step through the creation of relatively simple classes. You will find that, in many cases, this discussion will be all you need to get the job done.
Oracle8 i has created two new roles to support Java security. For many Java-based operations within the database, you will not have to work with these roles. If, on the other hand, you want to interact with the operating system (to access or modify operating system files, for example), you need to be granted one of the following roles:
JAVASYSPRIV JAVAUSERPRIV
You grant these roles as you would any other database role. For example, if I want to allow SCOTT to perform any kind of Java-related operation, I would issue this command from a SYSDBA account:
GRANT JAVASYSPRIV TO SCOTT;
If I want to place some restrictions on what the user can do with Java, I might execute this grant instead:
GRANT JAVAUSERPRIV TO SCOTT;
Here's one example of the difference between the two roles. To create a file through Java, I need the JAVASYSPRIV role; to read or write a file, I only need the JAVAUSERPRIV role. See the Oracle documentation for more details, including a table listing the different check methods of Java and which role is required to run those methods.
When the Aurora JVM is initialized, it installs an instance of java.lang.SecurityManager, the Java Security Manager. Each Oracle user has a dynamic ID , which will correspond to the session owner when you access the Java methods from within PL/SQL.
If a user lacking the sufficient privileges granted by one of these roles tries to execute an illegal operation, then the JVM will throw the java.lang.SecurityException. Here is what you would see in SQL*Plus:
ORA-29532: Java call terminated by uncaught Java exception: java.lang.SecurityException
When you run Java methods inside the database, different security issues can arise, particularly when interacting with the server-side filesystem or other operating system resources. Oracle follows one of the following two rules when checking I/O operations:
If the dynamic ID has been granted JAVASYSPRIV, then Security Manager allows the operation to proceed.
If the dynamic ID has been granted JAVAUSERPRIV, then Security Manager follows the same rules that apply to the PL/SQL UTL_FILE package to determine if the operation is valid. In other words, the file must be in a directory (or subdirectory) specified by the UTL_FILE_DIR parameter in the database initialization file.
Copyright (c) 2000 O'Reilly & Associates. All rights reserved.