/****************************************************************************
* FILE NAME:  Long2Gif.java     TITLE: Export LONG RAW column to GIF file 
*
* AUTHOR:  Ken North, Ken North Computing, LLC
*
*      This is an example of retrieving Oracle LONG RAW data and writing
*      it into a GIF (image) file. Uses the Oracle thin JDBC driver. 
*
*      From XML, Java, and Database Magic (Prentice-Hall, 1999)
****************************************************************************
* Copyright (c) Kendall E. North II, 1999. All rights reserved. Reproduction
* or translation of this work beyond that permitted in Section 117 of the
* United States Copyright Act without express written permission of the
* copyright owner is unlawful.
*
* The purchaser may make backup copies for his/her own use only and not for
* distribution or resale. The Author and Publisher assume no responsibility
* errors, omissions or damages caused by the use of these programs or from
* use of the information contained herein.
****************************************************************************/

import java.io.*;
import java.sql.*;

class Long2Gif
{
  public static void main (String args [])
       throws SQLException, ClassNotFoundException, IOException
  {

 			   // load the Oracle driver
    Class.forName ("oracle.jdbc.driver.OracleDriver");
	
                   	   // define the thin driver connection URL
    String url = "jdbc:oracle:thin:@burns:1521:magic";

                           // connect to data source
    Connection conn = DriverManager.getConnection(url,"north","north2");

			    // create an SQL statement instance
    Statement stmt = conn.createStatement ();

                        // retrieve image from row when the ID is 'AveryE Logo'
    ResultSet rs = 
      stmt.executeQuery ("SELECT img_data FROM webimages WHERE img_ident='AveryE Logo'");
    
                        // process the ResultSet data 
    if (rs.next ())
      {
		      // get binary stream data from result set
      InputStream web_image = rs.getBinaryStream (1);

		      // open operating system file for storing the image
      FileOutputStream ostr = new FileOutputStream ("backlogo.gif");
      
		      // fetch-write loop, fetch image data from rs, write to file 
      int i;
      while ((i = web_image.read ()) != -1)
        ostr.write (i);

  		    // Close the destination output stream
      ostr.close ();
      }
  
		    // Close all the resources
    if (rs != null)
      rs.close();
    
    if (stmt != null)
      stmt.close();
    
    if (conn != null)
      conn.close();
  }
}