This is an
excerpt from the book
Advanced PL/SQL: The Definitive Reference by Boobal Ganesan.
The Java classes and source files are
stored as schema objects in the database as Java is a
natively part of the Oracle database. Thus, the EXTPROC
agent, listener, or any environment parameters are not used
as a means of communication between Java and Oracle, where
it is taken care by the Java Virtual Machine (JVM). JVM
interprets the Java compilation and executes the run-time
object code. This need for JVM makes “Java in Oracle” a bit
slower than other languages in Oracle.
As Oracle natively supports Java, there
is no need for the shared library as a third wheel
instead, there is a
Java Shared Library called as LIBUNIT, which is
automatically loaded by the EXTPROC agent.
Java understands Oracle easily so there
are no big type conversions needed between the PL/SQL
wrapper and the Java class/ source file. As Java uses the
Java pool in the SGA part of the memory, there is a
possibility for a
Java program to use up the SGA memory resulting in memory
issues.
The below steps are to be undertaken to
implement Java class/ source files in PL/SQL.
Loading Java Class/ Source Files in
Oracle
A Java class or a source file can be
loaded into the Oracle database so that it is made
immediately available for the PL/SQL program accessing it.
Whenever a Java class or a source file is loaded in the form
of OS files, the Oracle JVM library manager loads Java
class files and resources from the OS location into
the RDBMS libunits.
These two processes to load Java class
or source file are explained below,
CREATE JAVA Syntax
We can either create a Java class object
or a source file object which in-turn creates the
appropriate classes by itself.
The prototype for creating a Java source
file object is shown below,
CREATE OR REPLACE [AND COMPILE| RESOLVE] java
[source NAMED | class] <Primary_name> [AuthID
[CURRENT_USER | DEFINER]]
[AS <Java_source_file>] | [USING BFILE
(<Directory_name>, <File_name>)]
·
AND COMPILE |
RESOLVE clause compiles the Java class/ source file upon
its creation. Compile and Resolve are synonymous keywords.
If this clause is omitted, the Java object may be in INVALID
state after its creation and we may need to execute
an “Alter Java [class
| source] <Primary_name> COMPILE;” to make it VALID.
·
SOURCE NAME
clause is used to specify that a Java source file is used
for the load.
·
CLASS clause is
used to specify that a Java class is used for the load.
·
PRIMARY_NAME
clause is the name for the Java object that is to be
created.
·
AS clause lets
us use the Java source file in character form.
·
USING clause
lets us use the Java source file stored in a location.
Using
Java Source File
In this method, a Java source code can
be used in the CREATE JAVA statement to create a schema
object without needing us to convert them into a “.class"
file externally. Upon successful creation of the Java source
object, the classes available in the Java source code
are automatically
created as Java class objects in the database.
In the below example, a Java source with
one class file for squaring the input number is created as a
Java source object in the database.
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED SQUARE
AS
public class SQUARE {
public static int SQUARE (int var1) {
return var1*var1;}}
We can check the created Java source and
class files in the USER_OBJECTS data dictionary as below,
SELECT object_id,
object_name,
object_type,
created,
status
FROM user_objects
WHERE object_name='SQUARE';
OBJECT_ID
|
OBJECT_NAME
|
OBJECT_TYPE
|
CREATED
|
STATUS
|
93493
|
SQUARE
|
JAVA SOURCE
|
20-OCT-16
|
VALID
|
93495
|
SQUARE
|
JAVA CLASS
|
20-OCT-16
|
VALID
|
Using Java Class File
By using this method, a single Java
class file can be created as a database object.
%
Note: Javac command can be used in the
command prompt for converting “.java” file
into a “.class” file.
|
As a first step, a directory has to be
created to store the class file as shown below,
CREATE DIRECTORY java_class_dir AS 'C:\Program
Files\Java';
Upon placing the class file in the above
directory, a Java class object can be created in the
database by executing the below script.
CREATE AND COMPILE JAVA CLASS USING BFILE
(java_class_dir, 'SQUARE.class');
We can check the created Java class file
in the USER_OBJECTS data dictionary as below,
SELECT object_id,
object_name,
object_type,
created,
status
FROM user_objects
WHERE object_name='SQUARE';
OBJECT_ID
|
OBJECT_NAME
|
OBJECT_TYPE
|
CREATED
|
STATUS
|
93495
|
SQUARE
|
JAVA CLASS
|
20-OCT-16
|
VALID
|
LOADJAVA
Utility
The
loadjava utility can be used for uploading a Java
class file into the database using the command prompt. The
steps to be followed are,
1)
Firstly, the location of the
Java file has to be set as the current location in the
command window as shown below,
C:\Users\Boobal Ganesan>cd C:\Users\Boobal
Ganesan\Desktop\JavaPrograms
C:\Users\Boobal Ganesan\Desktop\JavaPrograms>
2)
Next, the
loadjava utility can
be used for the Java
file which has to be uploaded as shown below,
C:\Users\Boobal Ganesan\Desktop\JavaPrograms>loadjava
-user c##/oracle SQUARE.java
Upon completion, there will not be any
message confirmation whether the load has been successful or
not. The data dictionary USER_OBJECTS can be browsed through
to find the loaded Java file.
SELECT object_id,
object_name,
object_type,
created,
status
FROM user_objects
WHERE object_name='SQUARE';
OBJECT_ID
|
OBJECT_NAME
|
OBJECT_TYPE
|
CREATED
|
STATUS
|
93493
|
SQUARE
|
JAVA SOURCE
|
20-OCT-16
|
INVALID
|
93495
|
SQUARE
|
JAVA CLASS
|
20-OCT-16
|
INVALID
|
Here, the loaded objects are invalidated
during their upload. They can be made valid by executing the
below statements.
ALTER java source square compile;
|
Need to learn to program with PL/SQL?
For complete notes on programming in PL/SQL, we
recommend the book
Advanced PL/SQL: The Definitive Reference by Boobal Ganesan.
This is a complete book on PL/SQL with
everything you need to know to write efficient and
complex PL/SQL code. |
|
|
|
Oracle Training from Don Burleson
The best on site
"Oracle
training classes" are just a phone call away! You can get personalized Oracle training by Donald Burleson, right at your shop!
|
|
|
|
|
Burleson is the American Team
Note:
This Oracle
documentation was created as a support and Oracle training reference for use by our
DBA performance tuning consulting professionals.
Feel free to ask questions on our
Oracle forum.
Verify
experience!
Anyone
considering using the services of an Oracle support expert should
independently investigate their credentials and experience, and not rely on
advertisements and self-proclaimed expertise. All legitimate Oracle experts
publish
their Oracle
qualifications.
Errata?
Oracle technology is changing and we
strive to update our BC Oracle support information. If you find an error
or have a suggestion for improving our content, we would appreciate your
feedback. Just
e-mail:
and include the URL for the page.
Copyright © 1996 - 2020
All rights reserved by
Burleson
Oracle ®
is the registered trademark of Oracle Corporation.
|
|