Using Coldfusion, you have run a query or procedure that returns a CLOB (Character Large Object) data type. You need to display this content in your web page or write it to a file. If you wrap your returned CLOB variable in a <cfoutput> you get an odd output like “[C@183eeb0″. If you try to <cfdump> the returned CLOB you get a giant array of objects. So how do you get your data?



<cfset x = CreateObject(“java”,”java.lang.String”).init(myClobOutput)>



First, I can’t take full credit for this. One of the Java guys I work with helped me figure this out. A CLOB is a “collection of character data.” In my case, I am using Oracle, and it has its own “construct” for the CLOB. In other words it’s an object of sorts. When you put this object in the <cfoutput> tags, you get the string’s identifier, just like you would if you output a Java object. When you dump it, again just as with a Java object, you see an array of nested objects, one object per character.

While doing a <cfset> is creating a Java String under the hood, Coldfusion gets to decide how to create the string. To get our CLOB to display, you need to create an instance of a Java String using the CreateObject() method, and then call the init() function. The init() constructor function can take a variety of arguments and knows how to translate each into a string. Passing in the CLOB as an argument, the init() function treats it as a char[] (character array) and “allocates a new String so that it represents the sequence of characters currently contained in the character array argument.” So, in other words, it knows what the array is and loops through it, putting each character back together as a giant string.

« »