Subject Retrieving bytea field to a file from postgresql with java
Author Vertus Hector
Hey
i, m Padasa , i have a bytea field in postgresql database table and I am trying to retrive it to a file such as a myImage.JPG file..... how would I do that ? The image was saving well.

Please help to save the byte to a file..



There is my table.
create table picture(code varchar(10) not null, photo bytea );

There is my Java code.

pstmt = conn.prepareStatement ("select photo from picture where code = ?");
pstmt .setObject(1,this.code);
rset = pstmt.executeQuery();

if (rset.next()) {
rows++;
//Get the locator from de database
Blob blobLocator = rset.getBlob(1);

//Get a binary stream using the locator
InputStream is = blobLocator.getBinaryStream();

File file = new File("myImage.JPG");
FileOutputStream fos = new FileOutputStream(file);

//Read and write the data 32 bytes at a time.
int length = 0;
int bufferSize = 102400; //32;
byte[] buffer = new byte[bufferSize];

while ((length = is.read(buffer)) != -1) {
fos.write(buffer, 0, length);
}

is.close();
fos.close();

}

pstmt.close();
pstmt = null;
conn.commit();






---------------------------------
Yahoo! Photos – Showcase holiday pictures in hardcover
Photo Books. You design it and we’ll bind it!

[Non-text portions of this message have been removed]