3

Hey I'm trying to find a way to get this running. I am using the oracle database 10g where I have blobs stored 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;

1 Answer 1

3

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;

Sign up to request clarification or add additional context in comments.

7 Comments

Unfortunately I got this error: *Cause: An attempt was made to execute a method in a Java class that had not been previously and cannot now be compiled or resolved successfully. *Action: Adjust the call or make the class resolvable.
what do I have to put in ('Test Data')
@DannyRe: You don't, that was just a string value I was converting to a blob for a simple test.
I put all your code into my db, but didnt work. im so frustrated :( I created a directory in my db. Loaded a jpg into my blob table. its the one i want to send into my java method. I appreciate your help!
Could you help me there? Damn I need it working by tomorrow... :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.