We usually use ps/SQL to write stored procedures for oracle. From oracle 8, we can have another option: Java. Starting from 8.0, oracle had a built-in jvm with built-in support for java. You can do anything that java can do in oracle. You can even operate db2 in it. This gives oracle great scalability. You can develop a trigger to send an instant message to the Administrator's msn whenever necessary.
The good news is over. Now it is bad news: ps/SQL is a procedural language, and it basically does not have the concept of polymorphism. the java method used by oracle must be declared as static, therefore, you cannot use the dynamic features of java in oracle, such as interfaces and reflection. However, this does not prevent you from using java for sex.
Let's take a simple hello world as an example. I think any experienced java programmer can use this example to derive other strange applications.
First, create a java class. You can use jbuilder/eclipse to compile the class, or you can directly create it in the oracle console. Let's not talk about the former. Let's look at the latter method.
Start SQL plus and execute the following command:
Create or replace and compile java source named "hello_sp"
/**
* Create a java class for the oracle Stored Procedure
* @ Author bromon
**/
Package org. bromon. oracle;
Public class Hello
{
Public static String say (String name)
{
Return "hello," + name;
}
}
Java to create.
Then, import the class into a function in oracle and execute the following command:
Create or replace function hello_sp (name varchar2) return varchar2
As language java name
Org. bromon. oracle. Hello. say (java. lang. String) return java. lang. String;
Function created
Now you can call this function and execute:
Select hello (bromon) from dual;
Returned results: Hello, bromon
Note that our java method has no parameters, for example:
Public static String say ()
{
Return "hello ";
}
When creating a function, the function name should not have an extension number: hello_sp
Otherwise, a compilation error is reported for the function.
If your java class is written in IDE, you only need to load the compiled class file in oracle:
Start the enterprise manage console, find "Source Type" under "solution", right-click "java class", select "load java", and select the corresponding class file.