This is an example of how to use CLOBs in your applications.
/////////////////////////////////////////
// UpdateClobs is an example application
// that shows some of the APIs providing
// support for changing Clob objects
// and reflecting those changes to the
// database.
//
// This program must be run after
// the PutGetClobs program has completed.
/////////////////////////////////////////
import java.sql.*;
public class UseClobs {
public static void main(String[] args)
throws SQLException
{
// Register the native JDBC driver.
try {
Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
} catch (Exception e) {
System.exit(1); // Setup error.
}
Connection c = DriverManager.getConnection("jdbc:db2:*local");
Statement s = c.createStatement();
ResultSet rs = s.executeQuery("SELECT * FROM CUJOSQL.CLOBTABLE");
rs.next();
Clob clob1 = rs.getClob(1);
rs.next();
Clob clob2 = rs.getClob(1);
// Determine the length of a LOB.
long end = clob1.length();
System.out.println("Clob1 length is " + clob1.length());
// When working with LOBs, all indexing that is related to them
// is 1-based, and not 0-based like strings and arrays.
long startingPoint = 450;
long endingPoint = 50;
// Obtain part of the CLOB as a byte array.
String outString = clob1.getSubString(startingPoint, (int)endingPoint);
System.out.println("Clob substring is " + outString);
// Find where a sub-CLOB or string is first found within a
// CLOB. The setup for this program placed two identical copies of
// a repeating CLOB into the database. Thus, the start position of the
// string extracted from clob1 can be found in the starting
// position in clob2 if the search begins close to the position where
// the string starts.
long startInClob2 = clob2.position(outString, 440);
System.out.println("pattern found starting at position " + startInClob2);
c.close(); // Connection close also closes stmt and rs.
}
}