Oracle interMedia Audio, Image, and Video Java Classes User's Guide and Reference Release 8.1.7 Part Number A85374-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, 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 Oracle, -- 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:
The ORDAudio.init method was added in release 8.1.7. If you are running against a previous release of interMedia and Oracle8i, you will have to modify the INSERT statements in step 4 to use the ORDAudio default constructor.
See Oracle8i interMedia Audio, Image, and Video 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( ) ; index++; } 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( ) ; index++; } 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( ) ; index1++; } 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( ); index++; } 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 = 32300; byte[ ] tempBuffer = new byte[32300]; [14] int numRead = inpStream.read(tempBuffer,0,length); try { [15] outStream = new FileOutputStream("output5.dat"); [16] while (numRead != -1) { [17] if (numRead < 32300) { 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( ) ; index++; } } [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 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-10 shows the contents of ImageExample.sql.
set echo on -- Please Change system password. connect system/manager drop user IMAGEUSER cascade; [1] grant connect,resource to IMAGEUSER identified by IMAGEUSER; -- Replace C:\Oracle\Ora81' with your ORACLE HOME [2] create or replace directory ORDIMAGEDIR as 'C:\Oracle\Ora81\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 releasen of interMedia and Oracle, -- 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( )); commit; set echo off exit;
The SQL statements in ImageExample.sql perform the following operations:
The ORDImage.init method was added in release 8.1.7. If you are running against a previous release of interMedia and Oracle8i, you will have to modify the INSERT statements in steps 5 and 6 to use the ORDImage default constructor.
See Oracle8i interMedia Audio, Image, and Video User's Guide and Reference for more information on the init method.
Section 2.2.2.1 through Section 2.2.2.9 show the methods contained in the ImageExample.java sample file.
Example 2-11 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] 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.9 will provide information on the methods called from the main( ) method.
Example 2-12 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-13 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-14 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-15 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-16 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-17 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-18 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-19 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:
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-20 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 Oracle, -- 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:
The ORDVideo.init method was added in release 8.1.7. If you are running against a previous release of interMedia and Oracle, you will have to modify the INSERT statements in step 4 to use the ORDVideo default constructor.
See Oracle8i interMedia Audio, Image, and Video User's Guide and Reference for more information on the init method.
Section 2.3.2.1 through Section 2.3.2.8 show the methods contained in the VideoExample.java sample file.
Example 2-21 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.3.2.2 through Section 2.3.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-22 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-23 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( ) ; index++; } 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-24 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( ) ; index++; } 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-25 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-26 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( ) ; index++; } 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-27 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( ) ; index1++; } 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-28 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 = 32300; byte[ ] tempBuffer = new byte[32300]; [14] int numRead = inpStream.read(tempBuffer,0,length); try { [15] outStream = new FileOutputStream("output5.dat"); [16] while(numRead != -1) { [17] if (numRead < 32300) { 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( ) ; index++; } } [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-2000, Oracle Corporation. All Rights Reserved. |
|