?
Solved

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

Posted on 2007-10-11
23
Medium Priority
?
12,763 Views
Last Modified: 2013-12-22
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 ?
0
Comment
Question by:cutie_smily
  • 11
  • 8
  • 4
23 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20060825
Use a PreparedStatement and setBinaryStream (s.getBytes())

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20060841
Actually you can try ps.setBytes - that'd be easier
0
 

Author Comment

by:cutie_smily
ID: 20060881
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 20060914
If you're using Hibernate, why would you, and do you, need a Blob?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 750 total points
ID: 20060968
In case you really do need one, try

Bload b = new javax.sql.rowset.serial.SerialBlob(string.getBytes());
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20060972
Whoops

Blob b = new javax.sql.rowset.serial.SerialBlob(string.getBytes());
0
 

Author Comment

by:cutie_smily
ID: 20060974
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20061031
Sounds like you have an inappropriate mapping in Hibernate. You need to map the column to a standard Java object
0
 

Author Comment

by:cutie_smily
ID: 20061056
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);
0
 

Author Comment

by:cutie_smily
ID: 20061081
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;
      }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20061096
Yes, String would be a good fit
0
 

Author Comment

by:cutie_smily
ID: 20061129
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20061156
You need to change the mapping throughout
0
 
LVL 92

Expert Comment

by:objects
ID: 20062052
0
 

Author Comment

by:cutie_smily
ID: 20064183
I need to read String and convert to Blob not the other way round. Thanks
Mapping looks alright. It complaints about the conversion type
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20064198
>>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
0
 
LVL 92

Expert Comment

by:objects
ID: 20064317
to go the other way use:

Blob blob = Hibernate.createBlob(string.getBytes());
0
 

Author Comment

by:cutie_smily
ID: 20066951
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20067013
>>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?
0
 

Author Comment

by:cutie_smily
ID: 20067147
By default, in Appfuse, the forms and generated by the XDoclet task @struts.form, and the data transfert used the jakarta commons beanutils.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20067183
Well i don't know how really. BeanUtils.copyProperties expects beans
0
 
LVL 92

Expert Comment

by:objects
ID: 20069371
> 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?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 750 total points
ID: 20069382
> Blob msg= (Blob)messageForm.getMessage();

Blob msg= Hibernate.createBlob(messageForm.getMessage().getBytes());

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question