O'Reilly logo

Java Enterprise in a Nutshell, Third Edition by William Crawford, Jim Farley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

BLOBs and CLOBs

As users began to increase the volume of data stored in databases, vendors introduced support for large objects (LOBs). The two varieties of LOBs, binary large objects (BLOBs) and character large objects (CLOBs), store large amounts of binary or character data, respectively. The SQL99 specification formally defined these data types.

Support for LOB types across databases varies. Some don’t support them at all, and most have unique type names (BINARY, LONG RAW, and so forth). JDBC 1.0 makes programs retrieve BLOB and CLOB data using the getBinaryStream() or getAsciiStream() methods. (A third method, getUnicodeStream(), has been deprecated in favor of the getCharacterStream() method, which returns a Reader.)

As of JDBC 2.0, the ResultSet interface includes getBlob() and getClob() methods, which return Blob and Clob objects, respectively. The Blob and Clob objects themselves allow access to their data via streams (the getBinaryStream() method of Blob and the getCharacterStream() method of Clob) or via direct-read methods (the getBytes() method of Blob and the getSubString() method of Clob).

To retrieve the data from a CLOB, simply retrieve the Clob object and call the getCharacterStream() method:

String s;
Clob clob = blobResultSet.getBlob("CLOBFIELD");
BufferedReader clobData = new BufferedReader(clob.getCharacterStream());
while((s = clobData.readLine()) != null)
  System.out.println(s);

In addition, you can set Blob and Clob objects when you are working with a ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required