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.getMessa ge();
Blob msg= (Blob)messageForm.getMessa ge();
Any suggestions how to do ?
if i do below i get an error like
[javac] found : java.lang.String
[javac] required: java.sql.Blob
[javac] Blob msg= (Blob)messageForm.getMessa
Blob msg= (Blob)messageForm.getMessa
Any suggestions how to do ?
Use a PreparedStatement and setBinaryStream (s.getBytes())
Actually you can try ps.setBytes - that'd be easier
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 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Whoops
Blob b = new javax.sql.rowset.serial.Se rialBlob(s tring.getB ytes());
Blob b = new javax.sql.rowset.serial.Se
ASKER
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
/**
* @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
ASKER
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.Se rialBlob(m sg.getByte s());
message.setMessage(b);
Message message = (Message) convert(messageForm);
String msg= messageForm.getMessage();
Blob b = new javax.sql.rowset.serial.Se
message.setMessage(b);
ASKER
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;
}
/*
* @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
ASKER
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.m odel.Messa ge.setMess age - 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
The process did not complete. Details should follow.
Warning Cannot invoke com.sterlingcommerce.bpn.m
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
ASKER
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
>>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
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(strin g.getBytes ());
Blob blob = Hibernate.createBlob(strin
ASKER
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(t arget, o);
BeanUtils does not convert String to Blob.
Is there a solution for it.
public class org.apache.commons.beanuti ls.BeanUti ls {
Is this is the right place to ask this question?? or should it be in Apache
Message message = (Message) convert(messageForm); //converting my MessageForm obj to Message Obj (model class)
which eventually calls BeanUtils.copyProperties(t
BeanUtils does not convert String to Blob.
Is there a solution for it.
public class org.apache.commons.beanuti
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?
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?
ASKER
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?
What issue exactly??
I posted how to convert String->Blob, and visa versa.
What problem exactly are you having?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.