Convert between String and Blob(Clob)


Convert String to Blob

String data = “hello world”;
java.sql.Blob blob = org.hibernate.Hibernate.createBlob(data.getBytes());

Convert Blob to String

byte[] bdata = blob.getBytes(1, (int)blob.length());
String data1 = new String(bdata);

if the length of blob exceeds the maxvalue for the int, then u can use CLOB format.To store large data we can use CLOB format and to store an image, use a BLOB data types.

Convert Clob to String:

String s = clob.getSubString(1, (int) clob.length());

Convert String to Clob:

Clob clo = new javax.sql.rowset.serial.SerialClob.SerialClob(str.toCharArray());
clob.setString(1, str);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s