Class definition
The class for the C opaque type, charattrUDT in
the following example, must implement the SQLData interface:
import java.sql.*; import com.informix.jdbc.*; /* * C struct of charattr_udt: * * typedef struct charattr_type * { * char chr1[4+1]; * mi_boolean bold; // mi_boolean (1 byte) * mi_smallint fontsize; // mi_smallint (2 bytes) * } * charattr; * * typedef charattr charattr_udt; * */ public class charattrUDT implements SQLData { private String sql_type = "charattr_udt"; // an ASCII character/a multibyte character, and is null-terminated. public String chr1; // Is the character in boldface? public boolean bold; // font size of the character public short fontsize; public charattrUDT() { } public charattrUDT(String chr1, boolean bold, short fontsize) { this.chr1 = chr1; this.bold = bold; this.fontsize = fontsize; } public String getSQLTypeName() { return sql_type; } // reads a stream of data values and builds a Java object public void readSQL(SQLInput stream, String type) throws SQLException { sql_type = type; chr1 = ((IfmxUDTSQLInput)stream).readString(5); bold = stream.readBoolean(); fontsize = stream.readShort(); } // writes a sequence of values from a Java object to a stream public void writeSQL(SQLOutput stream) throws SQLException { ((IfmxUDTSQLOutput)stream).writeString(chr1, 5); stream.writeBoolean(bold); stream.writeShort(fontsize); } // overides Object.equals() public boolean equals(Object b) { return (chr1.equals(((charattrUDT)b).chr1) && bold == ((charattrUDT)b).bold && fontsize == ((charattrUDT)b).fontsize); } public String toString() { return "chr1=" + chr1 + " bold=" + bold + " fontsize=" + fontsize; } }
In your JDBC application, a custom type map must map the
SQL-type name charattr_udt to the charattrUDT class:
java.util.Map customtypemap = conn.getTypeMap(); if (customtypemap == null) { System.out.println("\n***ERROR: typemap is null!"); return; } customtypemap.put("charattr_udt", Class.forName("charattrUDT"));