Create an opaque type without an existing Java™ class
In this example, the Java™ class MyCircle on
the client is used to create a fixed-length opaque type in the database
server named ACircle. The ACircle opaque type uses the
default support functions provided by the database server:
import java.sql.*;
public class MyCircle
{
String dbname = "test";
String url = null;
Connection conn = null;
public static void main (String args[])
{
new MyCircle(args);
}
MyCircle(String args[])
{
System.out.println("----------------");
System.out.println("- Start - Demo 3");
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;
}
// -------------------
// Setup UDT meta data
// -------------------
UDTMetaData mdata = null;
try
{
mdata = new UDTMetaData();
System.out.print("Setting fields in mdata...");
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);
// set class name if don't want to use the default name
// <udtsqlname>.class
mdata.setClassName("ACircle");
mdata.setJarFileSQLName("ACircleJar");
mdata.keepJavaFile(true);
System.out.println("ok");
}
catch (SQLException e)
{
System.out.println("***ERROR: " + e.getMessage());
return;
}
// --------------------------------------------------------
// create java file for UDT and install UDT in the database
// --------------------------------------------------------
UDTManager udtmgr = null;
try
{
udtmgr = new UDTManager(conn);
System.out.println("Creating .class/.java files - " +
"createUDTClass()");
String classname = udtmgr.createUDTClass(mdata); // generated
//java file is kept
System.out.println(" classname = " + classname);
System.out.println("\nCreating .jar file - createJar()");
String jarfilename = udtmgr.createJar(mdata,
new String[]{"ACircle.class"}); // jarfilename is
// <udtsqlname>.jar
// ie. acircle.jar
System.out.println("\nsetJarTmpPath()");
udtmgr.setJarTmpPath("/tmp");
System.out.print("\ncreateUDT()...");
udtmgr.createUDT(mdata,
"/vobs/jdbc/demo/tools/udtudrmgr/" + jarfilename, "ACircle", 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 acircle)");
// 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(" acircle = '" + c + "'");
}
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());
}
System.out.println("------------------");
System.out.println("- End - UDT Demo 3");
System.out.println("------------------");
}