Home | Previous Page | Next Page   Appendix C. Mapping Data Types > Data Type Mapping for PreparedStatement.setXXX() Extensions >

Using the Mapping Extensions

To direct the driver to map to a certain data type (so there is no ambiguity in UPDATE statements and WHERE clauses), you can use extensions to the PreparedStatement.setXXX() methods. The only data types that might have ambiguity are boolean, lvarchar, text, byte, BLOB, and CLOB.

To use these extended methods, you must cast your PreparedStatement references to IfmxPreparedStatement. For example, the following code casts the statement variable p_stmt to IfmxPreparedStatement to call the IfxSetObject() method and insert the contents of a file as a large object of type CLOB. IfxSetObject() is defined as I:

public void IfxSetObject(int i, Object x, int scale, int ifxType) 
   throws SQLException
public void IfxSetObject(int i, Object x, int ifxType) throws 
   SQLexception

The code is:

File file = new File("sblob_06.dat");
int fileLength = (int)file.length();
byte[] buffer = new byte[fileLength];
FileInputStream fin = new FileInputStream(file);
fin.read(buffer,0,fileLength);
String str = new String(buffer);

writeOutputFile("Prepare");
PreparedStatement p_stmt = myConn.prepareStatement
   ("insert into sblob_t20(c1) values(?)");

writeOutputFile("IfxSetObject");
((IfmxPreparedStatement)p_stmt).IfxSetObject(1,str,30,IfxTypes.IFX
   _TYPE_CLOB);

For the IfmxPreparedStatement.IfxSetObject extension, you cannot simply overload the method signature with an added ifxType parameter, because such overloading creates method ambiguity. You must name the method to IfxSetObject instead.

Using the Extensions for Opaque Types

The extensions for processing opaque types allow your application to specify the return type to which the database server should cast the opaque type before returning it to the client. This is known as prebinding the return value. The methods are:

In the following sections:

setBindColType() Methods

The methods are as follows:

public void setBindColType(int colIndex, int sqltype) throws SQLException;
public void setBindColType(int colIndex, int sqltype, int scale)
   throws SQLException;
public void setBindColType(int colIndex, int sqltype, String name)
   throws SQLException;

The first overloaded method allows applications to specify the output type to be java.sql.DECIMAL or java.sql.NUMERIC; the scale parameter specifies the number of digits to the right of the decimal point. The second overloaded method allows applications to specify the output type to be java.sql.STRUCT, java.sql.ARRAY, java.sql.DISTINCT, or java.sql.JAVA_OBJECT by assigning one of these values to the name parameter.

setBindColIfxType() Methods

The methods are as follows:

public void setBindColIfxType(int colIndex, int ifxtype) throws SQLException;
public void setBindColIfxType(int colIndex, int ifxtype, int scale) 
   throws SQLException;
public void setBindColIfxType(int colIndex, int ifxtype, String name)
   throws SQLException;

The first overloaded method allows applications to specify the output type to be IFX_TYPE_DECIMAL or IFX_TYPE_NUMERIC; the scale parameter specifies the number of digits to the right of the decimal point. The second overloaded method allows applications to specify the output type to be IFX_TYPE_LIST, IFX_TYPE_ROW, IFX_TYPE_MULTISET, IFX_TYPE_SET, IFX_TYPE_UDTVAR, or IFX_TYPE_UDTFIXED by assigning one of these values to the name parameter.

clearBindColType() Method

The method is as follows:

public void clearBindColType() throws SQLException;
Prebinding Example

The following code from the udt_bindCol.java sample program prebinds an opaque type to an Informix VARCHAR and then to a standard Java Integer type. The table used in this example has one int column and one opaque type column and is defined as follows:

create table charattr_tab (int_col int, charattr_col charattr_udt)

The code to select and prebind the opaque type in the charattr_col column is as follows:

String s = "select int_col, charattr_col as cast_udt_to_lvc, " +
    "charattr_col as cast_udt_to_int from charattr_tab order by 1";

pstmt = conn.prepareStatement(s);
            ((IfxPreparedStatement)pstmt).setBindColIfxType(2,IfxTypes.IFX_TYPE_LVARCHAR);
((IfxPreparedStatement)pstmt).setBindColType(3,Types.INTEGER);

ResultSet rs = pstmt.executeQuery();

System.out.println("Fetching data ...");
int curRow = 0;
while (rs.next())
{
     curRow++;
     int intret = rs.getInt("int_col");
     String strret = rs.getString("cast_udt_to_lvc");
     int intret2 = rs.getInt("cast_udt_to_int");
} // end while

Using Other Mapping Extensions

The remaining method signatures are listed next, along with any additional considerations that apply. In each case, the Informix type must be the last parameter to the standard JDBC PreparedStatement.setXXX() interface.

IfmxPreparedStatement.setArray()

public void setArray(int parameterIndex, Array x, int ifxType) 
   throws SQLException

IfmxPreparedStatement.setAsciiStream()

public void setAsciiStream(int i, InputStream x, int length, int 
   ifxType) throws SQLException

When your application is inserting a very large ASCII value into a LONGVARCHAR column, it is sometimes more efficient to send the ASCII value to the server using java.io.InputStream.

IfmxPreparedStatement.setBigDecimal()

public void setBigDecimal(int i, BigDecimal x, int ifxType)
   throws SQLException

IfmxPreparedStatement.setBinaryStream()

public void setBinaryStream(int i, InputStream x, int length, int 
   ifxType) throws SQLException

When your application is inserting a very large binary value into a LONGVARbinary column, it is sometimes more efficient to send the binary value to the server using java.io.InputStream.

IfmxPreparedStatement.setBlob()

public void setBlob(int parameterIndex, Blob x, int ifxType) 
   throws SQLException

IfmxPreparedStatement.setBoolean()

public void setBoolean(int i, boolean x, int ifxType) throws 
   SQLException

IfmxPreparedStatement.setByte()

public void setByte(int i, byte x, int ifxType) throws 
   SQLException

IfmxPreparedStatement.setBytes()

public void setBytes(int i, byte x[], int ifxType) throws 
   SQLException

IfmxPreparedStatement.setCharacterStream()

public void setCharacterStream(int parameterIndex, Reader reader, 
   int length, int ifxType) throws SQLException

When your application is setting a LONGVARCHAR parameter to a very large UNICODE value, it is sometimes more efficient to send the UNICODE value to the server using java.io.Reader.

IfmxPreparedStatement.setClob()

public void setClob(int parameterIndex, Clob x, int ifxType) 
   throws SQLException

IfmxPreparedStatement.setDate()

public void setDate(int i, Date x, int ifxType) throws 
   SQLException
public void setDate(int parameterIndex, Date x, Calendar Cal, 
   int ifxType) throws SQLException

IfmxPreparedStatement.setDouble()

public void setDouble(int i, double x, int ifxType) throws SQ
   LException

IfmxPreparedStatement.setFloat()

public void setFloat(int i, float x, int ifxType) throws 
   SQLException

IfmxPreparedStatement.setInt()

public void setInt(int i, int x, int ifxType) throws SQLException

IfmxPreparedStatement.setLong()

public void setLong(int i, long x, int ifxType) throws 
   SQLException

IfmxPreparedStatement.setNull()

public void setNull(int i, int sqlType, int ifxType) throws 
   SQLException

IfmxPreparedStatement.setShort()

public void setShort(int i, short x, int ifxType) throws 
SQLException

IfmxPreparedStatement.setString()

public void setString(int i, String x, int ifxType) throws 
   SQLException

IfmxPreparedStatement.setTime()

public void setTime(int i, Time x, int ifxType) throws 
   SQLException
public void setTime(int parameterIndex, Time time, Calendar Cal, 
   int ifxType) throws SQLException

IfmxPreparedStatement.setTimestamp()

public void setTimestamp(int i, Timestamp x, int ifxType) throws 
   SQLException
public void setTimestamp(int parameterIndex, Timestamp x, Calendar 
   Cal) throws SQLException
Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]