-
1. Re: ORACLE BLOBS
pallotjk Jul 12, 2005 9:01 AM (in response to pallotjk)Answer to this is as follows:
1. ensure that the 10g version of ojdbc14.jar is deployed to server/default/lib (if using default)
2. write method to obtain BLOB from table and write it to an OutputStream (in this example has been placed in a class called ProductBO - see 3 below)
public void writeImageForProduct(String id, OutputStream os) throws DAOException {
Connection conn = null;
ResultSet rs = null;
int v = 0;
conn = connectionHelper.getConnection();
PreparedStatement stmt = null;
try
{
stmt = conn.prepareStatement("SELECT dvd_title_id, cover_image FROM dvdtitle where dvd_title_id = '" + id + "'");
rs = stmt.executeQuery();
while (rs.next())
{
Blob blob = rs.getBlob(2);
try {
InputStream imageStream = blob.getBinaryStream();
while ((v = imageStream.read()) != -1) {
os.write(v);
}
imageStream.close();
}
catch (IOException e) {
throw new DAOException(e);
}
}
rs.close();
}
catch (SQLException e) {
throw new DAOException("SQL Exception " + e.getMessage(), e);
}
finally {
connectionHelper.closeConnection(conn, null);
}
}
3. Extract from servlet:
.................
response.setContentType("image/gif");
ProductBO productBO = new ProductBO();
try {
ServletOutputStream outputStream = response.getOutputStream();
productBO.writeImageForProduct(dvd_image_id,outputStream);
response.flushBuffer();
} catch (ProductException e) {
throw new ServletException(e.getMessage());
}
..................