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();
}

Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email