Insert data examples

The following example shows an SQL statement that defines a distinct type:
CREATE DISTINCT TYPE mymoney AS NUMERIC(10, 2);
CREATE TABLE distinct_tab (mymoney_col mymoney);
The following is an example of mapping to the base type:
String s = "insert into distinct_tab (mymoney_col) values (?)";
System.out.println(s);
pstmt = conn.prepareStatement(s);

...
BigDecimal bigDecObj = new BigDecimal(123.45);
pstmt.setBigDecimal(1, bigDecObj);
System.out.println("setBigDecimal...ok");
pstmt.executeUpdate();

When you map to the underlying type, HCL OneDB™ JDBC Driver performs the mapping on the client side because the database server provides implicit casting between the underlying type and the distinct type.

You can also map distinct types to Java™ objects that implement the SQLData interface. The following example shows an SQL statement that defines a distinct type:
CREATE DISTINCT TYPE mymoney AS NUMERIC(10,2)
The following code maps the distinct type to a Java object named MyMoney:
import java.sql.*;
import com.informix.jdbc.*;
public class myMoney implements SQLData
{
     private String sql_type = "mymoney";
     public java.math.BigDecimal value;
     public myMoney() { }

     public myMoney(java.math.BigDecimal value)
     
         this.value = value;
     
     public String getSQLTypeName()
     {
         return sql_type;
     {

     public void readSQL(SQLInput stream, String type) throws 
   SQLException
     {
         sql_type = type;
         value = stream.readBigDecimal();
     {

     public void writeSQL(SQLOutput stream) throws SQLException
     {
         stream.writeBigDecimal(value);
     }
     // overides Object.equals()
     public boolean equals(Object b)
     
         return value.equals(((myMoney)b).value);
     }
     public String toString()
     {
         return "value=" + value;
     }
}
...
String s - "insert into distinct_tab (mymoney_col) values (?)";
pstmt = conn.prepareStatement(s);
myMoney mymoney = new myMoney();
mymoney.value = new java.math.BigDecimal(123.45);
pstmt.setObject(1, mymoney);
System.out.println("setObject(myMoney)...ok");
pstmt.executeUpdate();

In this case, you use the setObject() method instead of the setBigDecimal() method to insert data.