Oracle interMedia Audio, Image, and Video Java Classes User's Guide and Reference
Release 8.1.7

Part Number A85374-01

Library

Product

Contents

Index

Go to previous page Go to next page

2
Program Examples Using Java Classes

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.


Note:

This chapter contains examples of Java and SQL code. Some of the code examples display boldface numbers enclosed in brackets; these indicate that further explanation of that code will be in the numbered list immediately following the example. 


2.1 Audio Example

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:

2.1.1 AudioExample.sql

Example 2-1 shows the complete contents of the AudioExample.sql sample file.

Example 2-1 Contents of AudioExample.sql

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:

  1. Create a user named AUDIOUSER and grant the appropriate permissions to the user.

  2. Connect to the database server as AUDIOUSER.

  3. Create a table named TAUD with two columns: a column of numbers and a column of ORDAudio objects.

  4. Add three rows to the table, each containing an empty ORDAudio object. The names of the variables being set are included in comments for the first INSERT statement only.

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.

2.1.2 AudioExample.java

Section 2.1.2.1 through Section 2.1.2.8 show the methods contained in the AudioExample.java sample file.

2.1.2.1 main( ) Method

Example 2-2 shows the main( ) method.

Example 2-2 main( ) Method (Audio)

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:

  1. Uses the connect( ) method to make a connection to a database table.

  2. Ensures the compatibility of your 8.1.7 application. See Section 1.7 for more information.

  3. Calls several methods (also defined in AudioExample.java) that manipulate objects on the database server and the local machine.

  4. Commits any changes made to the database table.

  5. Closes the connection to the database.

  6. Handles any errors or exceptions raised by the code.

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.

2.1.2.2 connect( ) Method

Example 2-3 shows a user-defined method named connect( ), which makes a connection from the application to the database.

Example 2-3 connect( ) Method (Audio)

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:

  1. Loads the JDBC drivers directly, because Oracle uses a JDK-compliant Java virtual machine.

  2. Defines a string that contains the URL of the database to which you will connect. You may need to change this string to match your database.

  3. Sets the connection to the database, using the URL contained in connectString, the user name AUDIOUSER, and the password AUDIOUSER. The user name and password were created by AudioExample.sql.

  4. Disables the auto-commit mode. This means that you must commit or roll back manually with the commit( ) or rollback( ) methods, respectively.

2.1.2.3 loadDataFromFile( ) Method

Example 2-4 shows a user-defined method named loadDataFromFile( ), which uses the interMedia loadDataFromFile( ) method to populate the application object with media data.

Example 2-4 loadDataFromFile( ) Method (Audio)

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:

  1. Creates an OracleStatement object.

  2. Executes the given SQL query and puts the results into a local OracleResultSet object.

  3. Performs the operations in the loop while there are results in the OracleResultSet that have not been processed. However, in this case, there is only one row included in the OracleResultSet, so the operations in the loop will run once.

  4. Sets an index variable to the value of the integer in the first column of the first row in the OracleResultSet (in this case, the value is 1).

  5. Creates a local ORDAudio object named audObj. Populates audObj with the contents of the ORDAudio object in the second column of the current row in the OracleResultSet.

  6. Uses the ORDAudio loadDataFromFile( ) method to load the media data in testaud.dat into the database ORDAudio object and into audObj. This also sets the local field on audObj, but not the database object.

  7. Uses the getDataInFile( ) method to get the media data from audObj and loads it into a file on the local system named output1.dat.

  8. Gets the content length of audObj and prints it to the screen to verify the success of the loading.

  9. Creates and executes a SQL statement that will update the database ORDAudio object with the contents of audObj.

  10. Handles any errors or exceptions raised by the code.

2.1.2.4 extractProperties( ) Method

Example 2-5 shows a user-defined method named extractProperties( ), which sets the properties in the application object.

Example 2-5 extractProperties( ) Method (Audio)

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:

  1. Creates a statement, a local OracleResultSet, and a local ORDAudio object named audObj, and populates audObj with media data through the same process described in steps 1 through 5 of Example 2-4. In this method, you will be operating on the contents of the first row of the database table.

  2. Calls setProperties( ) to extract properties values from the media data and set them in the application ORDAudio object. See "setProperties(byte[ ][ ])" in Chapter 3 for a list of the properties values extracted and set.

  3. Calls checkProperties( ) to compare the properties values in the application object with the values in the media data. If all values are the same, checkProperties( ) returns TRUE and the appropriate messages are printed to the screen. If any values differ, checkProperties( ) returns FALSE and the appropriate messages are printed to the screen.

  4. Creates and executes a SQL statement that will update the database ORDAudio object with the contents of audObj (including the properties extracted by setProperties( )).

  5. Handles any errors or exceptions raised by the code.

2.1.2.5 printProperties( ) Method

Example 2-6 shows a user-defined method named printProperties( ), which prints the attributes of the application object to the screen.

Example 2-6 printProperties( ) Method (Audio)

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:

  1. Creates a statement, a local OracleResultSet, and a local ORDAudio object named audObj, and populates audObj with media data through the same process described in steps 1 through 5 of Example 2-4. In this method, you will be operating on the contents of the first row of the database table.

  2. Gets the values of the properties in audObj and prints them to the screen.

  3. Handles any errors or exceptions raised by the code.

2.1.2.6 otherMethods( ) Method

Example 2-7 shows a user-defined method named otherMethods( ), which attempts to use the processSourceCommand( ) method.

Example 2-7 otherMethods( ) Method (Audio)

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:

  1. Creates an integer that will be used to indicate the success or failure of the method and sets it initially to 1 (for success).

  2. Creates a statement, a local OracleResultSet, and a local ORDAudio object named audObj, and populates audObj with media data through the same process described in steps 1 through 5 of Example 2-4. In this method, you will be operating on the contents of the first row of the database table.

  3. Tries to call processSourceCommand( ) with no value specified for the command to be called on the server side. This should raise an exception, which means the code following the processSourceCommand( ) call will not be run and the code in the catch loop will. If an exception is not raised, then the method has failed and the success indicator is set to 0 (for failure).

  4. Prints the expected exception that was raised in step 3.

  5. Creates and executes a SQL statement that will update the database ORDAudio object with the contents of audObj.

  6. Handles any unexpected errors or exceptions raised by the code.

  7. Prints the appropriate message to the screen based on the success or failure of the method.

2.1.2.7 loadDataFromStream( ) Method

Example 2-8 shows a user-defined method named loadDataFromStream( ), which uses the interMedia loadDataFromInputStream( ) method to load media data into the application object.

Example 2-8 loadDataFromStream( ) Method (Audio)

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:

  1. Creates a statement, a local OracleResultSet, and a local ORDAudio object named audObj, and populates audObj with media data through the same process described in steps 1 through 5 of Example 2-4. In this method, you will be operating on the contents of the second row of the database table.

  2. Creates a new FileInputStream object. This input stream contains the contents of the local file testaud.dat.

  3. Uses the loadDataFromInputStream( ) method to load the media data in the input stream into the database ORDAudio object and into audObj. This also sets the local field on audObj, but not the database object.

  4. Uses the getDataInFile( ) method to get the media data from the application ORDAudio object and load it into a file on the local system named output2.dat.

  5. Closes the local input stream.

  6. Gets the content length of audObj and prints it to the screen to verify the success of the loading.

  7. Creates and executes a SQL statement that will update the database ORDAudio object with the contents of audObj. This update will set the attributes on the database object to match the application object.

  8. Handles any errors or exceptions raised by the code.

2.1.2.8 loadDataFromByteArray( ) Method

Example 2-9 shows a user-defined method named loadDataFromByteArray( ), which uses the interMedia loadDataFromByteArray( ) method to load media data into the application object.

Example 2-9 loadDataFromByteArray( ) Method (Audio)

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:

  1. Creates a statement, a local OracleResultSet, and a local ORDAudio object named audObj, and populates audObj with media data through the same process described in steps 1 through 5 of Example 2-4. In this method, you will be operating on the contents of the third row of the database table.

  2. Determines the size (in bytes) of the local file testaud.dat and creates a byte array of the same size.

  3. Creates a new FileInputStream object. This input stream contains the contents of testaud.dat.

  4. Reads the contents of the input stream into the byte array.

  5. Uses the loadDataFromByteArray( ) method to load the media data in the byte array into the database ORDAudio object and into audObj. This also sets the local field on audObj, but not the database object.

  6. Closes the input stream.

  7. Uses the getDataInFile( ) method to get the media data from the application ORDAudio object and load it into a file on the local system named output3.dat.

  8. Uses the getDataInByteArray( ) method to get the media data from the application ORDAudio object and load it into a local byte array named resArr.

  9. Gets the length of resArr and prints it to the screen to verify the success of the loading.

  10. Creates a new FileOutputStream object named outStream. This output stream will write data to a local file named output4.dat.

  11. Writes the contents of resArr to output4.dat.

  12. Closes the output stream.

  13. Creates a new input stream named inpStream. Uses the getDataInStream( ) method to get the media data from the application ORDAudio object and store it in inpStream.

  14. Reads 32300 bytes from the beginning (that is, at an offset of 0) of inpStream into the byte array tempBuffer. The integer numRead will be set to the total number of bytes read, or -1 if the end of the input stream has been reached. In this case, if loading is successful, numRead should be equal to 32300.

  15. Re-opens OutStream. In this case, it will write data to a local file named output5.dat.

  16. Runs the operations in the while loop if numRead is not equal to -1. The program should enter this loop.

  17. Enters the if loop if numRead is less than 32300 (that is, if not all the data was read). The if loop will write the number of bytes read into tempBuffer into outStream, and then break out of the loop.

  18. Writes 32300 bytes into outStream if numRead is 32300.

  19. Attempts to read more data from the input stream into the byte array. If 32300 bytes of data are read successfully, then numRead will be set to -1 and the program will exit the loop. If there is still unread data in the input stream, then it will be read into the byte array and steps 17 and 18 will be repeated.

  20. Closes both the input stream and the output stream after exiting the while loop.

  21. Gets the content length of audObj and prints it to the screen to verify the success of the loading.

  22. Creates and executes a SQL statement that will update the database ORDAudio object with the contents of audObj. This update will set the attributes on the database object to match the application object.

  23. Handles any errors or exceptions raised by the code.

2.2 Image Example

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:

2.2.1 ImageExample.sql

Example 2-10 shows the contents of ImageExample.sql.

Example 2-10 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:

  1. Create a user named IMAGEUSER and grant the appropriate permissions to the user.

  2. Create a directory named ORDIMAGEDIR and set the appropriate permissions. You will need to change the directory to match your schema.

  3. Connect to the database server as IMAGEUSER.

  4. Create a table named ordimagetab, which contains one column of numbers and two columns of ORDImage objects.

  5. Using the init method, add four rows with two empty objects each.

  6. Using the init method, add two rows with one object with values and one empty object.

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.

2.2.2 ImageExample.java

Section 2.2.2.1 through Section 2.2.2.9 show the methods contained in the ImageExample.java sample file.

2.2.2.1 main( ) Method

Example 2-11 shows the main( ) method.

Example 2-11 main( ) Method (Image)

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:

  1. Uses the connect( ) method to make a connection to a database table.

  2. Ensures the compatibility of your 8.1.7 application; this will only work if your database server is at least release 8.1.7. See Section 1.7 for more information.

  3. Calls several methods (also defined in ImageExample.java) that manipulate objects on the database server and the local machine.

  4. Commits any changes made to the database table.

  5. Closes the connection to the database.

  6. Handles any errors or exceptions raised by the code.

Section 2.2.2.2 through Section 2.2.2.9 will provide information on the methods called from the main( ) method.

2.2.2.2 connect( )Method

Example 2-12 shows a user-defined method named connect( ), which makes a connection from the application to the database.

Example 2-12 connect( ) Method (Image)

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:

  1. Loads the JDBC drivers directly, because Oracle uses a JDK-compliant Java virtual machine.

  2. Defines a string that contains the URL of the database to which you will connect. You may need to change this string to match your database.

  3. Sets the connection to the database, using the URL contained in connectString, the user name IMAGEUSER, and the password IMAGEUSER. The user name and password were created by ImageExample.sql.

  4. Disables the auto-commit mode. This means that you must commit or roll back manually with the commit( ) or rollback( ) methods, respectively.

2.2.2.3 setPropertiesExample( ) Method

Example 2-13 shows a user-defined method named setPropertiesExample( ), which sets the properties in the application object.

Example 2-13 setPropertiesExample( ) Method (Image)

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:

  1. Creates an OracleStatement object.

  2. Executes the given SQL query and puts the results into a local OracleResultSet object.

  3. Performs the operations in the loop while there are results in the OracleResultSet that have not been processed. However, in this case, there is only one row included in the OracleResultSet, so the operations in the loop will run once.

  4. Sets an index variable to the value of the integer in the first column of the first row in the OracleResultSet (in this case, the value is 5).

  5. Creates a local ORDImage object named imgObj. Populates imgObj with the contents of the ORDImage object in the second column of the current row in the OracleResultSet.

  6. Calls setProperties( ) to extract properties values from the media data and set them in the application ORDImage object. See "setProperties( )" in Chapter 4 for a list of the properties values extracted and set.

  7. Calls checkProperties( ) to compare the properties values in the application object with the values in the media data. If all values are the same, checkProperties( ) returns TRUE and the appropriate messages are printed to the screen. If any values differ, checkProperties( ) returns FALSE and the appropriate messages are printed to the screen.

  8. Creates and executes a SQL statement that will update the database ORDImage object with the contents of imgObj.

  9. Handles any errors or exceptions raised by the code.

2.2.2.4 displayPropertiesExample( ) Method

Example 2-14 shows a user-defined method named displayPropertiesExample( ), which prints the attributes of the application object to the screen.

Example 2-14 displayPropertiesExample( ) Method (Image)

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:

  1. Creates a statement, a local OracleResultSet, and a local ORDImage object named imgObj, and populates imgObj with media data through the same process described in steps 1 through 5 of Example 2-13. In this method, you will be operating on the contents of the fifth row of the database table. This is the same row you operated on in Example 2-13.

  2. Gets the values of the properties in imgObj and prints them to the screen.

  3. Gets the attributes of imgObj and stores them in a string by using the getAllAttributesAsString( ) method, and prints the contents of the string to the screen. See Section 2.2.2.5 for more information on getAllAttributesAsString( ).

  4. Handles any errors or exceptions raised by the call to getAllAttributesAsString( ).

  5. Handles any errors or exceptions raised by the code in general.

2.2.2.5 getAllAttributesAsString( ) Method

Example 2-15 shows a user-defined method named getAllAttributesAsString( ), which creates a String object that contains the values of the application object attributes.

Example 2-15 getAllAttributesAsString( ) Method (Image)

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:

  1. Creates a String object named attStr. Gets the values of several attributes from the application image object and stores their values in attStr.

  2. Returns attStr to the method that called this method.

2.2.2.6 fileBasedExample( ) Method

Example 2-16 shows a user-defined method named fileBasedExample( ), which uses the loadDataFromFile( ) method to load media data into the application object.

Example 2-16 fileBasedExample( ) Method (Image)

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:

  1. Creates a statement, a local OracleResultSet, and a local ORDImage object named imgObj, and populates imgObj with media data through the same process described in steps 1 through 5 of Example 2-13. In this method, you will be operating on the contents of the second row of the database table.

  2. Uses the loadDataFromFile( ) method to load the media data from the local file imgdemo.dat into the database ORDImage object and into imgObj. This also sets the local field on imgObj, but not the database object.

  3. Calls setProperties( ) to extract properties values from the media data and set them in the application ORDImage object. See "setProperties( )" in Chapter 4 for a list of the properties values extracted and set.

  4. Uses the getDataInFile( ) method to get the media data from the application ORDImage object and loads it into a file on the local system named fileexample.dat.

  5. Creates and executes a SQL statement that will update the database ORDImage object with the contents of imgObj. This update will set the attributes on the database object, to match the application object.

  6. Handles any errors or exceptions raised by the code.

2.2.2.7 streamBasedExample( ) Method

Example 2-17 shows a user-defined method named streamBasedExample( ), which uses the loadDataFromInputStream( ) method to load media data into the application object.

Example 2-17 streamBasedExample( ) Method (Image)

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:

  1. Creates a statement, a local OracleResultSet, and a local ORDImage object named imgObj, and populates imgObj with media data through the same process described in steps 1 through 5 of Example 2-13. In this method, you will be operating on the contents of the third row of the database table.

  2. Creates a new FileInputStream object. This input stream contains the contents of the local file imgdemo.dat.

  3. Uses the loadDataFromInputStream( ) method to load the media data in the input stream into the database ORDImage object and into imgObj. This also sets the local field on imgObj, but not the database object.

  4. Closes the input stream.

  5. Calls setProperties( ) to extract properties values from the media data and set them in the application ORDImage object. See "setProperties( )" in Chapter 4 for a list of the properties values extracted and set.

  6. Creates a new InputStream named inpStream. Calls getDataInStream( ) to get the media data from the application ORDImage object and stores it in inpStream.

  7. Reads 32300 bytes from the beginning (that is, at an offset of 0) of inpStream into the byte array tempBuffer. The integer numRead will be set to the total number of bytes read, or -1 if the end of the input stream has been reached. In this case, if loading is successful, numRead should be equal to 32300.

  8. Creates a new FileOutputStream object named outStream. This output stream will write data to a local file named streamexample.dat.

  9. Runs the operations in the while loop if numRead is not equal to -1. The program should enter this loop.

  10. Writes the number of bytes read into tempBuffer into outStream if numRead is less than 32300 (that is, if not all the data was read).

  11. Writes 32300 bytes into outStream if numRead is 32300.

  12. Attempts to read more data from the input stream into the byte array. If 32300 bytes of data are read successfully, then numRead will be set to -1 and the program will exit the loop. If there is still unread data in the input stream, then it will be read into the byte array and steps 10 and 11 will be repeated.

  13. Closes both the input stream and the output stream after exiting the while loop.

  14. Creates and executes a SQL statement that will update the database ORDImage object with the contents of imgObj. This update will set the attributes on the database object to match the application object.

  15. Handles any errors or exceptions raised by the code.

2.2.2.8 byteArrayBasedExample( ) Method

Example 2-18 shows a user-defined method named byteArrayBasedExample( ), which uses the loadDataFromByteArray( ) method to load media data into the application object.

Example 2-18 byteArrayBasedExample( ) Method (Image)

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:

  1. Creates a statement, a local OracleResultSet, and a local ORDImage object named imgObj, and populates imgObj with media data through the same process described in steps 1 through 5 of Example 2-13. In this method, you will be operating on the contents of the fourth row of the database table.

  2. Determines the size (in bytes) of the local file imgdemo.dat and creates a byte array of the same size.

  3. Creates a new FileInputStream object. This input stream contains the contents of imgdemo.dat.

  4. Reads the contents of the input stream into the byte array.

  5. Uses the loadDataFromByteArray( ) method to load the media data in the byte array into the database ORDImage object and into imgObj. This also sets the local field on imgObj, but not the database object.

  6. Closes the input stream.

  7. Calls setProperties( ) to extract properties values from the media data and set them in the application ORDImage object. See "setProperties( )" in Chapter 4 for a list of the properties values extracted and set.

  8. Uses the getDataInByteArray( ) method to get the media data from the application ORDImage object and load it into a local byte array named resArr.

  9. Gets the length of resArr and prints it to the screen to verify the success of the loading.

  10. Creates and executes a SQL statement that will update the database ORDImage object with the contents of imgObj. This update will set the attributes on the database object to match the application object.

  11. Handles any errors or exceptions raised by the code.

2.2.2.9 processExample( ) Method

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.

Example 2-19 processExample( ) Method (Image)

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:

  1. Creates a statement, a local OracleResultSet, and a local ORDImage object named imgObj, and populates imgObj with media data through the same process described in steps 1 through 5 of Example 2-13. In this method, you will be operating on the contents of the second row of the database table. The database ORDImage object is named image.

  2. Creates a local ORDImage object named imgObj2. Populates imgObj2 with the contents of the ORDImage object in the third column of the current row in the OracleResultSet. This database ORDImage column is named image2.

  3. Populates the image data in imgObj2 with a 32 x 32 GIF thumbnail image generated from the image data in imgObj. imgObj is unchanged by this operation.

  4. Uses the process( ) method to convert the image in imgObj to a JPEG (JFIF) image.

  5. Gets the attributes of imgObj by using the getAllAttributesAsString( ) method, and prints the attributes to the screen. See Section 2.2.2.5 for more information on getAllAttributesAsString( ).

  6. Gets the attributes of imgObj2 by using the getAllAttributesAsString( ) method, and prints the attributes to the screen. See Section 2.2.2.5 for more information on getAllAttributesAsString( ).

  7. Handles any errors or exceptions raised by the code in steps 3 through 6.

  8. Creates and executes a SQL statement that will update the appropriate database ORDImage objects with the contents of imgObj and imgObj2.

  9. Handles any errors or exceptions raised by the code.

2.3 Video Example

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:

2.3.1 VideoExample.sql

Example 2-20 shows the contents of VideoExample.sql.

Example 2-20 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:

  1. Create a user named VIDEOUSER and connect as VIDEOUSER.

  2. Connect to the database server as VIDEOUSER.

  3. Create a table named TVID with two columns: a column of numbers and a column of ORDVideo objects.

  4. Add three rows to the table, each containing an empty ORDVideo object.

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.

2.3.2 VideoExample.java

Section 2.3.2.1 through Section 2.3.2.8 show the methods contained in the VideoExample.java sample file.

2.3.2.1 main( ) Method

Example 2-21 shows the main( ) method.

Example 2-21 main( ) Method (Video)

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:

  1. Uses the connect( ) method to make a connection to a database table.

  2. Ensures the compatibility of your 8.1.7 application; this will only work if your database server is at least release 8.1.7. See Section 1.7 for more information.

  3. Calls several methods (also defined in VideoExample.java) that manipulate objects on the database server and the local machine.

  4. Commits any changes made to the database table.

  5. Closes the connection to the database.

  6. Handles any errors or exceptions raised by the code.

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.

2.3.2.2 connect( ) Method

Example 2-22 shows a user-defined method named connect( ), which makes a connection from the application to the database.

Example 2-22 connect( ) Method (Video)

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:

  1. Loads the JDBC drivers directly, because Oracle uses a JDK-compliant Java virtual machine.

  2. Defines a string that contains the URL of the database to which you will connect. You may need to change this string to match your database.

  3. Sets the connection to the database, using the URL contained in connectString, the user name VIDEOUSER, and the password VIDEOUSER. The user name and password were created by VideoExample.sql.

  4. Disables the auto-commit mode. This means that you must commit or roll back manually with the commit( ) or rollback( ) methods, respectively.

2.3.2.3 loadDataFromFile( ) Method

Example 2-23 shows a user-defined method named loadDataFromFile( ), which uses the interMedia loadDataFromFile( ) method to load media data into the application object.

Example 2-23 loadDataFromFile( ) Method (Video)

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:

  1. Creates an OracleStatement object.

  2. Executes the given SQL query and puts the results into a local OracleResultSet object.

  3. Performs the operations in the loop while there are results in the OracleResultSet that have not been processed. However, in this case, there is only one row included in the OracleResultSet, so the operations in the loop will run once.

  4. Sets an index variable to the value of the integer in the first column of the first row in the OracleResultSet (in this case, the value is 1).

  5. Creates a local ORDVideo object named vidObj. Populates vidObj with the contents of the ORDVideo object in the second column of the current row in the OracleResultSet.

  6. Uses the loadDataFromFile( ) method to load the media data in testvid.dat into the database ORDVideo object and into vidObj. This also sets the local field on vidObj, but not the database object.

  7. Uses the getDataInFile( ) method to get the media data from the application ORDVideo object and loads it into a file on the local system named output1.dat.

  8. Gets the content length of vidObj and prints it to the screen to verify the success of the loading.

  9. Creates and executes a SQL statement that will update the database ORDVideo object with the contents of vidObj. This update will set the local attribute on the database object to match the application object.

  10. Handles any errors or exceptions raised by the code.

2.3.2.4 extractProperties( ) Method

Example 2-24 shows a user-defined method named extractProperties( ), which sets the properties in the application object.

Example 2-24 extractProperties( ) Method (Video)

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:

  1. Creates a statement, a local OracleResultSet, and a local ORDVideo object named vidObj, and populates vidObj with media data through the same process described in steps 1 through 5 of Example 2-23. In this method, you will be operating on the contents of the first row of the database table.

  2. Calls setProperties( ) to extract properties values from the media data and set them in the application ORDVideo object. See "setProperties(byte[ ][ ])" in Chapter 5 for a list of the properties values extracted and set.

  3. Calls checkProperties( ) to compare the properties values in the application object with the values in the media data. If all values are the same, checkProperties( ) returns TRUE and the appropriate messages are printed to the screen. If any values differ, checkProperties( ) returns FALSE and the appropriate messages are printed to the screen.

  4. Creates and executes a SQL statement that will update the database ORDVideo object with the contents of vidObj.

  5. Handles any errors or exceptions raised by the code.

2.3.2.5 printProperties( ) Method

Example 2-25 shows a user-defined method named printProperties( ), which prints the attributes of the application object to the screen.

Example 2-25 printProperties( ) Method (Video)

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:

  1. Creates a statement, a local OracleResultSet, and a local ORDVideo object named vidObj, and populates vidObj with media data through the same process described in steps 1 through 5 of Example 2-23. In this method, you will be operating on the contents of the first row of the database table.

  2. Gets the values of the properties in vidObj and prints them to the screen.

  3. Handles any errors or exceptions raised by the code.

2.3.2.6 loadDataFromStream( ) Method

Example 2-26 shows a user-defined method named loadDataFromStream( ), which uses the interMedia loadDataFromInputStream( ) method to load media data into the application object.

Example 2-26 loadDataFromStream( ) Method (Video)

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:

  1. Creates a statement, a local OracleResultSet, and a local ORDVideo object named vidObj, and populates vidObj with media data through the same process described in steps 1 through 5 of Example 2-23. In this method, you will be operating on the contents of the second row of the database table.

  2. Creates a new FileInputStream object. This input stream contains the contents of the local file testvid.dat.

  3. Uses the loadDataFromInputStream( ) method to load the media data in the input stream into the database ORDVideo object and into vidObj. This also sets the local field on vidObj, but not the database object.

  4. Uses the getDataInFile( ) method to get the media data from the application ORDVideo object and load it into a file on the local system named output2.dat.

  5. Closes the local input stream.

  6. Gets the content length of vidObj and prints it to the screen to verify the success of the loading.

  7. Creates and executes a SQL statement that will update the database ORDVideo object with the contents of vidObj. This update will set the attributes on the database object to match the application object.

  8. Handles any errors or exceptions raised by the code.

2.3.2.7 otherMethods( ) Method

Example 2-27 shows a user-defined method named otherMethods( ), which attempts to use the processSourceCommand( ) method.

Example 2-27 otherMethods( ) Method (Video)

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:

  1. Creates an integer that will be used to indicate the success or failure of the method and sets it initially to 1 (for success).

  2. Creates a statement, a local OracleResultSet, and a local ORDVideo object named vidObj, and populates vidObj with media data through the same process described in steps 1 through 5 of Example 2-23. In this method, you will be operating on the contents of the first row of the database table.

  3. Tries to call processSourceCommand( ) with no value specified for the command to be called on the server side. This should raise an exception, which means the code following the processSourceCommand( ) call will not be run and the code in the catch loop will. If an exception is not raised, then the method has failed and the success indicator is set to 0 (for failure).

  4. Prints the expected exception that was raised in step 3.

  5. Creates and executes a SQL statement that will update the database ORDVideo object with the contents of vidObj.

  6. Handles any unexpected errors or exceptions raised by the code.

  7. Prints the appropriate message to the screen based on the success or failure of the method.

2.3.2.8 loadDataFromByteArray( ) Method

Example 2-28 shows a user-defined method named loadDataFromByteArray( ), which uses the interMedia loadDataFromByteArray( ) method to load media data into the application object.

Example 2-28 loadDataFromByteArray( ) Method (Video)

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:

  1. Creates a statement, a local OracleResultSet, and a local ORDVideo object named vidObj, and populates vidObj with media data through the same process described in steps 1 through 5 of Example 2-23. In this method, you will be operating on the contents of the third row of the database table.

  2. Determines the size (in bytes) of the local file testvid.dat and creates a byte array of the same size.

  3. Creates a new FileInputStream object. This input stream contains the contents of testvid.dat.

  4. Reads the contents of the input stream into the byte array.

  5. Uses the loadDataFromByteArray( ) method to load the media data in the byte array into the database ORDVideo object and into vidObj. This also sets the local field on vidObj, but not the database object.

  6. Closes the input stream.

  7. Uses the getDataInFile( ) method to get the media data from the application ORDVideo object and load it into a file on the local system named output3.dat.

  8. Uses the getDataInByteArray( ) method to get the media data from the application ORDVideo object and load it into a local byte array named resArr.

  9. Gets the length of resArr and prints it to the screen to verify the success of the loading.

  10. Creates a new FileOutputStream object named outStream. This output stream will write data to a local file named output4.dat.

  11. Writes the contents of resArr to output4.dat.

  12. Closes the output stream.

  13. Creates a new input stream named inpStream. Uses the getDataInStream( ) method to get the media data from the application ORDVideo object and store it in inpStream.

  14. Reads 32300 bytes from the beginning (that is, at an offset of 0) of inpStream into the byte array tempBuffer. The integer numRead will be set to the total number of bytes read, or -1 if the end of the input stream has been reached. In this case, if loading is successful, numRead should be equal to 32300.

  15. Re-opens OutStream. In this case, it will write data to a local file named output5.dat.

  16. Runs the operations in the while loop if numRead is not equal to -1. The program should enter this loop.

  17. Writes the number of bytes read into tempBuffer into outStream if numRead is less than 32300 (that is, if not all the data was read).

  18. If numRead is 32300, writes 32300 bytes into outStream.

  19. Attempts to read more data from the input stream into the byte array. If 32300 bytes of data are successfully read, then numRead will be set to -1 and the program will exit the loop. If there is still unread data in the input stream, then it will be read into the byte array and steps 17 and 18 will be repeated.

  20. Closes both the input stream and the output stream after exiting the while loop.

  21. Gets the content length of vidObj and prints it to the screen to verify the success of the loading.

  22. Creates and executes a SQL statement that will update the database ORDVideo object with the contents of vidObj. This update will set the attributes on the database object to match the application object.

  23. Handles any errors or exceptions raised by the code.


Go to previous page Go to next page
Oracle
Copyright © 1996-2000, Oracle Corporation.

All Rights Reserved.

Library

Product

Contents

Index