Smart large objects within an opaque type
A smart large object can be a data member within an opaque type, although you are most likely to create a large object on the database server, outside of the opaque type context, using the HCL Informix® extension classes.
For more information about smart large objects, see Smart large object data types.
A large object
is stored as an IfxLocator object within the opaque type; in
the C struct that defines the opaque type internally, the large object
is referenced through a locator pointer of type MI_LO_HANDLE. The
object is created using the methods provided in the IfxSmartBlob class,
and the large object handle obtained from these methods becomes the
data member within the opaque type. Both BLOB and CLOB objects use
the same large object handle, as shown in the following example:
import java.sql.*;
import com.informix.jdbc.*;
/*
* C struct of large_bin_udt:
*
* typedef struct LARGE_BIN_TYPE
* {
* MI_LO_HANDLE lb_handle; // handle to large object (72 bytes)
* }
* large_bin_udt;
*
*/
public class largebinUDT implements SQLData
{
private String sql_type = "large_bin_udt";
public Clob lb_handle;
public largebinUDT() { }
public largebinUDT(Clob clob)
{
lb_handle = clob;
}
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;
lb_handle = stream.readClob();
}
// writes a sequence of values from a Java object to a stream
public void writeSQL(SQLOutput stream) throws SQLException
{
stream.writeClob(lb_handle);
}
}
In a JDBC application, you create the MI_LO_HANDLE object
using the methods provided by the IfxSmartBlob class:
String s = "insert into largebin_tab (int_col, largebin_col, lvc_col) " +
"values (?,?,?)";
System.out.println(s);
pstmt = conn.prepareStatement(s);
...
// create a large object using IfxSmartBlob's methods
String filename = "lbin_in1.dat";
File file = new File(filename);
int fileLength = (int) file.length();
FileInputStream fin = new FileInputStream(file);
IfxLobDescriptor loDesc = new IfxLobDescriptor(conn);
System.out.println("create large object descriptor...ok");
IfxLocator loPtr = new IfxLocator();
IfxSmartBlob smb = new IfxSmartBlob((IfxConnection)conn);
int loFd = smb.IfxLoCreate(loDesc, 8, loPtr);
System.out.println("create large object...ok");
int n = smb.IfxLoWrite(loFd, fin, fileLength);
System.out.println("write file content into large object...ok");
pstmt.setInt(1, 1);
System.out.println("setInt...ok");
// initialize largebin object using the large object created
// above, before doing setObject for the large_bin_udt column.
largebinUDT largebinObj = new largebinUDT();
largebinObj.lb_handle = new IfxCblob(loPtr);
pstmt.setObject(2, largebinObj);
System.out.println("setObject(largebinUDT)...ok");
pstmt.setString(3, "Sydney");
System.out.println("setString...ok");
pstmt.executeUpdate();
System.out.println("execute...ok");
// close/release large object
smb.IfxLoClose(loFd);
System.out.println("close large object...ok");
smb.IfxLoRelease(loPtr);
System.out.println("release large object...ok");
See Smart large object data types for details.