Java BLOB Example

Database can store not only numbers and strings, but also images. SQL3 introduced a new data type BLOB (Binary Large OBject) for storing binary data, which can be used to store images.
BLOB : Reading
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery
    (“select blob_column from blob_table”);
while (rs.next())
{
BLOB blob = ((OracleResultSet)rs).getBLOB(1);
InputStream is = blob.getBinaryStream();
int read =  0;
while ( (read  = is.read()) != -1)
{
// to do like writing a file using the stream
}
is.close();
}
BLOB : Writing
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery
  (“select blob_column from blob_table for update”);
while (rs.next())
{
BLOB blob = ((OracleResultSet)rs).getBLOB(1);
OutputStream os = blob.getBinaryOutputStream();
InputStream src = new InputStream(…);
byte [] buffer = new byte[1024];
int read  = 0;
while ( (read  = src.read(buffer)) != -1)
{
os.write(buffer, 0, read);  // write blob.
}
src.close();
os.close();
}

Search: