Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

How to make a blob and save it to a mysql database

I have an object and I don't know how I can cast it to a Blob, so thyat I can save it to a mysql database.
Can Anyone help me? :WAVE:
[147 byte] By [JochenKlein] at [2007-11-11 8:48:52]
# 1 Re: How to make a blob and save it to a mysql database
have a look at the javadoc for blob.
it has a method called "setBytes(long pos, byte[] bytes)".
instantiate blob, set the data by the method and pass it to a prepared insert statement.
that's it. by the way: there are a lot of examples reachable through http://www.google.com/search?q=jdbc+blob
graviton at 2007-11-11 22:34:19 >
# 2 Re: How to make a blob and save it to a mysql database
Thanks, but how to instantiate Blob?
when I try
Blob bl = new Blob();
Eclipse underlines Blob() and says: "Cannot instantiate the type Blob, since it is not a concrete class."

What can I do? :WAVE:
JochenKlein at 2007-11-11 22:35:24 >
# 3 Re: How to make a blob and save it to a mysql database
As denoted in the javadoc, java.sql.Blob is an interface and thus not instantiable. you will have to look out for the concrete implementation of your driver. eg if you use mysql, there should be somewhere in the package a CLASS called blob. this one you can instantiate.
graviton at 2007-11-11 22:36:17 >
# 4 Re: How to make a blob and save it to a mysql database
You don't have to use any blob class/interface to store a pile of bytes in an SQL database. You open a byte stream using the i/o stream methods defined for the Column object, write the bytes to that stream and close it, that works for all SQL databases.
sjalle at 2007-11-11 22:37:22 >