Home | Previous Page | Next Page   Working with Opaque Types > Creating Opaque Types and UDRs >

Steps to Creating Opaque Types

Using UDT Manager, you can create a Java opaque type from an existing Java class that implements the SQLData interface. UDT Manager can also help you create a Java opaque type without requiring that you have the Java class ready; you specify the characteristics of the opaque type you want to create, and the UDT Manager facility creates the Java class and then the Java opaque type.

Follow the steps in this section to use the UDTManager classes.

To create an opaque type from an existing Java class
  1. Ensure that the class meets the requirements for conversion to an opaque type.

    For the requirements, see Requirements for the Java Class.

  2. If you do not want to use the default input and output routines provided by the server, write support UDRs for input and output.

    For general information about writing support UDRs, see IBM Informix: User-Defined Routines and Data Types Developer's Guide.

  3. Create a default sbspace on the database server to hold the JAR file that contains the code for the opaque type.

    For information about creating an sbspace, see the Administrator's Guide for your database server and the IBM Informix: J/Foundation Developer's Guide.

  4. Open a JDBC connection.

    Make sure a database object is associated with the connection object. The driver cannot create an opaque type without a database object. For details about creating a connection with a database object, see Connecting to the Database.

  5. Instantiate an UDTManager object and an UDTMetaData object:
    UDTManager udtmgr = new UDTManager(connection);
    UDTMetaData mdata = new UDTMetaData();
  6. Set properties for the opaque type by calling methods in the UDTMetaData object.

    At a minimum, you must specify the SQL name, UDT length, and JAR file SQL name. For an explanation of SQL names, see SQL Names.

    You can also specify the alignment, implicit and explicit casts, and any support UDRs:

    mdata.setSQLName("circle2");
    mdata.setLength(24);
    mdata.setAlignment(UDTMetaData.EIGHT_BYTE)
    mdata.setJarFileSQLName("circle2_jar");
    mdata.setUDR(areamethod, "area");
    mdata.setSupportUDR(input, "input", UDTMetaData.INPUT)
    mdata.setSupportUDR(output, "output",UDTMetaData.OUTPUT)
    mdata.SetImplicitCast(com.informix.lang.IfxTypes.IFX_TYPE_
       LVARCHAR, "input");
    mdata.SetExplicitCast(com.informix.lang.IfxTypes.IFX_TYPE_
       LVARCHAR, "output");
  7. If desired, specify a pathname where the driver should place the JAR file in the database server file system:
    String pathname = "/work/srv93/examples";
    udtmgr.setJarFileTmpPath(pathname);

    Make sure the path exists in the server file system. For more information, see Specifying a JAR File Temporary Path.

  8. Create the opaque type:
    udtmgr.createUDT(mdata, "Circle2.jar", "Circle2", 0);

For additional information on creating an opaque type from existing code, see Creating an Opaque Type from Existing Code.

For a complete code example of using the preceding steps to create an opaque type, see Creating an Opaque Type from an Existing Java Class with UDTManager.

To create an opaque type without an existing Java class
  1. Create a default sbspace on the database server to hold the JAR file that contains the code for the opaque type.

    For information about creating an sbspace, see the Administrator's Guide for your database server and the IBM Informix: J/Foundation Developer's Guide.

  2. Open a JDBC connection.

    Make sure the connection object has a database object associated with it. For details, see Connecting to the Database.

  3. Instantiate a UDTManager object and a UDTMetaData object:
    UDTManager udtmgr = new UDTManager(connection);
    UDTMetaData mdata = new UDTMetaData();
  4. Specify the characteristics of the opaque type by calling methods in the UDTMetaData class:
    mdata.setSQLName("acircle");
    mdata.setLength(24);
    mdata.setFieldCount(3);
    mdata.setFieldName(1, "x");
    mdata.setFieldName(2, "y");
    mdata.setFieldName(3, "radius");
    mdata.setFieldType
       (1,com.informix.lang.IfxTypes.IFX_TYPE_INT);
    mdata.setFieldType
       (2,com.informix.lang.IfxTypes.IFX_TYPE_INT);
    mdata.setFieldType
       (3,com.informix.lang.IfxTypes.IFX_TYPE_INT);
    mdata.setJarFileSQLName("ACircleJar");

    For more information on setting characteristics for opaque types, see Specifying Characteristics for an Opaque Type.

  5. Create the Java file, the class file, and the JAR file:
    mdata.keepJavaFile(true);
    String classname = udtmgr.createUDTClass(mdata);
    String jarfilename = udtmgr.createJar(mdata, new String[] 
       {classname + .class"}); 

    For more information, see Creating the JAR and Class Files.

  6. If desired, specify a pathname where the driver should place the JAR file in the database server file system:
    String pathname = "/work/srv93/examples";
    udtmgr.setJarFileTmpPath(pathname);

    Make sure the path exists in the server file system. For more information, see Specifying a JAR File Temporary Path.

  7. Send the class definition to the database server:
    udtmgr.createUDT(mdata, jarfilename, classname, 0);

    For more information, see Sending the Class Definition to the Database Server.

For a complete code example of using the preceding steps to create an opaque type, see Creating an Opaque Type Without an Existing Java Class.

Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]