Oracle Visual Information Retrieval Java Classes User's Guide and Reference
Release 8.1.7

Part Number A85333-01

Library

Product

Contents

Index

Go to previous page Go to next page

2
Program Examples Using Java Classes

This chapter provides a full-length example of user-defined classes using Visual Information Retrieval Java Classes. A sample SQL script that demonstrates how to set up a schema on your database server is also included.

This code will not necessarily match the code shipped as VirExample.java with the Java Classes installation. If you want to run an example on your system, use the files provided with the 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. 


The Visual Information Retrieval example contains user-defined methods that use SQL, JDBC, and Visual Information Retrieval Java Classes APIs to perform the following operations:

2.1 VirExample.sql

Example 2-1 shows the contents of VirExample.sql.

Example 2-1 Contents of VirExample.sql

set echo on

-- Please change system password.
connect system/manager
drop user VIRUSER cascade;

[1] grant connect,resource to VIRUSER identified by VIRUSER;

-- Replace 'C:\Oracle\Ora81' with your Oracle home directory
[2] create or replace directory ORDVIRDIR as 'C:\Oracle\Ora81\ord\vir\demo';
grant read on directory ORDVIRDIR to public with grant option;

[3] connect VIRUSER/VIRUSER;

[4] create table ordvirtab(id number, image ORDSYS.ORDVir, image2 
ORDSYS.ORDVir);

-- Note - the ORDVir.init method was added in 8.1.7.  If you are running 
-- against an older release of Visual Information Retrieval and Oracle,
-- you will have to modify the following INSERT statements to use the
-- OrdVir default constructor.
--
[5] insert into ordvirtab values
(1, ORDSYS.ORDVir.init( ),
    ORDSYS.ORDVir.init( ));

insert into ordvirtab values
(2, ORDSYS.ORDVir.init( ),
    ORDSYS.ORDVir.init( ));

insert into ordvirtab values
(3, ORDSYS.ORDVir.init( ),
    ORDSYS.ORDVir.init( ));

insert into ordvirtab values
(4, ORDSYS.ORDVir.init( ),
    ORDSYS.ORDVir.init( ));

[5] insert into ordvirtab values
(5, ORDSYS.ORDVir.init('file','ORDVIRDIR','stripes1.bmp'),
    ORDSYS.ORDVir.init('file','ORDVIRDIR','stripes2.bmp'));

insert into ordvirtab values
(6, ORDSYS.ORDVir.init('file','ORDVIRDIR','lcolor1.bmp'),
    ORDSYS.ORDVir.init('file','ORDVIRDIR','lcolor2.bmp'));

commit;

set echo off

exit;

The SQL statements in VirExample.sql perform the following operations:

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

  2. Create a directory named ORDVIRDIR and set the appropriate permissions. If necessary, edit the name of the directory to match your file system.

  3. Connect to the database as VIRUSER.

  4. Create a table named ORDVIRTAB, which contains a column of numbers and two columns of ORDVir objects.

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

  6. Using the init method, add two rows with two objects in each row with values.

The ORDVir.init method was added in release 8.1.7. If you are running against a previous release of Visual Information Retrieval and Oracle8i, you will have to modify the INSERT statements in steps 5 and 6 to use the ORDVir default constructor.

See Oracle8i Visual Information Retrieval User's Guide and Reference for more information on the init method.

2.2 VirExample.java

Section 2.2.1 through Section 2.2.9 show the methods contained in the VirExample.java sample file.

2.2.1 main( ) Method

Example 2-2 shows the main( ) method.

Example 2-2 main( ) Method

public static void main (String args[ ]){
     byte[ ] ctx = new byte[4000];
     OracleConnection con = null;
     try{
          VirExample ve = new VirExample( );
          [1] con = ve.connect( );
          //Only include the following line 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.virCompatibilityInit(con);
          [3] ve.setPropertiesExample(con);
          ve.displayPropertiesExample(con);
          ve.fileBasedExample(con);
          ve.streamBasedExample(con);
          ve.byteArrayBasedExample(con);
          ve.processExample(con);
          ve.similarExample(con);
          ve.scoreExample(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 not work if your database server is 8.1.6 or earlier. See Section 1.4 for more information.

  3. Calls several methods (also defined in VirExample.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 through Section 2.2.9 will provide information on the methods called from the main( ) method.

2.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

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,"VIRUSER","VIRUSER");
     [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 VIRUSER, and the password VIRUSER. The user name and password were created by VirExample.sql.

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

2.2.3 setPropertiesExample( ) Method

Example 2-4 shows a user-defined method named setPropertiesExample( ) that sets the properties in the application object.

Example 2-4 setPropertiesExample( ) Method

public void setPropertiesExample(OracleConnection con){
     try{
          int index = 0;
          [1] Statement s = con.createStatement( );
          [2] OracleResultSet rs = (OracleResultSet)s.executeQuery
               ("select * from ordvirtab where id = 5 for update");
          [3] while(rs.next( )){
               [4] index = rs.getInt(1);
               [5] OrdVir imgObj = (OrdVir)rs.getCustomDatum
                    (2, OrdVir.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 ordvirtab 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 casts 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 ORDVir object named imgObj. Populates imgObj with the contents of the ORDVir 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 ORDVir object. See "setProperties( )" for a list of the properties values extracted and set.

  7. Calls checkProperties( ) to compare the properties values in the application object attributes 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 ORDVir object with the contents of imgObj.

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

2.2.4 displayPropertiesExample( ) Method

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

Example 2-5 displayPropertiesExample( ) Method

public void displayPropertiesExample(OracleConnection con){
     try{
          int index = 0;
          [1] Statement s = con.createStatement( );
          OracleResultSet rs = (OracleResultSet)s.executeQuery(
               "select * from ordvirtab where id = 5 ");
          while(rs.next( )){
               index = rs.getInt(1);
               OrdVir imgObj = (OrdVir) rs.getCustomDatum(2,
                    OrdVir.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 ORDVir object named imgObj, and populates imgObj 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 fifth row of the database table. This is the same row you operated on in Example 2-4.

  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.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.5 getAllAttributesAsString( ) Method

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

Example 2-6 getAllAttributesAsString( ) Method

public String getAllAttributesAsString (OrdVir 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 object and stores their values in attStr.

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

2.2.6 fileBasedExample( ) Method

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

Example 2-7 fileBasedExample( ) Method

public void fileBasedExample(OracleConnection con){
     try{
          int index = 0;
          [1] Statement s = con.createStatement( );
          OracleResultSet rs = (OracleResultSet)s.executeQuery(
               "select * from ordvirtab where id = 2 for update ");
          while(rs.next( )){
               index = rs.getInt(1);
               OrdVir imgObj = (OrdVir) rs.getCustomDatum(2,
                    OrdVir.getFactory( ));
               [2] imgObj.loadDataFromFile("virdemo1.dat");
               [3] imgObj.setProperties( );
               [4] imgObj.getDataInFile("fileexample.dat");
               [5] OraclePreparedStatement stmt1 = (OraclePreparedStatement)
                    con.prepareCall("update ordvirtab 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 operation:

  1. Creates a statement, a local OracleResultSet, and a local ORDVir object named imgObj, and populates imgObj 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. Uses the loadDataFromFile( ) method to load the media data from the local file virdemo1.dat into the database ORDVir object and into imgObj. This also sets the local field on imgObj, but not the database object.

  3. Calls the setProperties( ) method to extract property values from the media data and set them in the application ORDVir object. See "setProperties( )" for a list of the property values extracted and set.

  4. Uses the getDataInFile( ) method to get the media data from the application ORDVir 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 ORDVir 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.7 streamBasedExample( ) Method

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

Example 2-8 streamBasedExample( ) Method

public void streamBasedExample(OracleConnection con){
     try{
          int index = 0;
          [1] Statement s = con.createStatement( );
          OracleResultSet rs = (OracleResultSet)s.executeQuery(
               "select * from ORDVIRTAB where id = 3 for update ");
          while(rs.next( )){
               index = rs.getInt(1);
               OrdVir imgObj = (OrdVir) rs.getCustomDatum(2,
                    OrdVir.getFactory( ));
               [2] FileInputStream fStream = new FileInputStream
                    ("virdemo1.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 ordvirtab 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 ORDVir object named imgObj, and populates imgObj 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. Creates a new FileInputStream object. This input stream contains the contents of a local file named virdemo1.dat.

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

  4. Closes the input stream.

  5. Calls the setProperties( ) method to extract property values from the media data and sets them in the application ORDVir object. See "setProperties( )" for a list of the property values extracted and set.

  6. Creates a new InputStream object named inpStream. Calls the getDataInStream( ) method to get the media data from the application ORDVir 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, and then break out of the loop, if numRead is less than 32300 (that is, if not all of 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 you have already successfully read 32300 bytes of data, 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 repeats steps 10 and 11.

  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 ORDVir 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.8 byteArrayBasedExample( ) Method

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

Example 2-9 byteArrayBasedExample( ) Method

public void byteArrayBasedExample(OracleConnection con){
     try{
          int index = 0;
          [1] Statement s = con.createStatement( );
          OracleResultSet rs = (OracleResultSet)s.executeQuery
               ("select * from ORDVIRTAB where id = 4 for update ");
          while(rs.next( )){
               index = rs.getInt(1);
               OrdVir imgObj = (OrdVir) rs.getCustomDatum(2,
                    OrdVir.getFactory( ));
               [2] File ff = new File("virdemo1.dat");
               int fileLength = (int) ff.length( );
               byte[ ] data = new byte[fileLength];
               [3] FileInputStream fStream = new
                    FileInputStream("virdemo1.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 ordvirtab 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 ORDVir object named imgObj, and populates imgObj 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 fourth row of the database table.

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

  3. Creates a new FileInputStream object. This input stream contains the contents of virdemo1.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 ORDVir object and into imgObj. This also sets the local field on imgObj, but not the database object.

  6. Closes the input stream.

  7. Calls the setProperties( ) method to extract property values from the media data and set them in the application ORDVir object. See "setProperties( )" for a list of the property values extracted and set.

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

  9. Get the length of resArr and print it to the screen to verify the success of the loading.

  10. Creates and executes a SQL statement that will update the database ORDVir 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.9 processExample( ) Method

Example 2-10 shows a user-defined method named processExample( ) that uses the process( ) and processCopy( ) methods to manipulate the media data in the application object.

Example 2-10 processExample( ) Method

public void processExample(OracleConnection con){
     try{
          int index1 = 0;
          [1] Statement s1 = con.createStatement( );
          OracleResultSet rs1 = (OracleResultSet)s1.executeQuery
               ("select * from ORDVIRTAB where id = 2 for update ");
          while(rs1.next( )){
               index1 = rs1.getInt(1);
               OrdVir imgObj = (OrdVir) rs1.getCustomDatum(2,
                    OrdVir.getFactory( ));
               [2] OrdVir imgObj2 = (OrdVir) rs1.getCustomDatum(3,
                    OrdVir.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 ordvirtab 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 ORDVir object named imgObj, and populates imgObj 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. The database ORDVir object is named image.

  2. Creates a local ORDVir object named imgObj2. Populates imgObj2 with the contents of the ORDVir object in the third column of the current row in the OracleResultSet. This database ORDVir 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.5 for more information on the getAllAttributesAsString( ) method.

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

  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 ORDVir objects with the contents of imgObj and imgObj2.

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

2.2.10 similarExample( ) Method

Example 2-11 shows a user-defined method named similarExample( ) that uses the analyze( ) and similar( ) methods to compare two application objects.

Example 2-11 similarExample( ) Method

public void similarExample(OracleConnection con){
     try{
          int index1 = 0;
          [1] Statement s1 = con.createStatement( );
          OracleResultSet rs1 = (OracleResultSet)s1.executeQuery
               ("select * from ordvirtab where id = 5 for update ");
          while(rs1.next( )){
               index1 = rs1.getInt(1);
               OrdVir imgObj1 = (OrdVir) rs1.getCustomDatum
                    (2, OrdVir.getFactory( ));
               [2] OrdVir imgObj2 = (OrdVir) rs1.getCustomDatum
                    (3, OrdVir.getFactory( ));
               [3] try{
                    imgObj1.analyze( );
                    imgObj2.analyze( );
               }
               [4] catch (Exception e){
                    System.out.println("Exception raised in analyze" + e );
               }
               [5] if (1 == imgObj1.similar(imgObj2.getSignature( ),
                    "texture=50,structure=50", 5)){
                    System.out.println("imgObj1 is similar to imgObj2");
               }
               else{
                    System.out.println("imgObj1 is not similar to imgObj2");
               }
               [6] OraclePreparedStatement stmt1 = (OraclePreparedStatement)
                    con.prepareCall("update ordvirtab set image = ?,
                    image2 = ? where id = " + index1);
               stmt1.setCustomDatum(1,imgObj1);
               stmt1.setCustomDatum(2,imgObj2);
               stmt1.execute( );
               stmt1.close( ) ;
          }
          rs1.close( );
          s1.close( );
     }
     [7] catch(Exception e){
          System.out.println("Exception raised: " + e);
     }
     System.out.println("successful");
}

The similarExample( ) method performs the following operations:

  1. Creates a statement, a local OracleResultSet, and a local ORDVir object named imgObj, and populates imgObj 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 fifth row of the database table. The database ORDVir object is named image.

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

  3. Uses the analyze( ) method to generate signatures for image and image2.

  4. Handles any errors or exceptions raised by the analyze( ) method.

  5. Compares the two ORDVir objects with the similar( ) method, with equal weight being placed on the texture and structure attributes. If the score is lower than 5, the images match and the method will return 1. If similar( ) returns 1, a success message is printed to the screen; if it returns 0, a failure method is printed to the screen.

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

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

2.2.11 scoreExample( ) Method

Example 2-12 shows a user-defined method named scoreExample( ) that uses the analyze( ) and score( ) methods to create a score for an application object.

Example 2-12 scoreExample( ) Method

public void scoreExample(OracleConnection con){
     try{
          int index1 = 0;
          [1] Statement s1 = con.createStatement( );
          OracleResultSet rs1 = (OracleResultSet)s1.executeQuery
               ("select * from ordvirtab where id = 6 for update ");
          while(rs1.next( )){
               index1 = rs1.getInt(1);
               OrdVir imgObj1 = (OrdVir) rs1.getCustomDatum
                    (2, OrdVir.getFactory( ));
               [2] OrdVir imgObj2 = (OrdVir) rs1.getCustomDatum
                    (3, OrdVir.getFactory( ));
               [3] try{
                    imgObj1.analyze( );
                    imgObj2.analyze( );
               }
               [4] catch (Exception e){
                    System.out.println("Exception raised in analyze" + e );
               }
               [5] System.out.println("Score = " + imgObj1.score
                    (imgObj2.getSignature( ),"globalColor=100"));
               [6] OraclePreparedStatement stmt1 = (OraclePreparedStatement)
                    con.prepareCall("update ordvirtab set image = ?,
                    image2 = ? where id = " + index1);
               stmt1.setCustomDatum(1,imgObj1);
               stmt1.setCustomDatum(2,imgObj2);
               stmt1.execute( );
               stmt1.close( ) ;
          }
          rs1.close( );
          s1.close( );
     }
     [7] catch(Exception e){
          System.out.println("Exception raised: " + e);
     }
     System.out.println("successful");
}

The similarExample( ) method performs the following operations:

  1. Creates a statement, a local OracleResultSet, and a local ORDVir object named imgObj, and populates imgObj 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 sixth row of the database table. The database ORDVir object is named image.

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

  3. Uses the analyze( ) method to generate signatures for image and image2.

  4. Handles any errors or exceptions raised by the analyze( ) method.

  5. Generates the score of the comparison of the signatures of image and image2 with the score( ) method, and prints the score to the screen. The only attribute being compared is globalColor.

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

  7. 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