Click to See Complete Forum and Search --> : How to insert Blob value to Database


kori
May 10th, 2000, 02:24 PM
Hi,
I'm facing problem while inserting Blob type data into oracle8 database through java application.
i'm expecting your solution as early as possible.Thanks in advance.

kib63613
May 10th, 2000, 05:27 PM
First, you may create the table which may have the field which type is Blob. For the construction
procedure, you may reference the manual of Oracle.
In the application side, you need to take notice of if the JDBC driver support JDBC2.0. If so, you may
take into account the following code snippet. It is for referenced.

String sql_str = "insert into person_info (photo) values (?) ";
PrepareStatement pre_stmt = conn.prepareStatement(sql_str);
pre_stmt.setBlob(1, blob_data);
pre_stmt.executeUpdate();
where conn is the instance of Connection



For more information, you may reference java.sql.Connection, java.sql.PrepareStatement, and
java.sql.Blob.
good luck,
Alfred Wu

kori
May 10th, 2000, 06:12 PM
Thanks. but my query is How do I convert(through java) to compatable with a blob value before inserting into Blob column.
If you say pstmt.setBlob(1,blob_data); if I give like this it will give error.In which format should I covert before setBlob.I want that format.If i set that format it should not show error about BLOB could you give the solution.
Thanks in advance.

kib63613
May 10th, 2000, 10:55 PM
You may see the parameter needing the Blob type. It means that your data need to implement the
interface Blob. In that interface, it has several methods getBinaryStream(), getBytes(), length(),
position(). You may wrap your class with these methods. You may look like this :

public class BlobData implements Blob {
byte[] data=null;

public BlobData(byte[] blob_data) {
data = blob_data;
}

public InputSteam getBinaryStream(){
// implment your code
}

public byte[] getBytes(long pos, int length) {
// implment your code
}

public length() {
// implment your code
}

public long position(Blob pattern, long start) {
// implment your code
}

public long position(byte[] pattern, long start) {
// implment your code
}
}



When you get the blob data by the method getBlob(), you may use the method getBytes(),
or getBinaryStream() to fetch the data stored in the BlobData, in this case, for other further use.
I never try blob, But I think that this is the right way. You may try it. The key point is that you
have implement the Blob interface for your blob data.
good luck,
Alfred Wu