JDBC Type 2 Driver Programmer's Reference for SQL/MX Release 3.2.1 (H06.26+, J06.15+)

Clob myClob = null;
// stream begins at position 1
long pos = 1;
// Example string containing the Unicode data
String s = TEST_UNICODE_DATA;
// Obtain the output stream to write Clob data
Writer cw = myClob.setCharacterStream(pos);
// write Clob data using Writer
char[] myClobData = s.toCharArray();
cw.write(myClobData);
Inserting CLOB Data by Using the PreparedStatement Interface
You can use the PreparedStatement interface to insert a CLOB column with data as follows:
ASCII Data (page 47)
“Unicode Data (page 47)
ASCII Data
You can insert a CLOB column with ASCII or Unicode data from a FileInputSteam. You must
use the PreparedStatement interface to insert the CLOB column.
FileInputStream inputAsciiStream = new FileInputStream(myClobTestFile);
int clobLen = inputAsciiStream.available();
PreparedStatement ps = conn.prepareStatement("insert
into myTable (myClobColumn) values (?)");
ps.setAsciiStream(1, inputAsciiStream, clobLen);
ps.executeUpdate();
The JDBC/MX driver reads the data from FileInputSteam and writes the data to the LOB table.
The JDBC/MX driver substitutes the next-available data locator for the parameter of the CLOB
column in the table.
Unicode Data
You can insert a CLOB column with Unicode data from a FileReader. You must use the
PreparedStatement interface to insert the CLOB column.
FileReader inputReader = new FileReader(myClobTestFile);
PreparedStatement ps = conn.prepareStatement("insert
into myTable (myClobColumn) values (?)");
ps.setCharacterStream(1, inputReader, (int)myClobTestFile.length());
ps.executeUpdate();
The JDBC/MX driver reads the data from FileReader and writes the data to the LOB table. The
JDBC/MX driver substitutes the next available-data locator for the parameter of the CLOB column
in the table.
Inserting a Clob Object by Using the setClob Method
Your JDBC application cannot directly instantiate a Clob object. To perform an equivalent operation:
1. Obtain a Clob object by using the getClob method of the ResultSet interface.
2. Insert the Clob object into another row by using the setClob method of the
PreparedStatement interface.
In this situation, the JDBC/MX driver generates a new data locator and, when the
PreparedStatement is executed, copies the contents of the source Clob into the new Clob
object.
Reading CLOB Data
“Reading ASCII Data from a CLOB Column (page 48)
“Reading Unicode Data from a CLOB Column (page 48)
Reading CLOB Data 47