Re: Create java.sql.Blob from byte[]
From: Chuck Simpson (chuckls_at_cox-internet.com)
Date: 10/31/04
- Next message: Chuck Simpson: "Re: Passing a list to a Java prepared statement"
- Previous message: Raphi: "Re: accessing mySQL database with Java"
- In reply to: Shay: "Create java.sql.Blob from byte[]"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 31 Oct 2004 13:20:03 -0600
On Thu, 14 Oct 2004 07:30:59 -0700, Shay wrote:
> Hello All
>
> How can I create Blob from byte array? I use jdk1.3.1_06.
> I think that I must create a new class implements the Blob interface but
> I'm not sure.
In Oracle 8i I use the following method:
Use JDBC or your SQL tool to insert a new record with a Blob.
INSERT into (col1, col2, col3, col4) table VALUES (pkval, value2,
empty_blob(), value4);
Use the empty_blob() (or empty_clob()) function to create the Blob
(or Clob) element in col3.
con.setAutocommit(false);
ResultSet rs = con.prepareStatment(
"SELECT * FROM table WHERE col1 = pkval FOR UPDATE")
.executeQuery();
Select the record you just inserted for update to lock it.
oracle.sqlBLOB blob = (oracle.sql.BLOB) rs.getBlob(3);
Get the Blob as an Oracle BLOB object.
blob.setBytes(yourByteArray);
Use the setBytes method of the BLOB object to set the Blob content
to your byte[].
PreparedStatement ps = con.prepareStatement(
"UPDATE table SET col3 = ?where col1 = pkval");
ps.setBlob(1, blob);
ps.executeUpdate();
con.commit();
con.setAutocommit(true);
Set the Blob column (col3) in the prepared statement using the BLOB
object you just put your byte[] into.
Execute the update statement and commit the result.
Note: You must do all this with autocommit off and commit after you
perform the update. If you do not commit or commit before the update
your byte arrsy will not get inserted.
This is described in Oracle's manuals (http://www.oracle.com/otn).
This is supposed to be easier in 9i and 10g but I have not had the
opportunity to try those versions yet.
Hope this helps.
Chuck
- Next message: Chuck Simpson: "Re: Passing a list to a Java prepared statement"
- Previous message: Raphi: "Re: accessing mySQL database with Java"
- In reply to: Shay: "Create java.sql.Blob from byte[]"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|