Re: get image from database
Posted By:
Roshan_Shrestha
Posted On:
Wednesday, August 15, 2001 05:32 AM
Connection con = null;
PreparedStatement stmt = null;
try
{
// get connection
String queryString= "SELECT IMAGE FROM TABLE_NAME WHERE IMAGENAME=?
stmt =
con.prepareStatement(queryString);
stmt.setString (1, imageName);
stmt.executeQuery();
ResultSet rs = stmt.getResultSet();
if( rs.next() )
{
// set mime type
response.setContentType( "image/gif");
InputStream is =
rs.getBinaryStream(1 );
OutputStream os =
response.getOutputStream();
byte b[] = new byte[16384];
int numBytes;
while( ( numBytes=is.read( b ) )!=-1 )
{
os.write( b, 0, numBytes);
os.flush();
}
is.close();
os.close();
}
catch( IOException e)
{
}
finally
{
// free statement and connection
}