Basic steps for Oracle to invoke Java classes
1. Write Java code that can be used directly in Java code, class file or Jar package
2. There are two ways to import well-written Java code into an Oracle database: One is to use the Loadjava command, and the other is to write it when you write PL/sql
3. Write stored procedures, functions to encapsulate Java code to implement calls to Java functionality after
4. Call the written stored procedure and Oracle functions
Loadjava Command Introduction
For more information about the Loadjava parameters, you can use the: loadjava-help command to get detailed help. The main parameters used in this article are-u (for input user name and password),-V (for output detailed feedback information),-resolve (for Java files not compiled, can be compiled with resolve),-f (This command I do not use in the example, it means force, Indicates that the Java class is forced to be imported again, regardless of whether it was previously imported. Loadjava can realize the import of class file, jar file, resource file, property file, and when importing class file, you can use the following command:
Loadjava–u Username/userpassword–v Filename.class
When importing Java files, you can use the following command:
Loadjava–u Username/userpassword–v-resolve Filename.java
When importing a jar file, you can use the following command:
Loadjava–u Username/userpassword–v-resolve Filename.jar
Example: Using Oracle to invoke Java full step
The following class is an algorithm class that expands the 32-bit MD5 to 64-bit MD5, and we will call the methods in this class in the Java function to implement the 32-bit
Extension of the MD5 algorithm
Step one: Write Java code: (Note the parameters write the full class name)
Create or replace and compile Java source named MAC32TOMAC64 as
public class MAC32TOMAC64 {
public static byte[] Hex2byte (java.lang.String hex)
Throws Exception {
if (Hex.length ()% 2! = 0) {
throw new Exception ();
}
char[] arr = Hex.tochararray ();
Byte[] B = new Byte[hex.length ()/2];
for (int i = 0, j = 0, L = hex.length (), I < L; i++, J + +) {
Java.lang.String swap = "" + arr[i++] + arr[i];
int byteint = java.lang.Integer.parseInt (swap) & 0xFF;
B[J] = new Java.lang.Integer (byteint). Bytevalue ();
}
return b;
}
public static java.lang.String Byte2hex (byte[] b) {
Java.lang.StringBuffer hs = new Java.lang.StringBuffer ();
Java.lang.String stmp;
for (int i = 0; i < b.length; i++) {
Stmp = Java.lang.Integer.toHexString (B[i] & 0xFF). toUpperCase ();
if (stmp.length () = = 1) {
Hs.append ("0"). Append (STMP);
} else {
Hs.append (STMP);
}
}
return hs.tostring ();
}
public static byte[] Lengthen (byte[] byteArray) {
Byte[] result = new byte[bytearray.length * 2];
for (int i = 0; i < bytearray.length; i++) {
Result[2 * I] = bytearray[i];
int value = (Bytearray[i] + bytearray[i = = Bytearray.length-1 0:i + 1]) & 0xFF;
result[2 * i + 1] = new Java.lang.Integer (value). Bytevalue ();
}
return result;
}
public static java.lang.String To64mac (java.lang.String req32mac)
Throws Exception {
byte[] bytes = Hex2byte (REQ32MAC);
if (bytes.length = = 16) {
Return Byte2hex (Lengthen (bytes));
} else {
throw new Exception ("");
}
}
}
Step two: Use the Loadjava command to import Java into the Orale database, and after you type the password, you will have import information.
Loadjava–u username–v-resolve MAC32TOMAC64. java
You can also verify that the import was successful by using the following statement
SELECT * from user_objects u where u.object_name= ' MAC32TOMAC64 '
Step three: Write a stored procedure or function to achieve a later call, if a Java method does not return a value, then encapsulated into a stored procedure, if there is a return value, it is encapsulated into a function, here is the example encapsulated into a function, the stored procedure is similar, no longer allocations. Here varchar corresponding Java string type must write the full class name, otherwise it will be an errorCREATE OR REPLACE FUNCTION To64mac (R in VARCHAR2) RETURN VARCHAR2 as
LANGUAGE JAVA NAME ' Mac32tomac64.to64mac (java.lang.String) return String ';
You can test the results directly in PL/SQL
Methods for Oracle to invoke stored procedures and functions developed by the Java class