Re: create BLOB question - Mailing list pgsql-jdbc
From | Jeremiah Jahn |
---|---|
Subject | Re: create BLOB question |
Date | |
Msg-id | 1044916012.1847.568.camel@bluejay.goodinassociates.com Whole thread Raw |
In response to | Re: create BLOB question ("David Wall" <d.wall@computer.org>) |
List | pgsql-jdbc |
So does setBytes() insert or overwrite the data starting at a position? On Mon, 2003-02-10 at 15:28, David Wall wrote: > > does some9one out there know how to create a new oid from scratch. > > setBytes on 7.2 worked fine for me, but with the change to 7.3 I can't > > seem to get a new oid number. I want to avoid any postgres specific > > stuff. Correct me if I'm wrong, but It seems to me that the setBytes > > goes with the bytea stuff and the BLOB stuff goes with the oid stuff. I > > just can't seem to figure out how to insert a new oid/BLOB. > > > > any help would be great, > > You will need to use the setBlob() call on prepared statement. The trick is > that you'll need a wrapper that can take your byte array and pretend it's a > java.sql.Blob interface so that the driver can use it. In my code, Oracle > does it different, with an empty_blob() being first created, and then > updated with the blob data (why, I'll never know!). > > Below is a utility class that we use so that our mainline call, which has a > byte array, can just do the following: > YoByteBlob myBlob = new YoByteBlob( myByteArray ); > if ( ! isOracle ) > stmt.setBlob(1,myBlob); > > > // Copyright (c) 2002 Yozons, Inc. All rights reserved. > // This file is proprietary. > // > package com.yozons.jdbc; > > import java.sql.SQLException; > > /** > * Screwy wrapper class so that we can insert a Blob into the database from > a byte array. > * Includes more screwy stuff for Oracle specific updating of a blob (the > only way to insert a new blob). > * > * @author David Wall > */ > public class YoByteBlob > implements java.sql.Blob > { > byte[] bytes = null; > > /** > * Creates a YoByteBlob using the specified byte array. > */ > public YoByteBlob(byte[] b) > { > bytes = b; > } > > // My own constructor for taking a Blob of input and returning as an > array > public YoByteBlob(java.sql.Blob b) > { > java.io.InputStream is = null; > try > { > is = b.getBinaryStream(); > bytes = new byte[(int)b.length()]; > is.read(bytes); > } > catch( java.sql.SQLException e ) > { > bytes = null; > } > catch( java.io.IOException e ) > { > bytes = null; > } > finally > { > try > { > if ( is != null ) > is.close(); > } > catch( Exception e ) {} > } > } > > public long length() > throws java.sql.SQLException > { > return bytes.length; > } > > // My own API call for simplicity > public byte[] getBytes() > { > return bytes; > } > > public byte[] getBytes(long pos, int length) > throws java.sql.SQLException > { > if ( pos == 0 && length == bytes.length ) > return bytes; > > try > { > byte[] newbytes = new byte[length]; > System.arraycopy(bytes, (int)pos, newbytes, 0, length); > return newbytes; > } > catch( Exception e ) > { > throw new java.sql.SQLException("Could not get subset of > array"); > } > } > > public java.io.InputStream getBinaryStream() > throws java.sql.SQLException > { > return new java.io.ByteArrayInputStream(bytes); > } > > public long position(byte[] pattern, long start) > throws java.sql.SQLException > { > throw new java.sql.SQLException("Unsupported position() for blob"); > } > > public long position(java.sql.Blob pattern, long start) > throws java.sql.SQLException > { > throw new java.sql.SQLException("Unsupported position() for blob"); > } > > > /** > * Routine used to put the "real" object into an Oracle database, which > requires > * creating an empty blob, then retrieving it again and updating it from > there. > */ > public void updateOracleBlob(java.sql.Blob b) > throws java.sql.SQLException > { > java.io.OutputStream outstream = null; > > try > { > if ( b == null ) > throw new SQLException("YoByteBlob.updateOracleBlob() blob > was null"); > if ( ! (b instanceof oracle.sql.BLOB) ) > throw new SQLException("YoByteBlob.updateOracleBlob() blob > not an oracle.sql.BLOB object; is: " + > b.getClass().getName() ); > if ( bytes == null ) > throw new SQLException("YoByteBlob.updateOracleBlob() no > blob bytes to write"); > > oracle.sql.BLOB blob = (oracle.sql.BLOB)b; > outstream = blob.getBinaryOutputStream(); > > int bufSize = blob.getBufferSize(); > int pos = 0; > int remaining = bytes.length; > while ( remaining > 0 ) > { > int numOut = Math.min(bufSize,remaining); > outstream.write(bytes, pos, numOut); > pos += numOut; > remaining -= numOut; > } > } > catch( java.io.IOException e ) > { > throw new java.sql.SQLException("YoByteBlob.updateOracleBlob() > I/O failure: " + e.getMessage()); > } > finally > { > try > { > if ( outstream != null ) > outstream.close(); > } > catch( java.io.IOException e ) > { > throw new > java.sql.SQLException("YoByteBlob.updateOracleBlob() close I/O failure: " + > e.getMessage()); > } > } > } > } > > > ---------------------------(end of broadcast)--------------------------- > TIP 5: Have you checked our extensive FAQ? > > http://www.postgresql.org/users-lounge/docs/faq.html -- Jeremiah Jahn <jeremiah@cs.earlham.edu>
pgsql-jdbc by date: