package examples.jdbc.informix4;
import java.util.Properties;
import java.sql.*;
import java.io.*;

/**
 *
 * This example makes a connection to a database that is using a
 * multibyte codeset. You will need to specify the proper URL and codeset
 * for your database. Consult your Informix installation guide for more
 * information.  
 * <p> 
 * The example inserts three Unicode characters into a
 * database and then performs a Select statement and reads the data back
 * into a Result Set. The data is then read in via getUnicodeStream,
 * converted to it's hexadecimal value, and displayed.
 *
 *<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>Change the line containing <font face="Courier New"
 *size=-1>weblogic.codeset=myCodeset</font>, replacing <font
 *face="Courier New" size=-1>myCodeset</font> with the name of the
 *codeset used by your database. For testing, try using "<font
 *face="Courier New" size=-1>cp850</font>".
 *
 *<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 PreparedStatementUnicode.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.PreparedStatementUnicode</b></font>
 *
 *</ol>
 * @author Copyright (c) 1999-2000 by BEA Systems, Inc. All rights reserved
 */


public class PreparedStatementUnicode {

  public static void main (String argv[]) 
       throws SQLException {

      Driver myDriver = null;
      try {
        myDriver = (Driver) Class.forName("weblogic.jdbc.informix4.Driver").newInstance();
      }
      catch (Exception dr){
        System.out.println("unable to load driver");
      }  
  
      // Connect to your database. Substitute the proper database
      // name, server, codeset, user, and password 
      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
      props.put("weblogic.codeset", "cp850"); // name of codeset

      Connection c =  myDriver.connect("jdbc:weblogic:informix4", props);

      try {
        Statement ct = c.createStatement();
        ct.executeUpdate("CREATE TABLE dbtest (id smallint,tcol TEXT)");
      }
      catch (Exception cte){
        System.out.println("database allready created");
      }

      //construct a prepared statement with one parameter
      PreparedStatement ps = 
          c.prepareStatement("insert into dbtest values (10,?)"); 
        
     // construct a UnicodeInputStream from a string and Insert it in
     // the database using the prepared statement 
     String s = new String("\u93e1\u68b0\u897f");
     weblogic.jdbcbase.informix4.UnicodeInputStream uis =
       new weblogic.jdbcbase.informix4.UnicodeInputStream(s);
     try {
       ps.setUnicodeStream(1,uis,uis.available());
     } 
     catch (java.io.IOException ioe) {
       System.out.println("IO Exception in setUnicodeStream");
     }
     ps.executeUpdate();
      
     // retrieve the data 
     Statement stmt = c.createStatement();
     stmt.execute("select * from dbtest");
     ResultSet rs = stmt.getResultSet();

     InputStream uisout;
     while (rs.next()) {
       uisout= rs.getUnicodeStream(2);
       
       //read in the UnicodeStream and display the bytes in hexadecimal
       int i = 0;   
       while (true) {
         try {
           i = uisout.read();
         } catch (IOException e) {
           System.out.println("IO exception reading Unicode stream");
         }
         if (i == -1) 
            break;
         System.out.println("Ox" + toHex(i) + "  ");
        }                
     }
     rs.close();
     stmt.close();
     c.close();
}

  // method to convert string to hexadecimal
  final static private char hex [] = 
  { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'A', 'B', 'C', 'D', 'E', 'F' };

  private static String toHex(int number) {
    String s = "";
    long v = number & 0xFFFFFFFF;

    for(int i = 0; (number > 0) || (i < 2); i++, number /= 16) {
        s = hex[number % 16] + s; 
    }
    return s;
  }
}











