The standard solution is to do something like this:
try{
PreparedStatement ps=conn.prepareStatement("SELECT graphic_blob from graphic_table where primary_key = ?");
ps.setString(1,sTemplateKey);
ResultSet rs=ps.executeQuery();
if(rs.next()){
{
Blob b=rs.getBlob(1);
InputStream in =b.getBinaryStream();
int n=in.read();
while(n!=-1) {
out.write(n);
n=in.read();
//System.out.print("n: " + n);
}
int sheetCount = wb.getNumberOfSheets();
System.out.print( "sheetCount for createWorkbook(out): " + sheetCount);
out.flush();
out.close();
in.close();
if (theRS != null) theRS.close();
if (stmt != null) stmt.close();
}// end if for while loop
}// end of resultset fetch
}// end of try
catch (SQLException e){
String theError =e.getMessage() ;
if (theError.startsWith("Io exception")){
theError = "<b>Connection Timed Out</b><br><br>"+theError;
}
System.out.print( "connection failed because: " + theError);
out.println(sTheHeader);
h.add(helperHTML.HEADING,theError, true);
h.add(helperHTML.LINE,"", true);
h.add(helperHTML.NORMAL,sSQLString, true);
sThePage = h.getPage();
out.println(sThePage);
out.println("</body>");
out.println("</html>");
out.close();
return;
}//end of catch