Re: Blob manipulation
Date: Thu, 25 Oct 2001 13:24:06 +0400
Message-ID: <9r8lnt$116k$1_at_serv2.vsi.ru>
[Quoted] [Quoted] "Aviran Levy" <avlevy2k_at_hotmail.com> сообщил/сообщила в новостях следующее:
[Quoted] news:ZfAB7.133$0N4.17635_at_news.shore.net...
> Hi,
> From a java application I'm trying to do:
> ...
> Blob b = rs.getBlob("ZIP_FILE");
> b.setBytes(1,byteArray);
> ...
> but apparently I get a runtime exception since my oracle jdbc driver
doesn't
> have implementation for the setBytes method. I probably need a newer
oracle
> jdbc driver. Is anyone know where can I find one?
> Is anyone having that problem too?
> My goal is to store a zip file on the Oracle server and manipulate it from
> the java application (avoiding calls for store procedures). If anyone
knows
> a better way to do that then please... I'll appreciate any help about it.
> Note: to use setBytes you have to use JDK1.4
> Thanks,
> -- Avi
- Begin by using SQL statements to create the BLOB entry in the table. Use the empty_blob syntax to create the BLOB locator.
stmt.execute ("INSERT INTO my_blob_table VALUES ('row1', empty_blob())");
2. Get the BLOB locator from the table.
BLOB blob;
cmd = "SELECT * FROM my_blob_table WHERE X='row1'";
ResultSet rest = stmt.executeQuery(cmd);
BLOB blob = ((OracleResultSet)rset).getBLOB(2);
3. Declare a file handler for the john.gif file, then print the length of [Quoted] the file. This value will be used later
to ensure that the entire file is read into the BLOB. Next, create a [Quoted] FileInputStream object to read the
contents of the GIF file, and an OutputStream object to retrieve the [Quoted] BLOB as a stream.
File binaryFile = new File("YOUR_ZIP_FILE"); System.out.println("john.gif length = " + binaryFile.length()); FileInputStream instream = new FileInputStream(binaryFile); OutputStream outstream = blob.getBinaryOutputStream();
4. Call getBufferSize() to retrieve the ideal buffer size (according to calculations by the JDBC driver) to use in
writing to the BLOB, then create the buffer byte array.
int size = blob.getBufferSize(); byte[] buffer = new byte[size]; int length = -1;
5. Use the read() method to read the GIF file to the byte array buffer, then [Quoted] [Quoted] use the write() method to write it to
the BLOB. When you finish, close the input and output streams.
while ((length = instream.read(buffer)) != -1)
outstream.write(buffer, 0, length);
instream.close();
outstream.close();
This fragment is from "Oracle8i JDBC Developer's Guide and Reference" charpter 7 "Working with LOBs and BFILEs"
Sergey M.
P.S. Knowledge is of two kinds. We know a subject ourselves, or we know where we can find information upon it. --Samuel Johnson Received on Thu Oct 25 2001 - 11:24:06 CEST