/****************************************************************************
* FILE NAME:  Gif2Long.java     TITLE: Import GIF image into LONG RAW column
*
* AUTHOR:  Ken North, Ken North Computing, LLC
*
*      This is an example of reading a GIF image and storing it into
*      a LONG RAW using 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 Gif2Long
{
  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 ();

                            // Create the webimages table
    try
    {
      stmt.execute ("DROP TABLE webimages");
    }
    catch (SQLException e)
    {
		      // Ignore exceptions from DROP TABLE
    }

                     // execute CREATE for webimages table
    stmt.execute 
      ("CREATE TABLE webimages (img_ident VARCHAR2 (64), img_fmt CHAR (5), img_data LONG RAW)");

                                 // Insert data into the img_data column  
    File file = new File ("averye.gif");
    InputStream istr = new FileInputStream ("averye.gif");

                                 // prepare the INSERT into webimages using parameters
    PreparedStatement pstmt = conn.prepareStatement 
      ("INSERT INTO webimages (img_ident, img_fmt, img_data) VALUES (?, ?, ?)");

                                 // use a prepared INSERT for webimages 
    pstmt.setString (1, "AveryE Logo");
    pstmt.setString (2, "GIF");
    pstmt.setBinaryStream (3, istr, (int)file.length ());
    pstmt.execute ();

  
                                 // close connection and statement   
   
    if (pstmt != null)
      pstmt.close();

    if (conn != null)
      conn.close();
  }
}