Link to home
Start Free TrialLog in
Avatar of cutie_smily
cutie_smily

asked on

Urgent: How to convert a String to Blob in my java code

Hi I am using oracle DB. I want to convert String to java.sql.Blob

if i do below i get an error like

    [javac] found   : java.lang.String
    [javac] required: java.sql.Blob
    [javac]             Blob msg= (Blob)messageForm.getMessage();


Blob msg= (Blob)messageForm.getMessage();

Any suggestions how to do ?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Use a PreparedStatement and setBinaryStream (s.getBytes())

Actually you can try ps.setBytes - that'd be easier
Avatar of cutie_smily
cutie_smily

ASKER

I am using struts, hibernate...and xdoclets..(appfuse) i don't have any prepared statements.

If you could let me know how to convert String to Blob might help at this situation
If you're using Hibernate, why would you, and do you, need a Blob?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Whoops

Blob b = new javax.sql.rowset.serial.SerialBlob(string.getBytes());
xdoclet reads below javadoc

/**
       * @hibernate.property column="DETAILMESSAGE"
       * @return
       */
      public Blob getMessage()
      {
            return message;
      }

      public void setMessage(Blob message)
      {
            this.message = message;
      }
In my Form object is generated as

 public String getMessage()
    {
        return this.message;
    }
   /**
    */

    public void setMessage( String message )
    {
        this.message = message;
    }

While saving it complaints about type mismatch
Sounds like you have an inappropriate mapping in Hibernate. You need to map the column to a standard Java object
As per your advise i am changing as below

                                          Message message = (Message) convert(messageForm);
            String msg=  messageForm.getMessage();
            Blob b = new javax.sql.rowset.serial.SerialBlob(msg.getBytes());
            message.setMessage(b);
If i make below changes i mean if change Blob to String it works fine

/*
       * @hibernate.property column="DETAILMESSAGE"
       * @return
       */
      public String getMessage()
      {
            return message;
      }

      public void setMessage(String message)
      {
            this.message = message;
      }
Yes, String would be a good fit
But the reuirement is Blob ...here ia m getting below error after the changes also

 The process did not complete. Details should follow.
Warning Cannot invoke com.sterlingcommerce.bpn.model.Message.setMessage - argument type mismatch

I will be working on this issue after 4 hours from now. Thank you very much if you have any suggestion post it i will look in to it later in the day
You need to change the mapping throughout
I need to read String and convert to Blob not the other way round. Thanks
Mapping looks alright. It complaints about the conversion type
>>Mapping looks alright. It complaints about the conversion type

Well that seems to be a contradiction in terms. The mapping should be *to* a Blob type in the database layer. In your application layer, it should be a Java object, such as String or byte[], depending on what you're storing
to go the other way use:

Blob blob = Hibernate.createBlob(string.getBytes());
Hi! All, i see the issue is over below statement
      Message message = (Message) convert(messageForm);   //converting my MessageForm obj to Message Obj (model class)

which eventually calls   BeanUtils.copyProperties(target, o);
BeanUtils does not convert String to Blob.

Is there a solution for it.

public class org.apache.commons.beanutils.BeanUtils {

Is this is the right place to ask this question?? or should it be in Apache
>>BeanUtils does not convert String to Blob.

No, since neither is a bean, which is what BeanUtils.copyProperties operates on. I wonder why BeanUtils is being used at all. I take it that the objective is to persist the Message in the db?
By default, in Appfuse, the forms and generated by the XDoclet task @struts.form, and the data transfert used the jakarta commons beanutils.
Well i don't know how really. BeanUtils.copyProperties expects beans
> Hi! All, i see the issue is over below statement

What issue exactly??
I posted how to convert String->Blob, and visa versa.
What problem exactly are you having?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial