|
Oct
01
|
Read the bytes from the blob using a ByteArrayOutputStream and then create a String from the resulting byte array.
ByteArrayOutputStream baos = new ByteAttayOutputStream();
byte[] buf = new byte[1024];
InputStream in = blob.getBinaryStream();
int n = 0;
while ((n=in.read(buf))>=0)
{
baos.write(buf, 0, n);
}
in.close();
byte[] bytes = baos.toByteArray();
String blobString = new String(bytes);



July 22nd, 2009 at 4:49 am
You can achieve the same results by using this code :
java.sql.Blob ablob = rs.getBlob(1);
String str = new String(ablob.getBytes(1l, (int) ablob.length()));
December 29th, 2010 at 7:27 pm
@Simisani Takobana: this technique is for small data set in blob field.. if the data size exceeds then there are chances to get OutOfMemoryExceptions….
Thanks
December 29th, 2010 at 7:33 pm
StringBuffer strOut = new StringBuffer();
String aux;
// We access to stream, as this way we don't have to use the CLOB.length() which is slower...
// assuming blob = resultSet.getBlob("somefield");
BufferedReader br = new BufferedReader(new InputStreamReader(blob.getBinaryStream()));
while ((aux=br.readLine())!=null) {
strOut.append(aux);
}
return strOut.toString();