Oracle interMedia Java Classes User's Guide and Reference Release 9.0.1 Part Number A88785-01 |
|
This chapter provides full-length examples of user-defined classes using interMedia Java Classes. Sample SQL scripts that demonstrate how to set up a schema on your database server are also included.
This code will not necessarily match the code shipped as AudioExample.java, DocumentExample.java, ImageExample.java, or VideoExample.java with the interMedia Java Classes installation. If you want to run an example on your system, use the files provided with the interMedia Java Classes installation; do not attempt to compile and run the code presented in this chapter.
The audio example (including AudioExample.sql and AudioExample.java) contains user-defined methods that use SQL, JDBC, and interMedia Java Classes APIs to perform the following operations:
Example 2-1 shows the complete contents of the AudioExample.sql sample file.
set echo on -- PLEASE change system password connect system/manager drop user AUDIOUSER cascade; [1] create user AUDIOUSER identified by AUDIOUSER; grant connect,resource to AUDIOUSER identified by AUDIOUSER; [2] connect AUDIOUSER/AUDIOUSER [3] CREATE TABLE TAUD(n NUMBER, aud ORDSYS.ORDAUDIO); -- -- Note - the OrdAudio.init method was added in interMedia 8.1.7. -- If you are running against an older release of interMedia and the -- Oracle database, you will have to modify the following INSERT statements -- to use the OrdAudio default constructor. -- [4] INSERT INTO TAUD VALUES(1, ORDSYS.ORDAudio.init( )); INSERT INTO TAUD VALUES(2, ORDSYS.ORDAudio.init( )); INSERT INTO TAUD VALUES(3, ORDSYS.ORDAudio.init( )); commit;
The SQL statements in AudioExample.sql perform the following operations:
See Oracle interMedia User's Guide and Reference for more information on the init method.
Section 2.1.2.1 through Section 2.1.2.8 show the methods contained in the AudioExample.java sample file.
Example 2-2 shows the main( ) method.
public static void main (String args[ ]){ byte[ ] ctx = new byte[4000]; OracleConnection con = null; try { AudioExample tk = new AudioExample( ); [1] con = tk.connect( ); //Include the following line only if you are running //an Oracle 8.1.7 database or later. //If you are running a database server prior to 8.1.7, //the call will fail. [2] OrdMediaUtil.imCompatibilityInit(con); [3] tk.loadDataFromFile(con); tk.extractProperties(con); tk.printProperties(con); tk.otherMethods(con); tk.loadDataFromStream(con); tk.loadDataFromByteArray(con); [4] con.commit( ); [5] con.close( ); System.out.println("Done."); } [6] catch (Exception e) { try { System.out.println("Exception : " + e); con.close( ); } catch(Exception ex) { System.out.println("Close Connection Exception : " + ex); } } }
The code in the main( ) method performs the following operations:
Section 2.1.2.2 through Section 2.1.2.8 will provide information on the methods called from the main( ) method in the order in which they are called, not in the order they appear in AudioExample.java.
Example 2-3 shows a user-defined method named connect( ), which makes a connection from the application to the database.
public OracleConnection connect( ) throws Exception { String connectString; [1] Class.forName ("oracle.jdbc.driver.OracleDriver"); [2] connectString = "jdbc:oracle:oci8:@"; [3] OracleConnection con = (OracleConnection)DriverManager.getConnection (connectString,"AUDIOUSER","AUDIOUSER"); [4] con.setAutoCommit(false); return con; }
The connect( ) method performs the following operations:
Example 2-4 shows a user-defined method named loadDataFromFile( ), which uses the interMedia loadDataFromFile( ) method to populate the application object with media data.
public void loadDataFromFile(OracleConnection con) { try { [1] Statement s = con.createStatement( ); [2] OracleResultSet rs = (OracleResultSet) s.executeQuery ("select * from TAUD where n = 1 for update "); int index = 0; [3] while(rs.next( )){ [4] index = rs.getInt(1); [5] OrdAudio audObj = (OrdAudio) rs.getCustomDatum (2, OrdAudio.getFactory( )); [6] audObj.loadDataFromFile("testaud.dat"); [7] audObj.getDataInFile("output1.dat"); System.out.println("************AFTER getDataInFile "); [8] System.out.println(" getContentLength output : " + audObj.getContentLength( )); [9] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update taud set aud = ? where n = " + index); stmt1.setCustomDatum(1,audObj); stmt1.execute( ); stmt1.close( ) ; } System.out.println("loading successful"); } [10] catch(Exception e) { System.out.println("exception raised " + e); System.out.println("loading unsuccessful"); } }
The loadDataFromFile( ) method performs the following operations:
Example 2-5 shows a user-defined method named extractProperties( ), which sets the properties in the application object.
public void extractProperties(OracleConnection con){ byte[ ] ctx[ ] = new byte [4000][1]; try { [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet) s.executeQuery ("select * from TAUD where n = 1 for update"); int index = 0; while(rs.next( )){ index = rs.getInt(1); OrdAudio audObj = (OrdAudio) rs.getCustomDatum (2, OrdAudio.getFactory( )); [2] audObj.setProperties(ctx); System.out.println("set Properties called"); [3] if(audObj.checkProperties(ctx)){ System.out.println("checkProperties called"); System.out.println("setProperties successful"); System.out.println("checkProperties successful"); System.out.println("extraction successful"); } else{ System.out.println("checkProperties called"); System.out.println("extraction not successful"); System.out.println("checkProperties successful"); } [4] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update taud set aud = ? where n = " + index); stmt1.setCustomDatum(1,audObj); stmt1.execute( ); stmt1.close( ) ; } rs.close( ); s.close( ); } [5] catch(Exception e) { System.out.println("exception raised " + e); System.out.println("extract properties unsuccessful"); } }
The extractProperties( ) method performs the following operations:
Example 2-6 shows a user-defined method named printProperties( ), which prints the attributes of the application object to the screen.
public void printProperties(OracleConnection con){ try { [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet) s.executeQuery("select * from TAUD where n = 1 "); int index = 0; while(rs.next( )) { index = rs.getInt(1); OrdAudio audObj = (OrdAudio) rs.getCustomDatum (2, OrdAudio.getFactory( )); [2] System.out.println("format: " + audObj.getFormat( )); System.out.println("mimeType: " + audObj.getMimeType( )); System.out.println("encoding: " + audObj.getEncoding( )); System.out.println("numberOfChannels: " + audObj.getNumberOfChannels( )); System.out.println("samplingRate: " + audObj.getSamplingRate( )); System.out.println("sampleSize: " + audObj.getSampleSize( )); System.out.println("compressionType : " + audObj.getCompressionType( )); System.out.println("audioDuration: " + audObj.getAudioDuration( )); System.out.println("description: " + audObj.getDescription( )); } } [3] catch(Exception e){ System.out.println("exception raised " + e); System.out.println("print proerties unsuccessful"); } }
The printProperties( ) method performs the following operations:
Example 2-7 shows a user-defined method named otherMethods( ), which attempts to use the processSourceCommand( ) method.
public void otherMethods(OracleConnection con){ byte[ ] ctx[ ] = {new byte[4000]}; byte[ ] res[ ] = {new byte[20]}; [1] int suc = 1; try { [2] Statement s1 = con.createStatement( ); OracleResultSet rs1 = (OracleResultSet) s1.executeQuery ("select * from TAUD where n = 1 for update "); int index1 = 0; while(rs1.next( )) { index1 = rs1.getInt(1); OrdAudio audObj = (OrdAudio) rs1.getCustomDatum (2, OrdAudio.getFactory( )); [3] try { byte[ ] pSRes = audObj.processSourceCommand(ctx, "", "", res); suc = 0; } [4] catch (Exception e) { System.out.println("Expected Exception raised in processSourceCommand(...)" ); } [5] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update taud set aud = ? where n = " + index1); stmt1.setCustomDatum(1,audObj); stmt1.execute( ); stmt1.close( ) ; } rs1.close( ); s1.close( ); } [6] catch(Exception e){ System.out.println("Exception raised " ); } [7] if(suc == 1) System.out.println("other methods successful"); else System.out.println("other methods unsuccessful"); }
The otherMethods( ) method performs the following operations:
Example 2-8 shows a user-defined method named loadDataFromStream( ), which uses the interMedia loadDataFromInputStream( ) method to load media data into the application object.
public void loadDataFromStream(OracleConnection con){ try { [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet) s.executeQuery ("select * from TAUD where n = 2 for update "); int index = 0; while(rs.next( )){ index = rs.getInt(1); OrdAudio audObj = (OrdAudio) rs.getCustomDatum (2, OrdAudio.getFactory( )); [2] FileInputStream fStream = new FileInputStream("testaud.dat"); [3] audObj.loadDataFromInputStream(fStream); [4] audObj.getDataInFile("output2.dat"); [5] fStream.close( ); System.out.println("************AFTER getDataInFile "); [6] System.out.println(" getContentLength output : " + audObj.getContentLength( )); [7] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update taud set aud = ? where n = " + index); stmt1.setCustomDatum(1,audObj); stmt1.execute( ); stmt1.close( ); } System.out.println("load data from stream successful"); } [8] catch(Exception e) { System.out.println("exception raised " + e); System.out.println("load data from stream unsuccessful"); } }
The loadDataFromStream( ) method performs the following operations:
Example 2-9 shows a user-defined method named loadDataFromByteArray( ), which uses the interMedia loadDataFromByteArray( ) method to load media data into the application object.
public void loadDataFromByteArray(OracleConnection con){ try { [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet) s.executeQuery ("select * from TAUD where n = 3 for update "); int index = 0; while(rs.next( )) { index = rs.getInt(1); OrdAudio audObj = (OrdAudio) rs.getCustomDatum (2, OrdAudio.getFactory( )); [2] File ff = new File("testaud.dat"); int fileLength = (int) ff.length( ); byte[ ] data = new byte[fileLength]; [3] FileInputStream fStream = new FileInputStream("testaud.dat"); [4] fStream.read(data,0,fileLength); [5] audObj.loadDataFromByteArray(data); [6] fStream.close( ); [7] audObj.getDataInFile("output3.dat"); [8] byte[ ] resArr = audObj.getDataInByteArray( ); [9] System.out.println("byte array length : " + resArr.length); [10] FileOutputStream outStream = new FileOutputStream ("output4.dat"); [11] outStream.write(resArr); [12] outStream.close( ); [13] InputStream inpStream = audObj.getDataInStream( ); int length = 32768; byte[ ] tempBuffer = new byte[32768]; [14] int numRead = inpStream.read(tempBuffer,0,length); try { [15] outStream = new FileOutputStream("output5.dat"); [16] while (numRead != -1) { [17] if (numRead < 32768) { length = numRead; outStream.write(tempBuffer,0,length); break; } [18] else outStream.write(tempBuffer,0,length); [19] numRead = inpStream.read(tempBuffer,0,length); } } [20] finally { outStream.close( ); inpStream.close( ); } System.out.println("************AFTER getDataInFile "); [21] System.out.println("getContentLength output : " + audObj.getContentLength( )); [22] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update taud set aud = ? where n = " + index); stmt1.setCustomDatum(1,audObj); stmt1.execute( ); stmt1.close( ) ; } } [23] catch(Exception e) { System.out.println("exception raised " + e); System.out.println("load data from byte array unsuccessful"); } }
The loadDataFromByteArray( ) method performs the following operations:
The OrdDoc example (including DocumentExample.sql and DocumentExample.java) contains user-defined methods that use SQL, JDBC, and interMedia Java Classes APIs to perform the following operations:
Example 2-10 shows the complete contents of the DocumentExample.sql sample file.
set echo on --PLEASE change system password connect system/manager drop user DOCUSER cascade; [1] create user DOCUSER identified by DOCUSER ; grant connect,resource to DOCUSER identified by DOCUSER; [2] connect DOCUSER/DOCUSER [3] CREATE TABLE TDOC(n NUMBER, doc ORDSYS.ORDDOC); [4] INSERT INTO TDOC VALUES(1, ORDSYS.ORDDoc.init( )); INSERT INTO TDOC VALUES(2, ORDSYS.ORDDoc.init( )); INSERT INTO TDOC VALUES(3, ORDSYS.ORDDoc.init( )); commit;
The SQL statements in DocumentExample.sql perform the following operations:
Example 2.2.2.1 through Example 2.2.2.8 show the methods defined in the DocumentExample.java sample file.
Example 2-11 shows the contents of the main( ) method.
public static void main (String args[ ]){ byte[ ] ctx = new byte[4000]; OracleConnection con = null; try { DocumentExample tk = new DocumentExample( ); [1] con = tk.connect( ); [2] OrdMediaUtil.imCompatibilityInit(con); [3] tk.loadDataFromFile(con); tk.extractProperties(con); tk.printProperties(con); tk.loadDataFromStream(con); tk.otherMethods(con); tk.loadDataFromByteArray(con); [4] con.commit( ); [5] con.close( ); System.out.println("Done."); } [6] catch (Exception e) { try { System.out.println("Exception : " + e); con.close( ); } catch(Exception ex) { System.out.println("Close Connection Exception : " + ex); } } }
The code in the main( ) method performs the following operations:
Section 2.2.2.2 through Section 2.2.2.8 will provide information on the methods called from the main( ) method in the order in which they are called, not in the order they appear in DocumentExample.java.
Example 2-12 shows a user-defined method named connect( ), which makes a connection from an application to a database.
public OracleConnection connect( ) throws Exception{ String connectString; [1] Class.forName ("oracle.jdbc.driver.OracleDriver"); [2] connectString = "jdbc:oracle:oci8:@"; [3] OracleConnection con = (OracleConnection) DriverManager.getConnection(connectString,"DOCUSER","DOCUSER"); [4] con.setAutoCommit(false); return con; }
The connect( ) method performs the following operations:
Example 2-13 shows a user-defined method named loadDataFromFile( ), which uses the interMedia loadDataFromFile( ) method to populate the application object with media data.
public void loadDataFromFile(OracleConnection con){ try { [1] Statement s = con.createStatement( ); [2] OracleResultSet rs = (OracleResultSet) s.executeQuery ("select * from TDOC where n = 1 for update"); int index = 0; [3] while(rs.next( )){ [4] index = rs.getInt(1); [5] OrdDoc docObj = (OrdDoc) rs.getCustomDatum(2, OrdDoc.getFactory( )); [6] docObj.loadDataFromFile("testaud.dat"); [7] docObj.getDataInFile("output1.dat"); System.out.println("************AFTER getDataInFile "); [8] System.out.println("getContentLength output: " + docObj.getContent( ).length( )); [9] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update tdoc set doc = ? where n = " + index); stmt1.setCustomDatum(1,docObj); stmt1.execute( ); stmt1.close( ); } System.out.println("loading successful"); } [10] catch(Exception e) { System.out.println("exception raised " + e); System.out.println("loading unsuccessful"); } }
The loadDataFromFile( ) method performs the following operations:
Example 2-14 shows a user-defined method named extractProperties( ), which sets the properties in the application object.
public void extractProperties(OracleConnection con){ byte[ ] ctx[ ] = new byte [4000][1]; try { [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet) s.executeQuery("select * from TDOC where n = 1 for update"); int index = 0; while(rs.next( )){ index = rs.getInt(1); OrdDoc docObj = (OrdDoc)rs.getCustomDatum(2, OrdDoc.getFactory( )); [2] docObj.setProperties(ctx,false); System.out.println("setProperties successful"); [3] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update tdoc set doc = ? where n = " + index); stmt1.setCustomDatum(1,docObj); stmt1.execute( ); stmt1.close( ) ; } rs.close( ); s.close( ); } [4] catch(Exception e) { System.out.println("exception raised " + e); System.out.println("extract prop unsuccessful"); } }
The extractProperties( ) method performs the following operations:
Example 2-15 shows a user-defined method named printProperties( ), which prints the attributes of the application object to the screen.
public void printProperties(OracleConnection con){ try { [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet)s.executeQuery("select * from TDOC where n = 1 "); int index = 0; while(rs.next( )){ index = rs.getInt(1); OrdDoc docObj = (OrdDoc) rs.getCustomDatum(2, OrdDoc.getFactory( )); [2] System.out.println("format: " + docObj.getFormat( )); System.out.println("mimetype: " + docObj.getMimeType( )); System.out.println("contentlength: " + docObj.getContentLength( )); } } [3] catch(Exception e) { System.out.println("exception raised " + e); System.out.println("print properties unsuccessful"); } }
The printProperties( ) method performs the following operations:
Example 2-16 shows a user-defined method named loadDataFromStream( ), which uses the interMedia loadDataFromInputStream( ) method to load media data into the application object.
public void loadDataFromStream(OracleConnection con){ try { [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet)s.executeQuery("select * from TDOC where n = 2 for update "); int index = 0; while(rs.next( )){ index = rs.getInt(1); OrdDoc docObj = (OrdDoc) rs.getCustomDatum(2, OrdDoc.getFactory( )); [2] FileInputStream fStream = new FileInputStream ("testaud.dat"); [3] docObj.loadDataFromInputStream(fStream); [4] docObj.getDataInFile("output2.dat"); [5] fStream.close( ); System.out.println("************AFTER getDataInFile "); [6] System.out.println("getContentLength output: " + docObj.getContent( ).length( )); [7] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update tdoc set doc = ? where n = " + index); stmt1.setCustomDatum(1,docObj); stmt1.execute( ); stmt1.close( ) ; } System.out.println("load data from stream successful"); } [8] catch(Exception e) { System.out.println("exception raised " + e); System.out.println("load data from stream unsuccessful"); } }
The loadDataFromStream( ) method performs the following operations:
Example 2-17 shows a user-defined method named otherMethods( ), which attempts to use the processSourceCommand( ) method.
public void otherMethods(OracleConnection con){ byte[ ] ctx[ ] = {new byte[4000]}; byte[ ] res[ ] = {new byte[20]}; [1] int suc = 1; try { [2] Statement s1 = con.createStatement( ); OracleResultSet rs1 = (OracleResultSet) s1.executeQuery("select * from TDOC where n = 1 for update "); int index1 = 0; while(rs1.next( )){ index1 = rs1.getInt(1); OrdDoc docObj = (OrdDoc) rs1.getCustomDatum(2, OrdDoc.getFactory( )); [3] try { byte[ ] pSRes = docObj.processSourceCommand(ctx, "", "", res); suc = 0; } [4] catch (Exception e) { System.out.println("Expected Exception raised in processSourceCommand(...)" ); } [5] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update tdoc set doc = ? where n = " + index1); stmt1.setCustomDatum(1,docObj); stmt1.execute( ); stmt1.close( ) ; } rs1.close( ); s1.close( ); } [6] catch(Exception e){ System.out.println("Exception raised " ); } [7] if(suc ==1) System.out.println("other methods successful"); else System.out.println("other methods unsuccessful"); }
The otherMethods( ) method performs the following operations:
Example 2-18 shows a user-defined method named loadDataFromByteArray( ), which uses the interMedia loadDataFromByteArray( ) method to load media data into the application object.
public void loadDataFromByteArray(OracleConnection con){ try { [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet) s.executeQuery("select * from TDOC where n = 3 for update "); int index = 0; while(rs.next( )){ index = rs.getInt(1); OrdDoc docObj = (OrdDoc) rs.getCustomDatum(2, OrdDoc.getFactory( )); [2] File ff = new File("testaud.dat"); int fileLength = (int) ff.length( ); byte[ ] data = new byte[fileLength]; [3] FileInputStream fStream = new FileInputStream ("testaud.dat"); [4] fStream.read(data,0,fileLength); [5] docObj.loadDataFromByteArray(data); [6] fStream.close( ); [7] docObj.getDataInFile("output3.dat"); [8] byte[ ] resArr = docObj.getDataInByteArray( ); [9] System.out.println("byte array length: " + resArr.length); [10] FileOutputStream outStream = new FileOutputStream ("output4.dat"); [11] outStream.write(resArr); [12] outStream.close( ); [13] InputStream inpStream = docObj.getDataInStream( ); int length = 32768; byte[ ] tempBuffer = new byte[32768]; [14] int numRead = inpStream.read(tempBuffer,0,length); try { [15] outStream = new FileOutputStream("output5.dat"); [16] while(numRead != -1) { [17] if (numRead < 32768) { length = numRead; outStream.write(tempBuffer,0,length); break; } [18] else outStream.write(tempBuffer,0,length); [19] numRead = inpStream.read(tempBuffer,0,length); } } [20] finally { outStream.close( ); inpStream.close( ); } System.out.println("************AFTER getDataInFile "); [21] System.out.println("getContentLength output: " + docObj.getContent( ).length( )); [22] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update tdoc set doc = ? where n = " + index); stmt1.setCustomDatum(1,docObj); stmt1.execute( ); stmt1.close( ) ; } } [23] catch(Exception e) { System.out.println("exception raised " + e); System.out.println("loadData from byte array unsuccessful"); } }
The loadDataFromByteArray( ) method performs the following operations:
The image example (including ImageExample.sql and ImageExample.java) contains user-defined methods that use SQL, JDBC, and interMedia Java Classes APIs to perform the following operations:
Example 2-19 shows the contents of ImageExample.sql.
set echo on -- Please Change system password. connect / as sysdba; drop user IMAGEUSER cascade; [1] grant connect,resource to IMAGEUSER identified by IMAGEUSER; -- Replace C:\Oracle\Ora' with your ORACLE HOME [2] create or replace directory ORDIMAGEDIR as 'C:\Oracle\Ora\ord\img\demo'; grant read on directory ORDIMAGEDIR to public with grant option; [3] connect IMAGEUSER/IMAGEUSER; [4] create table ordimagetab(id number, image ORDSYS.ORDImage, image2 ORDSYS.ORDImage); -- Note - the OrdImage.init method was added in interMedia 8.1.7. -- If you are running against an older release of interMedia and the -- Oracle database, you will have to modify the following INSERT statements -- to use the OrdImage default constructor. -- [5] insert into ordimagetab values (1, ORDSYS.ORDImage.init( ), ORDSYS.ORDImage.init( )); insert into ordimagetab values (2, ORDSYS.ORDImage.init( ), ORDSYS.ORDImage.init( )); insert into ordimagetab values (3, ORDSYS.ORDImage.init( ), ORDSYS.ORDImage.init( )); insert into ordimagetab values (4, ORDSYS.ORDImage.init( ), ORDSYS.ORDImage.init( )); [6] insert into ordimagetab values (5, ORDSYS.ORDImage.init('file','ORDIMAGEDIR','imgdemo.dat'), ORDSYS.ORDImage.init( )); insert into ordimagetab values (6, ORDSYS.ORDImage.init('file','ORDIMAGEDIR','imgdemo.dat'), ORDSYS.ORDImage.init( )); [7] insert into ordimagetab values (10, ORDSYS.ORDImage.init('file','ORDIMAGEDIR','cbrdemo1.dat'), ORDSYS.ORDImage.init( )); [8] insert into ordimagetab values (11, ORDSYS.ORDImage.init('file','ORDIMAGEDIR','cbrdemo2.dat'), ORDSYS.ORDImage.init( )); [9] create table sigtable(id number, sig ORDSYS.ORDImageSignature); [10] insert into sigtable values (10, ORDSYS.ORDImageSignature.init( )); insert into sigtable values (11, ORDSYS.ORDImageSignature.init( )); commit; set echo off exit;
The SQL statements in ImageExample.sql perform the following operations:
See Oracle interMedia User's Guide and Reference for more information on the init method.
Section 2.3.2.1 through Section 2.3.2.12 show the methods contained in the ImageExample.java sample file.
Example 2-20 shows the main( ) method.
public static void main (String args[ ]){ byte[ ] ctx = new byte[4000]; OracleConnection con = null; try{ ImageExample ie = new ImageExample( ); [1] con = ie.connect( ); //Include the following line only if you are running //an Oracle 8.1.7 database or later. //If you are running a database server prior to 8.1.7, //the call will fail. [2] OrdMediaUtil.imCompatibilityInit(con); [3] ie.setPropertiesExample(con); ie.displayPropertiesExample(con); ie.fileBasedExample(con); ie.streamBasedExample(con); ie.byteArrayBasedExample(con); ie.processExample(con); [4] ie.sigGeneration(con); ie.sigSimilarity(con); ie.imageMatchingScore(con); [5] con.commit( ); [6] con.close( ); System.out.println("Done."); } [7] catch (Exception e){ try{ System.out.println("Exception : " + e); con.close( ); } catch(Exception ex){ System.out.println("Close Connection Exception : " + ex); } } }
The code in the main( ) method performs the following operations:
Section 2.3.2.2 through Section 2.3.2.12 will provide information on the methods called from the main( ) method.
Example 2-21 shows a user-defined method named connect( ), which makes a connection from the application to the database.
public OracleConnection connect( ) throws Exception{ String connectString; [1] Class.forName ("oracle.jdbc.driver.OracleDriver"); [2] connectString = "jdbc:oracle:oci8:@"; [3] OracleConnection con = (OracleConnection)DriverManager.getConnection (connectString,"IMAGEUSER","IMAGEUSER"); [4] con.setAutoCommit(false); return con; }
The connect( ) method performs the following operations:
Example 2-22 shows a user-defined method named setPropertiesExample( ), which sets the properties in the application object.
public void setPropertiesExample(OracleConnection con){ try{ int index = 0; [1] Statement s = con.createStatement( ); [2] OracleResultSet rs = (OracleResultSet)s.executeQuery ("select * from ordimagetab where id = 5 for update"); [3] while(rs.next( )){ [4] index = rs.getInt(1); [5] OrdImage imgObj = (OrdImage)rs.getCustomDatum (2, OrdImage.getFactory( )); [6] imgObj.setProperties( ); System.out.println("set Properties called"); [7] if(imgObj.checkProperties( )){ System.out.println("checkProperties called"); System.out.println("setProperties successful"); System.out.println("checkProperties successful"); System.out.println("successful"); } else{ System.out.println("checkProperties called"); System.out.println("setProperties not successful"); System.out.println("checkProperties successful"); } [8] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update ordimagetab set image = ? where id = " + index); stmt1.setCustomDatum(1,imgObj); stmt1.execute( ); stmt1.close( ) ; } rs.close( ); s.close( ); } [9] catch(Exception e){ System.out.println("exception raised " + e); } }
The setPropertiesExample( ) method performs the following operations:
Example 2-23 shows a user-defined method named displayPropertiesExample( ), which prints the attributes of the application object to the screen.
public void displayPropertiesExample(OracleConnection con){ try{ int index = 0; [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet)s.executeQuery( "select * from ordimagetab where id = 5 "); while(rs.next( )){ index = rs.getInt(1); OrdImage imgObj = (OrdImage) rs.getCustomDatum(2, OrdImage.getFactory( )); [2] System.out.println("format : " + imgObj.getFormat( )); System.out.println("mimeType: " + imgObj.getMimeType( )); System.out.println("height: " + imgObj.getHeight( )); System.out.println("width: " + imgObj.getWidth( )); System.out.println("contentLength: " + imgObj.getContentLength( )); System.out.println("contentFormat: " + imgObj.getContentFormat( )); System.out.println("compressionFormat: " + imgObj.getCompressionFormat( )); System.out.println("source type: " + imgObj.getSourceType( )); System.out.println("source loc: " + imgObj.getSourceLocation( )); System.out.println("source name: " + imgObj.getSourceName( )); System.out.println("source : " + imgObj.getSource( )); [3] try{ String attrString = getAllAttributesAsString(imgObj); System.out.println(attrString); } [4] catch (Exception e){ System.out.println("Exception raised in getAllAttributesAsString:"); } System.out.println("successful"); } } [5] catch(Exception e) { System.out.println("exception raised " + e); } }
The displayPropertiesExample( ) method performs the following operations:
Example 2-24 shows a user-defined method named getAllAttributesAsString( ), which creates a String object that contains the values of the application object attributes.
public String getAllAttributesAsString (OrdImage imgObj) throws Exception{ [1] String attStr = imgObj.getSource( ) + " mimeType = " + imgObj.getMimeType( ) + ", fileFormat = " + imgObj.getFormat( ) + ", height = " + imgObj.getHeight( ) + ", width = " + imgObj.getWidth( ) + ", contentLength = " + imgObj.getContentLength( ) + ", contentFormat = " + imgObj.getContentFormat( ) + ", compressionFormat = " + imgObj.getCompressionFormat( ); [2] return attStr; }
The getAllAttributesAsString( ) method performs the following operations:
Example 2-25 shows a user-defined method named fileBasedExample( ), which uses the loadDataFromFile( ) method to load media data into the application object.
public void fileBasedExample(OracleConnection con){ try{ int index = 0; [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet)s.executeQuery( "select * from ORDIMAGETAB where id = 2 for update "); while(rs.next( )){ index = rs.getInt(1); OrdImage imgObj = (OrdImage) rs.getCustomDatum(2, OrdImage.getFactory( )); [2] imgObj.loadDataFromFile("imgdemo.dat"); [3] imgObj.setProperties( ); [4] imgObj.getDataInFile("fileexample.dat"); [5] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update ordimagetab set image = ? where id = " + index); stmt1.setCustomDatum(1,imgObj); stmt1.execute( ); stmt1.close( ) ; } System.out.println("successful"); } [6] catch(Exception e){ System.out.println("exception raised " + e); } }
The fileBasedExample( ) method performs the following operations:
Example 2-26 shows a user-defined method named streamBasedExample( ), which uses the loadDataFromInputStream( ) method to load media data into the application object.
public void streamBasedExample(OracleConnection con){ try{ int index = 0; [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet)s.executeQuery( "select * from ORDIMAGETAB where id = 3 for update "); while(rs.next( )){ index = rs.getInt(1); OrdImage imgObj = (OrdImage) rs.getCustomDatum(2, OrdImage.getFactory( )); [2] FileInputStream fStream = new FileInputStream ("imgdemo.dat"); [3] imgObj.loadDataFromInputStream(fStream); [4] fStream.close( ); [5] imgObj.setProperties( ); [6] InputStream inpStream = imgObj.getDataInStream( ); int length = 32300; byte[ ] tempBuffer = new byte[length]; [7] int numRead = inpStream.read(tempBuffer,0,length); FileOutputStream outStream=null; try{ [8] outStream = new FileOutputStream ("streamexample.dat"); [9] while(numRead != -1){ [10] if (numRead < length){ length = numRead; outStream.write(tempBuffer,0,length); break; } [11] else outStream.write(tempBuffer,0,length); [12] numRead = inpStream.read(tempBuffer,0, length); } } [13] finally{ if (outStream != null) outStream.close( ); inpStream.close( ); } [14] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update ordimagetab set image = ? where id = " + index); stmt1.setCustomDatum(1,imgObj); stmt1.execute( ); stmt1.close( ) ; } System.out.println("successful"); } [15] catch(Exception e){ System.out.println("exception raised " + e); } }
The streamBasedExample( ) method performs the following operations:
Example 2-27 shows a user-defined method named byteArrayBasedExample( ), which uses the loadDataFromByteArray( ) method to load media data into the application object.
public void byteArrayBasedExample(OracleConnection con){ try{ int index = 0; [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet)s.executeQuery ("select * from ORDIMAGETAB where id = 4 for update "); while(rs.next( )){ index = rs.getInt(1); OrdImage imgObj = (OrdImage) rs.getCustomDatum(2, OrdImage.getFactory( )); [2] File ff = new File("imgdemo.dat"); int fileLength = (int) ff.length( ); byte[ ] data = new byte[fileLength]; [3] FileInputStream fStream = new FileInputStream("imgdemo.dat"); [4] fStream.read(data,0,fileLength); [5] imgObj.loadDataFromByteArray(data); [6] fStream.close( ); [7] imgObj.setProperties( ); [8] byte[ ] resArr = imgObj.getDataInByteArray( ); [9] System.out.println("byte array length : " + resArr.length); [10] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update ordimagetab set image = ? where id = " + index); stmt1.setCustomDatum(1,imgObj); stmt1.execute( ); stmt1.close( ) ; } System.out.println("successful"); } [11] catch(Exception e){ System.out.println("exception raised " + e); } }
The byteArrayBasedExample( ) method performs the following operations:
Example 2-28 shows a user-defined method named processExample( ), which uses the process( ) and processCopy( ) methods to manipulate the media data in the application object.
public void processExample(OracleConnection con){ try{ int index1 = 0; [1] Statement s1 = con.createStatement( ); OracleResultSet rs1 = (OracleResultSet)s1.executeQuery ("select * from ORDIMAGETAB where id = 2 for update "); while(rs1.next( )){ index1 = rs1.getInt(1); OrdImage imgObj = (OrdImage) rs1.getCustomDatum(2, OrdImage.getFactory( )); [2] OrdImage imgObj2 = (OrdImage) rs1.getCustomDatum(3, OrdImage.getFactory( )); try{ [3] imgObj.processCopy("maxScale=32 32, fileFormat= GIFF", imgObj2); [4] imgObj.process("fileFormat=JFIF"); [5] System.out.println(getAllAttributesAsString (imgObj)); [6] System.out.println(getAllAttributesAsString(imgObj2)); } [7] catch (Exception e){ System.out.println("Exception raised in process" + e ); } [8] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update ordimagetab set image = ?, image2 = ? where id = " + index1); stmt1.setCustomDatum(1,imgObj); stmt1.setCustomDatum(2,imgObj2); stmt1.execute( ); stmt1.close( ) ; } rs1.close( ); s1.close( ); } [9] catch(Exception e){ System.out.println("Exception raised: " + e); } System.out.println("successful"); }
The processExample( ) method performs the following operations:
Example 2-29 shows a user-defined method named sigGeneration( ), which generates a signature for a given OrdImage object and stores it in the database.
public void sigGeneration(OracleConnection con){ byte[ ] ctx[ ] = new byte [4000][1]; try{ [1] int IMG_ID=10; int SIG_ID=11; int IMG_COL=2; int SIG_COL=2; [2] Statement get_image_obj = con.createStatement( ); Statement get_sig_obj = con.createStatement( ); [3] OracleResultSet get_image_obj_result = (OracleResultSet)get_image_obj.executeQuery ("select * from ordimagetab where id =" + IMG_ID + " for update"); OracleResultSet get_sig_result = (OracleResultSet) get_sig_obj.executeQuery("select * from sigtable where id =" + SIG_ID + " for update"); [4] get_image_obj_result.next( ); get_sig_result.next( ); [5] OrdImage img_obj = (OrdImage)get_image_obj_result.getCustomDatum (IMG_COL, OrdImage.getFactory( )); [6] OrdImageSignature img_sig = (OrdImageSignature) get_sig_result.getCustomDatum(SIG_COL, OrdImageSignature.getFactory( )); [7] img_obj.setProperties( ); [8] img_obj.importData(ctx); [9] img_sig.generateSignature(img_obj); [10] OraclePreparedStatement update_sig = (OraclePreparedStatement) con.prepareCall("update sigtable set sig = ? where id =" + SIG_ID); update_sig.setCustomDatum(1,img_sig); update_sig.execute( ); update_sig.close( ) ; [11] get_sig_result.close( ); get_image_obj_result.close( ); get_sig_obj.close( ); get_image_obj.close( ); System.out.println("image signature generation complete."); } [12] catch(Exception e){ System.out.println("exception raised " + e); System.out.println("image signature generation failed"); } }
The code in sigGeneration( ) performs the following operations:
Example 2-30 shows the imageMatchingScore( ) method, which generates signatures for two OrdImage objects and compares them based on different criteria.
public void imageMatchingScore(OracleConnection con){ byte[ ] ctx[ ] = new byte [4000][1]; try { [1] int IMG_ID1=10; int SIG_ID1=10; int IMG_ID2=11; int SIG_ID2=11; int IMG_COL=2; int SIG_COL=2; [2] Statement get_image_obj = con.createStatement( ); Statement get_sig_obj = con.createStatement( ); [3] OracleResultSet get_image_obj_result1 = (OracleResultSet) get_image_obj.executeQuery("select * from ordimagetab where id ="+ IMG_ID1 +" for update"); OracleResultSet get_sig_result1 = (OracleResultSet) get_sig_obj.executeQuery("select * from sigtable where id =" +SIG_ID1 + " for update"); OracleResultSet get_image_obj_result2 = (OracleResultSet) get_image_obj.executeQuery("select * from ordimagetab where id ="+ IMG_ID2 +" for update"); OracleResultSet get_sig_result2 = (OracleResultSet) get_sig_obj.executeQuery("select * from sigtable where id =" +SIG_ID2 + " for update"); [4] get_image_obj_result1.next( ); get_sig_result1.next( ); get_image_obj_result2.next( ); get_sig_result2.next( ); [5] OrdImage img_obj1 = (OrdImage) get_image_obj_result1.getCustomDatum(IMG_COL, OrdImage.getFactory( )); [6] OrdImageSignature img_sig1 = (OrdImageSignature) get_sig_result1.getCustomDatum( SIG_COL,OrdImageSignature.getFactory( )); [7] img_obj1.setProperties( ); [8] img_obj1.importData(ctx); [9] img_sig1.generateSignature(img_obj1); [10] OrdImage img_obj2 = (OrdImage)get_image_obj_result2. getCustomDatum(IMG_COL, OrdImage.getFactory( )); [11] OrdImageSignature img_sig2 = (OrdImageSignature) get_sig_result2.getCustomDatum( SIG_COL,OrdImageSignature.getFactory( )); [12] img_obj2.setProperties( ); [13] img_obj2.importData(ctx); [14] img_sig2.generateSignature(img_obj2); [15] float gscore = OrdImageSignature.evaluateScore(img_sig1, img_sig2,"color=1", con); System.out.println("score value (global color comparison):" + gscore); [16] float lscore = OrdImageSignature.evaluateScore(img_sig1, img_sig2,"color=1 location=1", con); System.out.println("Score value (local color comparison):" + lscore); [17] get_sig_result2.close( ); get_image_obj_result2.close( ); get_sig_result1.close( ); get_image_obj_result1.close( ); get_sig_obj.close( ); get_image_obj.close( ); } [18] catch(Exception e){ System.out.println("exception raised " + e); System.out.println("Image matching score computation failed"); } }
The code in the imageMatchingScore( ) method performs the following operations:
Example 2-31 shows the sigSimilarity( ) method, which compares the image signatures of two objects and determines if they match.
public void sigSimilarity(OracleConnection con){ byte[ ] ctx[ ] = new byte [4000][1]; try { [1] int IMG_ID1=10; int SIG_ID1=10; int IMG_ID2=11; int SIG_ID2=11; int IMG_COL=2; int SIG_COL=2; [2] double similarity_threshold= 20.0; [3] Statement get_image_obj = con.createStatement( ); Statement get_sig_obj = con.createStatement( ); [4] OracleResultSet get_image_obj_result1 = (OracleResultSet) get_image_obj.executeQuery("select * from ordimagetab where id ="+ IMG_ID1 +" for update"); OracleResultSet get_sig_result1 = (OracleResultSet) get_sig_obj.executeQuery("select * from sigtable where id =" +SIG_ID1 + " for update"); OracleResultSet get_image_obj_result2 = (OracleResultSet) get_image_obj.executeQuery("select * from ordimagetab where id ="+ IMG_ID2 +" for update"); OracleResultSet get_sig_result2 = (OracleResultSet) get_sig_obj.executeQuery("select * from sigtable where id =" +SIG_ID2 + " for update"); [5] get_image_obj_result1.next( ); get_sig_result1.next( ); get_image_obj_result2.next( ); get_sig_result2.next( ); [6] OrdImage img_obj1 = (OrdImage)get_image_obj_result1. getCustomDatum(IMG_COL, OrdImage.getFactory( )); [7] OrdImageSignature img_sig1 = (OrdImageSignature)get_sig_result1. getCustomDatum(SIG_COL,OrdImageSignature.getFactory( )); [8] img_obj1.setProperties( ); [9] img_obj1.importData(ctx); [10] img_sig1.generateSignature(img_obj1); [11] OrdImage img_obj2 = (OrdImage) get_image_obj_result2. getCustomDatum(IMG_COL, OrdImage.getFactory( )); [12] OrdImageSignature img_sig2 = (OrdImageSignature)get_sig_result2. getCustomDatum(SIG_COL,OrdImageSignature.getFactory( )); [13] img_obj2.setProperties( ); [14] img_obj2.importData(ctx); [15] img_sig2.generateSignature(img_obj2); [16] int result = OrdImageSignature.isSimilar(img_sig1, img_sig2, "color=1 texture=1 shape=1 location=1", (float)similarity_threshold,con); if (result==1){ System.out.println("Signatures are similar "); } else{ System.out.println("Signatures are different "); } [17] get_sig_result2.close( ); get_image_obj_result2.close( ); get_sig_result1.close( ); get_image_obj_result1.close( ); get_sig_obj.close( ); get_image_obj.close( ); } [18] catch(Exception e){ System.out.println("exception raised " + e); System.out.println("Signature similarity computation failed"); } }
The video example (including VideoExample.sql and VideoExample.java) contains user-defined methods that use SQL, JDBC, and interMedia Java Classes APIs to perform the following operations:
Example 2-32 shows the contents of VideoExample.sql.
set echo on --PLEASE change system password connect system/manager drop user VIDEOUSER cascade; [1] create user VIDEOUSER identified by VIDEOUSER ; grant connect,resource to VIDEOUSER identified by VIDEOUSER; [2] connect VIDEOUSER/VIDEOUSER [3] CREATE TABLE TVID(n NUMBER, vid ORDSYS.ORDVIDEO); -- Note - the OrdVideo.init method was added in interMedia 8.1.7. -- If you are running against an older release of interMedia and the -- Oracle database, you will have to modify the following INSERT statements -- to use the OrdVideo default constructor. [4] INSERT INTO TVID VALUES(1, ORDSYS.ORDVideo.init( )); INSERT INTO TVID VALUES(2, ORDSYS.ORDVideo.init( )); INSERT INTO TVID VALUES(3, ORDSYS.ORDVideo.init( )); commit; /
The SQL statements in VideoExample.sql perform the following operations:
See Oracle interMedia User's Guide and Reference for more information on the init method.
Section 2.4.2.1 through Section 2.4.2.8 show the methods contained in the VideoExample.java sample file.
Example 2-33 shows the main( ) method.
public static void main (String args[ ]){ byte[ ] ctx = new byte[4000]; OracleConnection con = null; try { VideoExample tk = new VideoExample( ); [1] con = tk.connect( ); //Include the following line only if you are running //an Oracle 8.1.7 database or later. //If you are running a database server prior to 8.1.7, //the call will fail. [2] OrdMediaUtil.imCompatibilityInit(con); [3] tk.loadDataFromFile(con); tk.extractProperties(con); tk.printProperties(con); tk.loadDataFromStream(con); tk.otherMethods(con); tk.loadDataFromByteArray(con); [4] con.commit( ); [5] con.close( ); System.out.println("Done."); } [6] catch (Exception e) { try { System.out.println("Exception : " + e); con.close( ); } catch(Exception ex) { System.out.println("Close Connection Exception : " + ex); } } }
The code in the main( ) method performs the following operations:
Section 2.4.2.2 through Section 2.4.2.8 will provide information on the methods called from the main( ) method in the order in which they are called, not in the order they appear in VideoExample.java.
Example 2-34 shows a user-defined method named connect( ), which makes a connection from the application to the database.
public OracleConnection connect( ) throws Exception{ String connectString; [1] Class.forName ("oracle.jdbc.driver.OracleDriver"); [2] connectString = "jdbc:oracle:oci8:@"; [3] OracleConnection con = (OracleConnection) DriverManager.getConnection(connectString,"VIDEOUSER","VIDEOUSER"); [4] con.setAutoCommit(false); return con; }
The connect( ) method performs the following operations:
Example 2-35 shows a user-defined method named loadDataFromFile( ), which uses the interMedia loadDataFromFile( ) method to load media data into the application object.
public void loadDataFromFile(OracleConnection con){ try { [1] Statement s = con.createStatement( ); [2] OracleResultSet rs = (OracleResultSet)s.executeQuery ("select * from TVID where n = 1 for update "); int index = 0; [3] while(rs.next( )){ [4] index = rs.getInt(1); [5] OrdVideo vidObj = (OrdVideo) rs.getCustomDatum(2, OrdVideo.getFactory( )); [6] vidObj.loadDataFromFile("testvid.dat"); [7] vidObj.getDataInFile("output1.dat"); System.out.println("************AFTER getDataInFile "); [8] System.out.println("getContentLength output : " + vidObj.getContentLength( )); [9] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update tvid set vid = ? where n = " + index); stmt1.setCustomDatum(1,vidObj); stmt1.execute( ); stmt1.close( ) ; } System.out.println("loading successful"); } [10] catch(Exception e) { System.out.println("exception raised " + e); System.out.println("loading unsuccessful"); } }
The loadDataFromFile( ) method performs the following operations:
Example 2-36 shows a user-defined method named extractProperties( ), which sets the properties in the application object.
public void extractProperties(OracleConnection con){ byte[ ] ctx[ ] = new byte [4000][1]; try { [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet)s.executeQuery ("select * from TVID where n = 1 for update"); int index = 0; while(rs.next( )){ index = rs.getInt(1); OrdVideo vidObj = (OrdVideo) rs.getCustomDatum(2, OrdVideo.getFactory( )); [2] vidObj.setProperties(ctx); System.out.println("set Properties called"); [3] if(vidObj.checkProperties(ctx)) { System.out.println("checkProperties called"); System.out.println("setBindParams successful"); System.out.println("setProperties successful"); System.out.println("checkProperties successful"); System.out.println("extraction successful"); } else { System.out.println("checkProperties called"); System.out.println("extraction not successful"); System.out.println("checkProperties successful"); } [4] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update tvid set vid = ? where n = " + index); stmt1.setCustomDatum(1,vidObj); stmt1.execute( ); stmt1.close( ) ; } rs.close( ); s.close( ); } [5] catch(Exception e) { System.out.println("exception raised " + e); System.out.println("extract prop unsuccessful"); } }
The extractProperties( ) method performs the following operations:
Example 2-37 shows a user-defined method named printProperties( ), which prints the attributes of the application object to the screen.
public void printProperties(OracleConnection con){ try { [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet)s.executeQuery ("select * from TVID where n = 1 "); int index = 0; while(rs.next( )){ index = rs.getInt(1); OrdVideo vidObj = (OrdVideo) rs.getCustomDatum(2, OrdVideo.getFactory( )); [2] System.out.println("format: " + vidObj.getFormat( )); System.out.println("mimetype: " + vidObj.getMimeType( )); System.out.println("width: " + vidObj.getWidth( )); System.out.println("height: " + vidObj.getHeight( )); System.out.println("frame resolution: " + vidObj.getFrameResolution( )); System.out.println("frame rate: " + vidObj.getFrameRate( )); System.out.println("video duration: " + vidObj.getVideoDuration( )); System.out.println("number of frames: " + vidObj.getNumberOfFrames( )); System.out.println("description : " + vidObj.getDescription( )); System.out.println("compression type: " + vidObj.getCompressionType( )); System.out.println("bit rate: " + vidObj.getBitRate( )); System.out.println("num of colors: " + vidObj.getNumberOfColors( )); } } [3] catch(Exception e) { System.out.println("exception raised " + e); System.out.println("print proerties unsuccessful"); } }
The printProperties( ) method performs the following operations:
Example 2-38 shows a user-defined method named loadDataFromStream( ), which uses the interMedia loadDataFromInputStream( ) method to load media data into the application object.
public void loadDataFromStream(OracleConnection con){ try { [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet) s.executeQuery ("select * from TVID where n = 2 for update "); int index = 0; while(rs.next( )){ index = rs.getInt(1); OrdVideo vidObj = (OrdVideo) rs.getCustomDatum(2, OrdVideo.getFactory( )); [2] FileInputStream fStream = new FileInputStream ("testvid.dat"); [3] vidObj.loadDataFromInputStream(fStream); [4] vidObj.getDataInFile("output2.dat"); [5] fStream.close( ); System.out.println("************AFTER getDataInFile "); [6] System.out.println("getContentLength output : " + vidObj.getContentLength( )); [7] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update tvid set vid = ? where n = " + index); stmt1.setCustomDatum(1,vidObj); stmt1.execute( ); stmt1.close( ) ; } System.out.println("load data from stream successful"); } [8] catch(Exception e) { System.out.println("exception raised " + e); System.out.println("load data from stream unsuccessful"); } }
The loadDataFromStream( ) method performs the following operations:
Example 2-39 shows a user-defined method named otherMethods( ), which attempts to use the processSourceCommand( ) method.
public void otherMethods(OracleConnection con){ byte[ ] ctx[ ] = {new byte[4000]}; byte[ ] res[ ] = {new byte[20]}; [1] int suc = 1; try { [2] Statement s1 = con.createStatement( ); OracleResultSet rs1 = (OracleResultSet) s1.executeQuery("select * from TVID where n = 1 for update "); int index1 = 0; while(rs1.next( )) { index1 = rs1.getInt(1); OrdVideo vidObj = (OrdVideo) rs1.getCustomDatum(2, OrdVideo.getFactory( )); [3] try { byte[ ] pSRes = vidObj.processSourceCommand(ctx, "", "", res); suc = 0; } [4] catch (Exception e) { System.out.println("Expected Exception raised in processSourceCommand(...)" ); } [5] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update tvid set vid = ? where n = " + index1); stmt1.setCustomDatum(1,vidObj); stmt1.execute( ); stmt1.close( ) ; } rs1.close( ); s1.close( ); } [6] catch(Exception e){ System.out.println("Exception raised " ); } [7] if(suc == 1) System.out.println("other methods successful"); else System.out.println("other methods unsuccessful"); }
The otherMethods( ) method performs the following operations:
Example 2-40 shows a user-defined method named loadDataFromByteArray( ), which uses the interMedia loadDataFromByteArray( ) method to load media data into the application object.
public void loadDataFromByteArray(OracleConnection con){ try { [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet) s.executeQuery ("select * from TVID where n = 3 for update "); int index = 0; while(rs.next( )){ index = rs.getInt(1); OrdVideo vidObj = (OrdVideo) rs.getCustomDatum(2, OrdVideo.getFactory( )); [2] File ff = new File("testvid.dat"); int fileLength = (int) ff.length( ); byte[ ] data = new byte[fileLength]; [3] FileInputStream fStream = new FileInputStream("testvid.dat"); [4] fStream.read(data,0,fileLength); [5] vidObj.loadDataFromByteArray(data); [6] fStream.close( ); [7] vidObj.getDataInFile("output3.dat"); [8] byte[ ] resArr = vidObj.getDataInByteArray( ); [9] System.out.println("byte array length : " + resArr.length); [10] FileOutputStream outStream = new FileOutputStream ("output4.dat"); [11] outStream.write(resArr); [12] outStream.close( ); [13] InputStream inpStream = vidObj.getDataInStream( ); int length = 32768; byte[ ] tempBuffer = new byte[32768]; [14] int numRead = inpStream.read(tempBuffer,0,length); try { [15] outStream = new FileOutputStream("output5.dat"); [16] while(numRead != -1) { [17] if (numRead < 32768) { length = numRead; outStream.write(tempBuffer,0,length); break; } [18] else outStream.write(tempBuffer,0,length); [19] numRead = inpStream.read(tempBuffer,0,length); } } [20] finally { outStream.close( ); inpStream.close( ); } System.out.println("************AFTER getDataInFile "); [21] System.out.println(" getContentLength output : " + vidObj.getContentLength( )); [22] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update tvid set vid = ? where n = " + index); stmt1.setCustomDatum(1,vidObj); stmt1.execute( ); stmt1.close( ) ; } } [23] catch(Exception e) { System.out.println("exception raised " + e); System.out.println("loadData from byte array unsuccessful"); } }
The loadDataFromByteArray( ) method performs the following operations:
|
Copyright © 1996-2001, Oracle Corporation. All Rights Reserved. |
|