Re: Create java.sql.Blob from byte[]

From: Chuck Simpson (chuckls_at_cox-internet.com)
Date: 10/31/04


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



Relevant Pages

  • Re: Create java.sql.Blob from byte[]
    ... > Use JDBC or your SQL tool to insert a new record with a Blob. ... > Get the Blob as an Oracle BLOB object. ... > Set the Blob column in the prepared statement using the BLOB ... > Execute the update statement and commit the result. ...
    (comp.lang.java.databases)
  • Re: [PATCH] Simplified GIT usage guide
    ... This is incorrect, a 'blob' contains only the contents of the blob, the ... file mode is stored in the 'tree' object. ... A commit object contains the attribute of that commit (the author and the ... Using git clone --mirror would be much efficient, ...
    (Linux-Kernel)
  • Re: Goof in the design of setBinaryStream
    ... > create a Blob object in order to feed to an update or insert. ... java.sql.Blob and java.sql.Clob that is not tied to an active connection. ... JDBC drivers will accept a Blob object unless it was their native version. ... FirstSQL/J Object/Relational DBMS ...
    (comp.lang.java.databases)
  • Re: Unable to read BLOB objects through ADO.NET
    ... I am using following code for retreival of Blob Data. ... It seems that code is working properly and retreiving the BLOB object also ...
    (microsoft.public.dotnet.framework.adonet)