The opaque type

The following JDBC client application installs the class Circle (which is packaged in Circle.jar) as an opaque type in the system catalog. Applications can then use the opaque type Circle as a data type in SQL statements:
import java.sql.*;
import java.lang.reflect.*;


public class PlayWithCircle
{
    String dbname = "test";
    String url = null;
    Connection conn = null;

    public static void main (String args[])
    {
        new PlayWithCircle(args);
    }

    PlayWithCircle(String args[])
    {
        System.out.println("----------------");
        System.out.println("- Start - Demo 1");
        System.out.println("----------------");

        // -----------
        // Getting URL
        // -----------
        if (args.length == 0)
            {
            System.out.println("\n***ERROR: connection URL must be provided " +
                               "in order to run the demo!");
            return;
            }
        url = args[0];

        // --------------
        // Loading driver
        // --------------
        try
            {
            System.out.print("Loading JDBC driver...");
            Class.forName("com.informix.jdbc.IfxDriver");
            System.out.println("ok");
            }
        catch (java.lang.ClassNotFoundException e)
            {
            System.out.println("\n***ERROR: " + e.getMessage());
            e.printStackTrace();
            return;
            }
 
        // ------------------
        // Getting connection
        // ------------------
        try
            {
            System.out.print("Getting connection...");
            conn = DriverManager.getConnection(url);
            System.out.println("ok");
            }
        catch (SQLException e)
            {
            System.out.println("URL = '" + url + "'");
            System.out.println("\n***ERROR: " + e.getMessage());
            e.printStackTrace();
            return;
            }
        System.out.println();

        // -------------------
        // Setup UDT meta data
        // -------------------
        Method areamethod = null;
        try
            {
            Class c = Class.forName("Circle");
            areamethod = c.getMethod("area", new Class[] {c});
            }
        catch (ClassNotFoundException e)
            {
            System.out.println("Cannot get Class: " + e.toString());
            return;
            }
        catch (NoSuchMethodException e)
            {
            System.out.println("Cannot get Method: " + e.toString());
            return;
            }
 
        UDTMetaData mdata = null;
        try
            {
            System.out.print("Setting mdata...");
            mdata = new UDTMetaData();
            mdata.setSQLName("circle");
            mdata.setLength(24);
            mdata.setAlignment(UDTMetaData.EIGHT_BYTE);
            mdata.setUDR(areamethod, "area");
            mdata.setJarFileSQLName("circle_jar");
            System.out.println("ok");
            }
        catch (SQLException e)
            {
            System.out.println("\n***ERROR: " + e.getMessage());
            return;
            }
 
        // -------------------------------
        // Install the UDT in the database
        // -------------------------------
        UDTManager udtmgr = null;
        try
            {
            udtmgr = new UDTManager(conn);
 
            System.out.println("\ncreateJar()");
            String jarfilename = udtmgr.createJar(mdata,
                new String[] {"Circle.class"}); // jarfilename = circle.jar
            System.out.println("   jarfilename = " + jarfilename);
 
            System.out.println("\nsetJarTmpPath()");
            udtmgr.setJarTmpPath("/tmp");
 
            System.out.print("\ncreateUDT()...");
            udtmgr.createUDT(mdata,
            "/vobs/jdbc/demo/tools/udtudrmgr/" + jarfilename, "Circle", 0);
            System.out.println("ok");
            }
        catch (SQLException e)
            {
            System.out.println("\n***ERROR: " + e.getMessage());
            return;
            }
        System.out.println();

        // ---------------
        // Now use the UDT
        // ---------------
        try
        {
            String s = "drop table tab";
            System.out.print(s + "...");
            Statement stmt = conn.createStatement();
            int count = stmt.executeUpdate(s);
            stmt.close();
            System.out.println("ok");
        }
        catch ( SQLException e)
        {
            // -206 The specified table (%s) is not in the database.
            if (e.getErrorCode() != -206)
                {
                System.out.println("\n***ERROR: " + e.getMessage());
                return;
                }
            System.out.println("ok");
        }
 
        executeUpdate("create table tab (c circle)");
 
        // test DEFAULT Input function
        executeUpdate("insert into tab values ('10 10 10')");
 
        // test DEFAULT Output function
        try
            {
            String s = "select c::lvarchar from tab";
            System.out.println(s);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(s);
            if (rs.next())
                {
                String c = rs.getString(1);
                System.out.println("   circle = '" + c + "'");
                }
            rs.close();
            stmt.close();
            }
        catch (SQLException e)
            {
            System.out.println("***ERROR: " + e.getMessage());
            }
        System.out.println();
 
        // test DEFAULT Send function
        try
            {
            // setup type map before using getObject() for UDT data.
            java.util.Map customtypemap = conn.getTypeMap();
            System.out.println("getTypeMap...ok");
            if (customtypemap == null)
                {
                System.out.println("***ERROR: map is null!");
                return;
                }
            customtypemap.put("circle", Class.forName("Circle"));
            System.out.println("put...ok");
 
            String s = "select c from tab";
            System.out.println(s);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(s);
            if (rs.next())
                {
                Circle c = (Circle)rs.getObject(1, customtypemap);
                System.out.println("   c.x = " + c.x);
                System.out.println("   c.y = " + c.y);
                System.out.println("   c.radius = " + c.radius);
                }
            rs.close();
            stmt.close();
            }
        catch (SQLException e)
            {
            System.out.println("***ERROR: " + e.getMessage());
            }
        catch (ClassNotFoundException e)
            {
            System.out.println("***ERROR: " + e.getMessage());
            }
        System.out.println();
 
        // test user's non-support UDR
        try
            {
            String s = "select area(c) from tab";
            System.out.println(s);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(s);
            if (rs.next())
                {
                double a = rs.getDouble(1);
                System.out.println("   area = " + a);
                }
            rs.close();
            stmt.close();
            }
        catch (SQLException e)
            {
            System.out.println("***ERROR: " + e.getMessage());
            }
        System.out.println();
 
        executeUpdate("drop table tab");
 
        // ------------------
        // Closing connection
        // ------------------
        try
            {
            System.out.print("Closing connection...");
            conn.close();
            System.out.println("ok");
            }
        catch (SQLException e)
            {
            System.out.println("\n***ERROR: " + e.getMessage());
            }
}