passing a blob as parameter on a java method using Oracle JVM
Hey I'm trying to find a way to get this running. I am using the oracle database 10g where I have blobs store开发者_运维知识库d in a table. I want to be able to read out and pass a blob to a java method in my java code. I loaded my java class in my database via loadjava. The table where I store my blobs is set up, too.
This is my java class and the method I'd like to pass a BLOB
import java.lang.*;
import java.sql.*;
import oracle.sql.*;
public class Test
{
public static void getWidth(BLOB myBlob) throws Exception
{
System.out.println(myblob.length());
}
};
And this is my Java Stored Procedure (Wrapper) in PL/SQL
CREATE OR REPLACE PROCEDURE testmethod (p_blob IN BLOB)
AS LANGUAGE JAVA
NAME 'Test.getWidth(oracle.sql.BLOB)';
It loads the java class into the database and my wrapper compiles and is stored as well.
When I want to run execute testmethod(testphoto.jpg);
it gives me the error: 'testphoto.jpg must be declared'
Any advice to get this running? Thanks for your time.
This is my PL/SQL BLock from my procedure testmethod:
DECLARE
P_FILE VARCHAR2(200);
P_BLOB BLOB;
BEGIN
P_FILE := NULL;
P_BLOB := NULL;
TESTMETHOD(
P_FILE => P_FILE,
P_BLOB => P_BLOB
);
END;
testphoto.jpg
is just the name of a file that you want to pass to the method. You need the contents of that file as a blob to be passed into your procedure. You will need code to actually load the file contents into a blob variable or pull it from a table if it is already in the database. Then you will pass that as the second argument to your procedure.
If you wanted to test it, you can create a temp table and load it with some blob data like so:
create table blobtest (filecontents blob);
insert into blobtest values (utl_raw.cast_to_raw('Test Data'));
Then, you can run some pl/sql from sqlplus to pull that data and pass it to your procedure:
declare
temp blob;
filename varchar2(200) := 'Test';
begin
select filecontents
into temp
from blobtest;
testmethod(filename, temp);
end;
/
Of course, if the data was in a table already and you wanted the length of a blob, you could use the dbms_lob
function getLength
like so:
select dbms_lob.getLength(filecontents) from blobtest;
精彩评论