Saturday, April 19th, 2014

Convert String to Blob and Blob to String

1

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.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • RSS
  • Technorati
  • Twitter

Comments

One Response to “Convert String to Blob and Blob to String”
  1. Geethu says:

    I am getting an exception when I use this code .

    package com.cms.model;
    import org.hibernate.*;
    import java.sql.*;
    import java.sql.SQLException;

    public class blob_to_string {
    public static void main(String args[]){

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

    byte[] bdata;
    try {
    bdata = blob.getBytes(1, (int)blob.length());
    String data1 = new String(bdata);
    System.out.println(data1);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    System.out.println(“ERROR”);
    e.printStackTrace();
    }
    }

    }

    Exception in thread “main” java.lang.UnsupportedOperationException: Blob may not be manipulated from creating session
    at org.hibernate.lob.BlobImpl.excep(BlobImpl.java:104)
    at org.hibernate.lob.BlobImpl.getBytes(BlobImpl.java:50)
    at org.hibernate.lob.SerializableBlob.getBytes(SerializableBlob.java:69)
    at com.cms.model.blob_to_string.main(blob_to_string.java:15)

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

Secure Linux Web Hosting