[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

JAVA accessing BLOBS and CLOBS



Hi All.
A bit out of context here, but this is one of the most knowledgeable and helpful forum ...
I have an Oracle table , each row has one CLOB and one BLOB.
I have been able to stream documents into and out of the BLOB's incl. MS .doc Also I have been able to get the content from BLOB and write it to a target directory.
However I am having odd problems with CLOB, a simple text file. My servlet attempts to read the CLOB and does create the output file in the target directory, but does not copy the content over. It also returns 0 for clobLength - here is where the troubles start !?! Here is the stub-code :
...
ResultSet clobRS = thisStmt.executeQuery (
"SELECT ATextMsg " +
"FROM ... " +
"WHERE ... " );
clobRS.next();
//
CLOB thisCLOB = ((OracleResultSet) clobRS).getCLOB("ATextMsg");
int chunkSize = thisClob.getChunkSize();
System.out.println("chunkSize is " + chunkSize); // default us 8132...
char [] textBuffer = new char[chunkSize];
String targetFile = targetDir + "retrieved_" + fileName;
File thisFile = new File(targetFile); // it does create an empty target file...
// read content as ASCII (text) content...
FileOutputStream thisFileOutputStream = new FileOutputStream(thisFile);
OutputStreamWriter thisWriter = new OutputStreamWriter(thisFileOutputStream);
BufferedWriter thisBufferedWriter = new BufferedWriter(thisWriter);
//
long clobLength = thisClob.length(); // things go very wrong here, returns length of 0, why ?!?
System.out.println("clobLength is " + clobLength);
//
// flush to targetFile, but nothing gets copied over due to clobLength=0 ...
for (position=1; position<=clobLength; position+=chunkSize)
{
int charsRead = thisClob.getChars(position, chunkSize, textBuffer);
thisBufferedWriter.write(testBuffer);
}
...
Since the clobLength is 0, obviously it will not copy from the database CLOB to the file.
Manually change the clobLength = 8132, recompile and run, then it copies some junk to the targetFile, certainly not the textual data in the CLOB itself.
The BLOB is fine...
Can someone comment on this please and if possible, give me a hint on where I might have gone wrong with a simple text file ???
 
 
 
 

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]