package examples.jdbc.informix4;

import java.io.*;
import java.sql.*;
import java.util.Properties;

/**
 * 
 *This example shows how to insert data into an Informix BLOB column.
 * It demonstrates this by serializing an object and storing it in the
 * database and then retrieving the object.
 * <p>
 *
 *<h3>To set up this example:</h3>
 *<ol>
 *<li>Set up your development shell as described in <a
 *href=Package-examples.jdbc.informix4.html>Package examples.jdbc.informix4</a>.
 *<p>
 *<li> Change connection parameters to correspond to your Informix
 *Dynamic Server configuration.  If you need more help, check the section on
 *connecting to a database in the Developers Guide, <a
 *href=../../../classdocs/API_jinf4.html>Using WebLogic jDriver for Informix</a>.
 *
 *<p>
 *
 *<li>Compile this example by executing the following command in your development shell:
 *
 *<p>
 *
 *<font face="Courier New"><b>$ javac -d c:/weblogic/informix4/classes serializer.java</b></font>
 *<p>	
 *
 *<li>Run this example by executing the following command in your development shell:
 *
 *<p>
 *
 *<font face="Courier New"><b>$ java examples.jdbc.informix4.serializer</b></font>
 *
 *</ol>
 *
 *This example shows how to insert data into an Informix BLOB column.
 * It demonstrates this by serializing an object and storing it in the
 * database and then retrieving the object.
 * <p>
 * This example is run on the command line and is output to
 * System.out.
 *
 * @author Copyright (c) 1998-2000 by BEA Systems, Inc.  All Rights Reserved.
 */

public class serializer {

  public static void main(String args[]) throws Exception {

    Connection conn = null;
    Statement stmt = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;

    Properties props = new Properties();
    props.put("user",     "informix");
    props.put("password", "secret");
    props.put("server",   "myDBHost"); // hostname on which Informix server runs
    props.put("port",     "1493");       // port on which Informix server listens
    props.put("db",       "myDB");  // name of database on Informix server

    Driver myDriver = (Driver) Class.forName("weblogic.jdbc.informix4.Driver").newInstance();
    conn = myDriver.connect("jdbc:weblogic:informix4", props);

    stmt = conn.createStatement();
    try {
      stmt.execute("drop table blobtest");
      System.out.println("Dropped table blobtest");
    }
    catch (SQLException e) {
      // table does not exist
    }
    stmt.execute("create table blobtest (foo byte)");
    System.out.println("creating blobtest table");

    // create an instance of aClass and serialize it into a byte array
    aClass a = new aClass(1234);
    System.out.println("Created aClass with value 1234");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream (baos);
    oos.writeObject(a);
    byte[] aBytes = baos.toByteArray();

    // write the byte array to the database
    pstmt = conn.prepareStatement("insert into blobtest(foo) values (?)");
    pstmt.setBinaryStream(1, new ByteArrayInputStream(aBytes),aBytes.length);
    pstmt.executeUpdate();
    System.out.println("Inserted aClass into the database");
    a = null;
    aBytes = null;

    // retrieve the serialized object back from the database and instantiate a new copy
    rs = stmt.executeQuery("select foo from blobtest");
    rs.next();
    ObjectInputStream ois = new ObjectInputStream(rs.getBinaryStream(1));
    aClass newa = (aClass)ois.readObject();
    System.out.println("Retrieved the aClass object from the database with value " + newa.getVal());

    rs.close();
    pstmt.close();
    conn.close();
  }

}

class aClass implements Serializable {
  int aValue;
  public aClass(int v) { aValue = v; }
  public int getVal() { return aValue; }
}
