Oracle8i interMedia Audio, Image, and Video Java Client User's Guide and Reference
Release 8.1.5

A67296-01

Library

Product

Contents

Index

Prev Next

2
Java Client Program Examples

This chapter provides examples that show common operations between the client and server. Examples are presented in two groups: audio/video and image.

All examples assume that you have created a database table on a server machine and the table is accessible from your client machine. For an example of creating the table, see Oracle8i interMedia Audio, Image, and Video User's Guide and Reference.

2.1 Example of Audio and Video APIs

Both the audio and video APIs work in the same way. Therefore, an example is provided for only the audio APIs. It can be assumed that the video APIs will behave in the same way and can be called in the same way as in the audio example.

For the audio example, the test class given here invokes the following APIs:

Reference information on the audio methods used in this example is presented in Chapter 5. Reference material on similar video methods is presented in Chapter 7.

2.1.1 Complete Audio Example

The following is a complete test class for invoking the audio related APIs:

import java.sql.*;
import java.io.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;
import oracle.ord.media.*;

public class demoProgram {
     public Connection connection;
     
     demoProgram( ) {} 
     
     public void connect( ) throws Exception
     {
          String connectString;
          Class.forName ("oracle.jdbc.driver.OracleDriver");
          connectString = "jdbc:oracle:oci8:@";
          connection = 
          DriverManager.getConnection(connectString,"ORDMEDIADEMO", 
               "ORDMEDIADEMO");
          connection.setAutoCommit(false);
     }
     
     public static void main(String[ ] args) 
     {
          byte[ ] ctx = new byte[4000];
          byte[ ] byteArray = new byte[80];
          String tableName;
          String columnName;
          String dataCondition;
          demoProgram client = new demoProgram( ); 
          
          try {
               client.connect( );
               System.out.println("after Connected");
               
               OrdAudio audioObj = new OrdAudio(client.connection);
               
               System.out.println("Enter your search parameters");
               System.out.println("Enter table Name");
               System.in.read(byteArray);
               tableName = new String(byteArray);
               StringBuffer strBuffer = new StringBuffer(
                    tableName.substring(0,tableName.indexOf("\n")));
               tableName = new String(strBuffer);
               
               System.out.println("Enter column Name");
               System.in.read(byteArray);
               columnName = new String(byteArray);
               strBuffer = new StringBuffer(
                    columnName.substring(0,columnName.indexOf("\n")));
               columnName = new String(strBuffer);
               
               System.out.println("Enter Data Condition");
               System.in.read(byteArray);
               dataCondition= new String(byteArray);
               strBuffer = new StringBuffer(
                    dataCondition.substring(0,dataCondition.indexOf("\n")));
               dataCondition = new String(strBuffer);
               
               audioObj.setBindParams(tableName, columnName, dataCondition);
               
               audioObj.setProperties(ctx);
               
               audioObj.refresh(true);
               
               System.out.println("Encoding : " + audioObj.getEncoding(ctx));
               System.out.println("Sample Size: " + 
                    audioObj.getSampleSize(ctx));
               System.out.println("Sampling Rate: " + 
                    audioObj.getSamplingRate(ctx));
               System.out.println("Format : " + audioObj.getFormat(ctx));
               
               audioObj.appendToComments(5,"Drama");
               System.out.println("Comments :" + 
                    audioObj.readFromComments(1,5));
               audioObj.appendToComments(11," and Comedy");
               System.out.println("Comments :" + 
                    audioObj.readFromComments(1,16));
               System.out.println("Drama is in :" + 
                    audioObj.locateInComment("Drama",1,1));
               System.out.println("Comment Length output  : " + 
                    audioObj.getCommentLength( ));
               
               System.out.println("MimeType: " + audioObj.getMimeType( )); 
               
               System.out.println("SourceType : " + audioObj.getSourceType( ));
               System.out.println("SourceLocation : " + 
                    audioObj.getSourceLocation( ));
               System.out.println("SourceName: " + audioObj.getSourceName( ));
               System.out.println("SourceInformation: " + 
                    audioObj.getSource( ));
               
               System.out.println(" getContentLength output  : " + 
                    audioObj.getContentLength(ctx));
               audioObj.getDataInFile("output.dat");
               
               audioObj.setDescription("Classic Collection");
               System.out.println("Description : " + 
                    audioObj.getDescription( ));
               
               try {
                    audioObj.getCompressionType(ctx);
               }
               catch (Exception e) {
                    System.out.println("Exception raised in 
                         getCompressionType");
               }
               
               audioObj.flush( );
               
               client.connection.close( );
               
               System.out.println("after close");
          }
          catch (Exception e) {
               try {
                    System.out.println("Exception : " + e);
                    client.connection.close( );
               } catch(Exception ex) {
                    System.out.println("Close Connection Exception : " + ex);
               }
          }
     }
}

2.1.2 Connecting to the Database

The following sections will provide more information on the preceding code. Throughout these examples, audioObj will refer to an audio object created on the client side.

Example 2-1 calls the connect( ) method, which makes a connection to the database.

Example 2-1 Connecting to the Database

client.connect( );

Example 2-2 shows the connect( ) method.

Example 2-2 The connect( ) Method

public void connect( ) throws Exception
{
     String connectString;
     Class.forName ("oracle.jdbc.driver.OracleDriver");
     connectString = "jdbc:oracle:oci8:@";
     connection = DriverManager.getConnection(connectString,"ORDMEDIADEMO", 
          "ORDMEDIADEMO");
     connection.setAutoCommit(false);
}

2.1.3 Binding to the Database Parameters

Example 2-3 shows the code used to create an IPC connection. This includes obtaining the table name, column name, and data condition for the server-side object to which you will connect, as well as binding to these parameters.

Example 2-3 Binding to the Database Parameters

System.out.println("Enter your search parameters");
System.out.println("Enter table Name");
System.in.read(byteArray);
tableName = new String(byteArray);
StringBuffer strBuffer = new StringBuffer(
     tableName.substring(0,tableName.indexOf("\n")));
tableName = new String(strBuffer);

System.out.println("Enter column Name");
System.in.read(byteArray);
columnName = new String(byteArray);
strBuffer = new StringBuffer(
     columnName.substring(0,columnName.indexOf("\n")));
columnName = new String(strBuffer);

System.out.println("Enter Data Condition");
System.in.read(byteArray);
dataCondition= new String(byteArray);
strBuffer = new StringBuffer(
     dataCondition.substring(0,dataCondition.indexOf("\n")));
dataCondition = new String(strBuffer);

audioObj.setBindParams(tableName, columnName, dataCondition);

2.1.4 Setting the Properties

Example 2-4 sets the properties on the server side.

Example 2-4 Setting the Properties

audioObj.setProperties(ctx);

where:

2.1.5 Refreshing the Client-Side Object

Example 2-5 refreshes the client-side object with the attribute values from the client-side object.

Example 2-5 Refreshing the Client-Side Object

audioObj.refresh(true);

where:

2.1.6 Performing Basic Operations on the Client Object

The following examples demonstrate how the Java client program performs basic operations upon the ORDAudio object. Not all operations included in the complete example in Section 2.1.1 are shown here.

Example 2-6 gets the encoding, sample size, sampling rate, and format of the audio data.

Example 2-6 Getting Audio Attributes

System.out.println("Encoding : " + audioObj.getEncoding(ctx));
System.out.println("Sample Size: " + audioObj.getSampleSize(ctx));
System.out.println("Sampling Rate: " + audioObj.getSamplingRate(ctx));
System.out.println("Format : " + audioObj.getFormat(ctx));

where:

Example 2-7 performs operations upon the comments information.

Example 2-7 Comments Operations

audioObj.appendToComments(5,"Drama");
System.out.println("Comments :" + audioObj.readFromComments(1,5));
audioObj.appendToComments(11," and Comedy");
System.out.println("Comments :" + audioObj.readFromComments(1,16));
System.out.println("Drama is in :" + audioObj.locateInComment("Drama",1,1));
System.out.println("Comment Length output  : " + audioObj.getCommentLength( ));

See "appendToComments( ) Method", "readFromComments( ) Method", "locateInComment( ) Method", and "getCommentLength( ) Method", all of which are in Chapter 5, for more information.

Example 2-8 gets the MIME type information.

Example 2-8 Getting the MIME Type Information

System.out.println("MimeType: " +audioObj.getMimeType( )); 

Example 2-9 gets source information.

Example 2-9 Getting Source Information

System.out.println("SourceType : " + audioObj.getSourceType( ));
System.out.println("SourceLocation : " + audioObj.getSourceLocation( ));
System.out.println("SourceName: " + audioObj.getSourceName( ));
System.out.println("SourceInformation: " +audioObj.getSource( ));

Example 2-10 sets and gets information pertaining to the audio content.

Example 2-10 Setting and Getting Information Pertaining to the Audio Content

System.out.println("getContentLength output : " + 
     audioObj.getContentLength(ctx));
audioObj.getDataInFile("output.dat");
audioObj.setDescription("Classic Collection");
System.out.println("Description : " + audioObj.getDescription( ));

where:

2.1.7 Flushing the Client Object

Example 2-11 flushes the client object to the server side.

Example 2-11 Flushing the Client Object

audioObj.flush( );

2.1.8 Closing the Connection

Example 2-12 closes the connection to the database.

Example 2-12 Closing the Connection

client.connection.close( );

2.2 Example of Image APIs

For the image example, the test class given here invokes the following APIs:

Reference information on the image methods used in this example is presented in Chapter 6.

2.2.1 Complete Image Example

import java.sql.*;
import oracle.sql.*;
import java.io.*;
import oracle.jdbc.driver.*;
import oracle.ord.media.*;

public class doc {
     public Connection con;
     
     doc( ) {} 
     
     // Get connection to server
     //
     public void connect( ) throws Exception 
     {
          String connectString; 
          Class.forName ("oracle.jdbc.driver.OracleDriver");
          connectString = "jdbc:oracle:oci8:@"; // for IPC
          DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver( ));
          con = DriverManager.getConnection(connectString,"SCOTT", "TIGER");
          con.setAutoCommit(false);
     }
     
     // main method: calls various OrdImage methods
     //
     public static void main(String[ ] args) 
     {
          doc cl = new doc( ); 
          byte[ ] ctx = new byte[4000];
          try {
               cl.connect( );
               
               // Create new client-side OrdImage object
               //
               OrdImage imgObj = new OrdImage(cl.con);
               
               // Specify table, column, and "where" clause to use when
               // fetching data into imgObj.
               //
               imgObj.setBindParams("ordimgtab", "imagebfile", " ID = 1 ");
               
               //Populate imgObj attributes with values from the database,
               //locking the row for update.
               //
               imgObj.refresh(true);
               
               // Display some attribute values.
               //
               System.out.println("ContentFormat : " + 
                    imgObj.getContentFormat( ));
               System.out.println("FileFormat : " + imgObj.getFormat( ));
               
               // Get the BFILE object on which imgObj is based, and display
               // the name of the file containing the image data.
               //
               BFILE bfile = imgObj.getBFILE( );
               String fileNameStr = bfile.getName( );
               if (fileNameStr.length( ) > 0)
               {
                    System.out.println("bfile.getName = ");
                    System.out.println(fileNameStr);
               }
               
               // Create and populate another client-side OrdImage object.
               //
               OrdImage imgObj2 = new OrdImage(cl.con);
               imgObj2.setBindParams("ordimgtab", "imageblob", " ID = 1 ");
               imgObj2.refresh(true);
               
               // Copy the contents of imgObj to imgObj2.
               //
               imgObj.copy(imgObj2);
                 
               //Import data from BFILE into BLOB in source of server-side
               //object.
               //
               imgObj2.importData(ctx);
               imgObj2.setLocal( );
               
               // Get the BLOB containing image data and print its length.
               //
               BLOB blob = imgObj2.getContent( );
               System.out.println("blob.length = " + blob.length( ));
               
               // Set server object properties from the data in the BLOB.
               //
               imgObj2.setProperties( );
               
               // Display new attribute values.
               //
               System.out.println("fileFormat : " + imgObj2.getFormat( ));
               System.out.println("width : " + imgObj2.getWidth( ));
               System.out.println("height : " + imgObj2.getHeight( ));
               System.out.println("contentFormat : " + 
                    imgObj2.getContentFormat( ));
               System.out.println("compressionFormat : " + 
                    imgObj2.getCompressionFormat( ));
               
               // Get all attribute settings in a single string.
               //
               System.out.println("getAllAttributesAsString : " + 
                    imgObj2.getAllAttributesAsString( ));
               
               // Check that attribute values match those in BLOB data.
               //
               imgObj2.setProperties( );
               if (imgObj2.checkProperties( ) == true)
                    System.out.println("Attribute and data properties match.");
               else
                    System.out.println("Attribute and data properties do not 
                         match.");
               
               // Make modifications to image data based on command in
               //cmdStr.
               //
               String cmdStr = "scale=2 cut=100 100 100 100";
               imgObj2.process(cmdStr);
               imgObj2.setProperties( );
               System.out.println("After imgObj2.process(scale=2 cut=100 100 
                    100 100)");
               System.out.println("width : " + imgObj2.getWidth( ));
               System.out.println("height : " + imgObj2.getHeight( ));
               
               // Load image data from local client file into imgObj2 BLOB.
                    if (imgObj2.loadData("/OraHome/ord/img/demo/imgdemo.dat"))
               {
                    System.out.println("loadData succeeded");
                    blob = imgObj2.getContent( );
                    System.out.println("blob.length = " + blob.length( ));
                    imgObj2.setProperties( );
               }
               else 
                    System.out.println("loadData failed ***");
          } 
          catch (Exception e) 
          {
               System.out.println("Exception : " + e);
          }
          
          //Set the MIME type of imgObj2.
          imgObj2.setMimeType("image/bmp");
          
          //Save the new MIME type setting in the server object.
          imgObj2.flush( );
          
          finally
          {
               // Always close the connection before exiting.
               //
               try
               {
                    cl.con.close( );
               } 
               catch(Exception ex) 
               {
                    System.out.println("Close Connection Exception : " + ex);
               }
          }
     }
}

2.2.2 Connecting to the Database

The following sections will provide more information about the preceding code. Throughout these examples, imgObj and imgObj2 will refer to ORDImage objects created on the client side.

Example 2-13 calls the connect( ) method, which makes a connection to the database.

Example 2-13 Connecting to the Database

doc cl = new doc( );
.
.
.
cl.connect( );

Example 2-14 shows the connect( ) method.

Example 2-14 The connect( ) Method

public void connect( ) throws Exception 
{
     String connectString; 
     Class.forName ("oracle.jdbc.driver.OracleDriver");
     connectString = "jdbc:oracle:oci8:@"; // for IPC
     DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver( ));
     con = DriverManager.getConnection(connectString,"SCOTT", "TIGER");
     con.setAutoCommit(false);
}

where:

2.2.3 Binding to the Database Parameters

Example 2-15 shows the code used to bind to the database parameters. This includes obtaining the table name, column name, and data condition for the server-side object to which you will connect, as well as binding to the parameters.

Example 2-15 Binding to the Database Parameters

imgObj.setBindParams("ordimgtab", "imagebfile", " ID = 1 ");

where:

2.2.4 Refreshing the Client-Side Object

Example 2-16 refreshes the client-side object.

Example 2-16 Refreshing the Client-Side Object

imgObj.refresh(true);

where:

2.2.5 Performing Basic Operations on the Client Object

The following examples demonstrate how the Java client program performs some basic operations upon the ORDImage object. Not all operations included in the complete example in Section 2.2.1 are shown here.

Example 2-17 gets the content format and file format of the image data.

Example 2-17 Getting Specific Image-Related Attributes

System.out.println("contentFormat : " + imgObj2.getContentFormat( ));
System.out.println("fileFormat : " + imgObj2.getFormat( ));

Example 2-18 gets a content BFILE that holds image data and prints the file name to the screen.

Example 2-18 Getting the Content BFILE

BFILE bfile = imgObj.getBFILE( );
String fileNameStr = bfile.getName( );
if (fileNameStr.length( ) > 0)
{
     System.out.println("bfile.getName = ");
     System.out.println(fileNameStr);
}

Example 2-19 copies the image data from the client-side ORDImage object source to the server-side ORDImage object source.

Example 2-19 Copying Image Data

imgObj.copy(imgObj2);

Example 2-20 gets a content BLOB that holds image data.

Example 2-20 Getting the Content BLOB

BLOB blob = imgObj2.getContent( );

Example 2-21 gets all the attributes of the image data as a String.

Example 2-21 Getting All Image-Related Attributes as a String

System.out.println("getAllAttributesAsString : " + 
     imgObj2.getAllAttributesAsString( ));

Example 2-22 checks that the values of the database ORDImage object are consistent with the values stored in the content header.

Example 2-22 Checking Properties

imgObj2.setProperties( );
if (imgObj2.checkProperties( ) == true)
     System.out.println("Attribute and data properties match");
else
     System.out.println("Attribute and data properties do not match");

Example 2-23 executes the given commands on the server-side ORDImage object; that is, it makes the modifications included in cmdStr on the image data stored in imgObj2. See "process( ) Method" in Chapter 5 for more information.

Example 2-23 Processing Commands on the Server-Side Object

String cmdStr = "scale=2, cut=100 100 100 100";
imgObj2.process(cmdStr);

Example 2-24 sets the MIME type of the client-side ORDImage object.

Example 2-24 Setting the MIME Type

imgObj2.setMimeType("image/bmp");

where:

2.2.6 Setting the Properties

Example 2-25 updates the server-side ORDImage object with the setProperties( ) method, which updates the server-side ORDImage attributes to reflect the changes made on the client side.

Example 2-25 Setting the Properties

imgObj2.setProperties( );

2.2.7 Flushing the Client Object

Example 2-26 updates the server-side ORDImage object by flushing the client object to the server side.

Example 2-26 Flushing the Client Object

imgObj2.flush( );

2.2.8 Closing the Connection

Example 2-27 closes the connection to the database.

Example 2-27 Closing the Connection

cl.con.close( );



Prev

Next
Oracle
Copyright © 1999 Oracle Corporation.

All Rights Reserved.

Library

Product

Contents

Index