Jeg ønsker at lægge en JPG-fil som en BLOB ned i min Personal Oracle 8.1 via en Java-applikation som anvender DBMS_LOB-package (altså ikke OCI).
Hvor skal jeg placere min JPG-fil på harddisken for at kunne kopiere dens indhold over i Oracle-databasen? Der er noget med en buffer, som jeg ikke har kunnet få til at fungere.
Har nogen et fungerende kodesegment i Java til dette?
Hvordan undgår jeg ved udtrækning af data, at jeg ikke blot modtager en reference, men derimod en byte-array el. lign.?
Using Java Classes loaded into the database from Java Applications --------------------------------------------------------------------
You could have Java Classes loaded into the database using \"loadjava\" or you could have them stored into a BLOB column of a user defined table.
The Java classes loaded into the database by any of the above methods could be used by a Java application by creating a user defined ClassLoader, similar to the ClassLoader used by browsers for retreiving & executing Applet classes from a remote system.
Advatages of storing Java classes in the database -- a. Could be used by different clients. b. The Classes could be secured using database userid & password.
Explained below are the code for using Classes stored in the database using both of the above methods.
1. Classes stored into a user defined table.
-- Create a table with a BLOB column for storing classes
-- For Loading the java classes into the table, create a DIRECTORY object to specify the OS directory from where the java classes are to be upload
SQL>create directory TEMPDIR as \'c:\\temp\';
Copy the java classes to be uploaded into the above directory (ie c:\\temp)
Create the procedure mentioned below
SQL>create or replace procedure upload_java_class (class varchar2) as ablob blob; abfile bfile ; -- Get a pointer to the file. amount integer; asize integer; class_name varchar2(2000); cursor1 integer; rows_processed integer; begin cursor1 := dbms_sql.open_cursor; dbms_sql.parse(cursor1,\'delete from java_classes where classname = :x\', dbms_sql.v7); dbms_sql.bind_variable(cursor1, \'x\', class); rows_processed := dbms_sql.execute (cursor1); dbms_sql.parse(cursor1,\'insert into java_classes values (:x, empty_blob())\', dbms_sql.v7); dbms_sql.bind_variable(cursor1, \'x\', class); rows_processed := dbms_sql.execute (cursor1); class_name := class || \'.class\'; abfile:=bfilename(\'TEMPDIR\', class_name); dbms_sql.parse(cursor1,\'select classdata from java_classes where classname = :x for update\',dbms_sql.v7); dbms_sql.define_column(cursor1,1,ablob); dbms_sql.bind_variable(cursor1, \'x\', class); rows_processed := dbms_sql.execute (cursor1); if dbms_sql.fetch_rows (cursor1) > 0 then dbms_sql.column_value (cursor1, 1, ablob); end if; dbms_sql.close_cursor(cursor1); dbms_lob.fileopen(abfile); asize := dbms_lob.getlength(abfile); dbms_lob.loadfromfile(ablob, abfile, asize); asize := dbms_lob.getlength(ablob); commit; exception when others then dbms_output.put_line(\'An exception occurred\'); dbms_output.put_line(sqlcode || sqlerrm); end;
Upload the Class file using the above procedure
SQL> exec upload_java_class(\'Tester\');-- To Upload Tester.class under c:\\temp SQL> select classname from java_classes; -- Should show Tester
--- Create a ClassLoader to retreive the BLOB contents for a particular Class. ( Attached below is the code for retreving the contents from the java_classes table shown above ). You need to have the JDBC drivers in the CLASSPATH ( $ORACLE_HOME\\jdbc\\lib\\classes111.zip )
--- You could now use the above ClassLoader to load the classes in your Java applications.
eg : To Use the \"Tester\" class loaded into the scott schema.
public class CLTester { public static void main (String args[]) throws Exception { DatabaseClassLoader loader = new DatabaseClassLoader(\"scott\",\"tiger\", \"orcl8i\"); Class c = loader.loadClass (\"Tester\",true); // Call the empty construtor of the loaded class Object tester = c.newInstance(); } }
2. Classes loaded into the Oracle JServer using loadjava.
--- Use the loadjava executable to load the class file
c:\\temp> loadjava -oci8 -user scott/tiger@orcl8i -resolve Tester.class ( To load Tester.class into scott schema).
Check if the class is loaded & is valid
SQL>connect scott/tiger@orcl8i SQL>select object_name, status from user_objects where object_name like \'Tester%\' and object_type like \'JAVA CLASS\';
--- Create a Class Loader for loading the Class
Create the procedure retreive_class in the database, this would be used by the Class Loader to retreive the Class contents
SQL>create or replace procedure retreive_class(v1 IN varchar2, b1 IN OUT blob) as begin dbms_lob.createtemporary(b1,TRUE); dbms_java.export_class(v1, b1); end;
Create the ClassLoader class similar to the above.The only change would be in the loadClassdata method.Use the loadClassdata method as given below
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.