Tuesday, June 12, 2007

Convert java.sql.Blob to String

For some reason I have put in really lots of effort to find the reference to convert Blob to String.

Blob blob = rs.getBlob("Data");
byte[] bdata = blob.getBytes(1, (int) blob.length());
String text = new String(bdata);


Anonymous said...


Anonymous said...

I tried this but its failing as java.lang.OutofMemory for me in the third line where we convert bytes to string.Since i have a huge amount of data.Can u let mw know if there is an alternative for this?.

Seymour Cakes said...

Hi there, OutofMemory exception is probably caused by well out of memory.

Try export JAVA_OPTS="-XX:MaxPermSize=256M"

ThaRealBoss27 said...


Anonymous said...

How do I decrypt the Blob object?

Anonymous said...

Here is one more way of doing it

int offset = -1;
int chunkSize = 1024;
long blobLength = blob.length();
if(chunkSize > blobLength) {
chunkSize = (int)blobLength;
char buffer[] = new char[chunkSize];
StringBuilder stringBuffer = new StringBuilder();
Reader reader = new InputStreamReader(blob.getBinaryStream());

while((offset = reader.read(buffer)) != -1) {
value = stringBuffer.toString();

Anonymous said...


Anonymous said...

Thank you!!
worked well for me.

Anonymous said...

Thanks man

Anonymous said...

You will be have error if length of blob more than int length!