JDBC for Oracle - Herong's Tutorial Examples - v3.13, by Herong Yang
Retrieving BLOB Values with getBinaryStream() Method
This section describes how to retrieve BLOB values with the ResultSet.getBinaryStream() method.
BLOB values can also be retrieved with the getBinaryStream() method on the ResultSet object, which will return an OutputStream object. Then you can read the BLOB values from the OutputStream object with the read() method. The sample program below shows you how to create OutputStream objects with the getBinaryStream() method. A utility method is included to read all bytes from an OutputStream object and save them in a file.
/* OracleBlobGetBinaryStream.java * Copyright (c) HerongYang.com. All Rights Reserved. */ import java.io.*; import java.sql.*; public class OracleBlobGetBinaryStream { public static void main(String [] args) { Connection con = null; try { oracle.jdbc.pool.OracleDataSource ds = new oracle.jdbc.pool.OracleDataSource(); ds.setDriverType("thin"); ds.setServerName("localhost"); ds.setPortNumber(1521); ds.setDatabaseName("XE"); ds.setUser("Herong"); ds.setPassword("TopSecret"); con = ds.getConnection(); // Retrieving CLOB value with getBinaryStream() Statement sta = con.createStatement(); ResultSet res = sta.executeQuery("SELECT * FROM Image"); int i = 0; while (res.next() && i<3) { i++; System.out.println("Record ID: "+res.getInt("ID")); System.out.println(" Subject = "+res.getString("Subject")); InputStream bodyOut = res.getBinaryStream("Body"); String fileOut = "BlobOut_"+res.getInt("ID")+".bin"; saveOutputStream(fileOut,bodyOut); bodyOut.close(); System.out.println(" Body = (Saved in "+fileOut+")"); } res.close(); sta.close(); con.close(); } catch (Exception e) { System.err.println("Exception: "+e.getMessage()); e.printStackTrace(); } } public static void saveOutputStream(String name, InputStream body) { int c; try { OutputStream f = new FileOutputStream(name); while ((c=body.read())>-1) { f.write(c); } f.close(); } catch (Exception e) { e.printStackTrace(); } } }
The output looked correct:
herong> java -cp .;ojdbc11.jar OracleBlobGetBinaryStream Record ID: 2 Subject = Test of the setBytes() method Body = (Saved in BlobOut_2.bin) Record ID: 3 Subject = Test of setBinaryStream() methods Body = (Saved in BlobOut_3.bin) Record ID: 4 Subject = Error test of setBinaryStream() method Body = (Saved in BlobOut_4.bin)
I also tested BlobOut_3.bin file to make sure that I am getting my OracleBlobSetBinaryStream.class file back correctly:
herong> del OracleBlobSetBinaryStream.class herong> copy BlobOut_3.bin OracleBlobSetBinaryStream.class 1 file(s) copied. herong> java -cp .;ojdbc11.jar OracleBlobSetBinaryStream The inserted record: Subject = Test of setBinaryStream() methods Body = -||+ 2 | ...
Table of Contents
JDBC (Java Database Connectivity) Introduction
Oracle Express Edition Installation on Windows
Oracle - Reference Implementation of JdbcRowSet
Oracle - JBDC CallableStatement
Oracle CLOB (Character Large Object) - TEXT
►Oracle BLOB (Binary Large Object) - BLOB
Overview of BLOB (Binary Large Object)
Create Tables with CLOB Columns
Inserting BLOB Values with SQL INSERT Statements
Inserting BLOB Values with setBytes() Method
Inserting BLOB Values with setBinaryStream() Method
Closing InputStream Too Early on setBinaryStream()
Retrieving BLOB Values with getBytes() Method
►Retrieving BLOB Values with getBinaryStream() Method
Retrieving BLOB Values with getBlob() Method
Inserting BLOB Values with setBlob() Method
Copying BLOB Values to New Rows