Home | Previous Page | Next Page   Working With Informix Types > Smart Large Object Data Types >

Smart Large Object Examples

The examples on the following pages illustrate some of the tasks discussed in this section.

Creating a Smart Large Object

This example illustrates the steps shown in Steps for Creating Smart Large Objects.

file = new File("data.dat");
FileInputStream fin = new FileInputStream(file);

byte[] buffer = new byte[200];;

IfxLobDescriptor loDesc = new IfxLobDescriptor(myConn);
IfxLocator loPtr = new IfxLocator();
IfxSmartBlob smb = new IfxSmartBlob(myConn);

// Now create the large object in server. Read the data from the 
   file 
// data.dat and write to the large object.
int loFd = smb.IfxLoCreate(loDesc, smb.LO_RDWR, loPtr);
System.out.println("A smart-blob is created ");
int n = fin.read(buffer); 
if (n > 0)
n = smb.IfxLoWrite(loFd, buffer);
System.out.println("Wrote: " + n +" bytes into it");

// Close the large object and release the locator.
smb.IfxLoClose(loFd);
System.out.println("Smart-blob is closed " );
smb.IfxLoRelease(loPtr);
System.out.println("Smart Blob Locator is released ");

The contents of the file data.dat are written to the smart large object.

Inserting Data into a Smart Large Object

The following code inserts data into a smart large object:

String s = "insert into large_tab (col1, col2) values (?,?)";
pstmt = myConn.prepareStatement(s);

file = new File("data.dat");
FileInputStream fin = new FileInputStream(file);

byte[] buffer = new byte[200];;

IfxLobDescriptor loDesc = new IfxLobDescriptor(myConn);
IfxLocator loPtr = new IfxLocator();
IfxSmartBlob smb = new IfxSmartBlob(myConn);

// Create a smart large object in server
int loFd = smb.IfxLoCreate(loDesc, smb.LO_RDWR, loPtr);
System.out.println("A smart-blob has been created ");
int n = fin.read(buffer); 
if (n > 0)
n = smb.IfxLoWrite(loFd, buffer);
smb.IfxLoClose(loFd);

System.out.println("Wrote: " + n +" bytes into it");
System.out.println("Smart-blob is closed " );

Blob blb = new IfxBblob(loPtr);
pstmt.setInt(1, 2);  // set the Integer column
pstmt.setBlob(2, blb); // set the blob column
pstmt.executeUpdate();
System.out.println("Binding of smart large object to table is 
   done");

pstmt.close();
smb.IfxLoRelease(loPtr);
System.out.println("Smart Blob Locator is released ");

The contents of the file data.dat are written to the BLOB column of the large_tab table.

Retrieving Data from a Smart Large Object

The example in this section illustrates the steps in Steps for Accessing Smart Large Objects.

The following code example shows how to access the smart large object data using Informix extension classes:

byte[] buffer  = new byte[200];
System.out.println("Reading data now ...");
try
   {
   int row = 0;
   Statement stmt = myConn.createStatement();
   ResultSet rs =  stmt.executeQuery("Select * from demo_14");
   while( rs.next() )
      {
      row++;
      String str = rs.getString(1);
      InputStream value = rs.getAsciiStream(2);
      IfxBblob b = (IfxBblob) rs.getBlob(2);
      IfxLocator loPtr = b.getLocator();
      IfxSmartBlob smb = new IfxSmartBlob(myConn);
      int loFd = smb.IfxLoOpen(loPtr, smb.LO_RDONLY);

      System.out.println("The Smart Blob is Opened for reading ..");
      int number = smb.IfxLoRead(loFd, buffer, buffer.length);
      System.out.println("Read total " + number  + " bytes");
      smb.IfxLoClose(loFd);
      System.out.println("Closed the Smart Blob ..");
      smb.IfxLoRelease(loPtr);
      System.out.println("Locator is released ..");
      }
   rs.close();
   }
catch(SQLException e)
   {
   System.out.println("Select Failed ...\n" +e.getMessage());
         }

First, the ResultSet.getBlob() method gets an object of type BLOB. The casting is required to convert the returned object to an object of type IfxBblob. Next, the IfxBblob.getLocator() method gets an IfxLocator object from the IfxBblob object. After the IfxLocator object is available, you can instantiate an IfxSmartBlob object and use the IfxLoOpen() and IfxLoRead() methods to read the smart large object data. Fetching CLOB data is similar, but it uses the methods ResultSet.getClob(), IfxCblob.getLocator(), and so on.

If you use getBlob() or getClob() to fetch data from a column of type BLOB, you do not need to use the Informix extensions to retrieve the actual BLOB content as outlined in the preceding sample code. You can simply use Java.Blob.getBinaryStream() or Java.Clob.getAsciiStream() to retrieve the content. IBM Informix JDBC Driver implicitly gets the content from the database server for you, using basically the same steps as the sample code. This approach is simpler than the approach of the preceding example but does not provide as many options for reading the contents of the BLOB column.

Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]