Search This Blog

Loading...

Friday, November 11, 2011

How to Call C and Java from PLSQL

Call C
csum
int csum(int a, int b)
{

return (a+b);

}
gcc -c -fPIC csum.c -o csum.o
gcc -shared -Wl,-soname,csum.so.1 -o csum.so.1.0.1 csum.o
mv csum.so.1.0.1 /oracle/product/11.2.0/dbhome_1/lib
ln -sf /oracle/product/11.2.0/dbhome_1/lib/csum.so.1.0.1 /oracle/product/11.2.0/dbhome_1/lib/csum.so

ln -sf /oracle/product/11.2.0/dbhome_1/lib/csum.so.1.0.1 /oracle/product/11.2.0/dbhome_1/lib/csum.so.1
create or replace library calc_sum as '/oracle/product/11.2.0/dbhome_1/lib/csum.so';
/
create or replace function getCSUM( a binary_integer,b binary_integer) return binary_integer
as language c
library calc_sum
name "csum";
/
select getCSUM(10,2) from dual;
Call Java

java

public class JSUM
{
public static int sum ( int a , int b){return a+b;}
}


[oracle@db11g myC]$ /oracle/product/11.2.0/dbhome_1/jdk/bin/javac JSUM.java
[oracle@db11g myC]$ loadjava -user hr/hr JSUM.class


 ALTER JAVA CLASS "JSUM"
    RESOLVER (("C:\app\Administrator\product\11.2.0\dbhome_1\jdk\bin\*" pm)(* public))
   RESOLVE;

create or replace function javaSUM( a number,b number) return number
as
language java
name 'JSUM.sum(int,int) return int';
/

select javaSUM(1,2) from dual;