package examples.dbkona;

import weblogic.db.jdbc.*;
import java.sql.*;
import java.util.Properties;

/**
 * This example constructs a TableDataSet for inserting, updating,
 * and deleting records from an Oracle database. It illustrates
 * the use of an Oracle ROWID as a unique key for updates and deletes.
 * <p>
 * To run this example, you must have the Oracle DEMO database
 * running. The connection to the database is made with
 * WebLogic jDriver for Oracle. This example is run from the command line and the
 * results are displayed to standard out. For other examples of how to
 * display query results, check the server-side Java examples that use
 * htmlKona and dbKona in the examples/htmlkona folder.
 * <p>
 * If you don't have the Oracle DEMO database, you can
 * use the file <tt>examples/applets/emp.sql</tt> to set it up.
 *<h3>To set up this example:</h3>
 *<ol>
 *<li>Set up your development shell as described in 
 *<a href=../../techstart/environment.html>Setting up your environment</a>.
 *<p>
 *<li> Change connection parameters to correspond to your Oracle configuration.
 * If you need more help, check the section on connecting
 *to a database in the Developers Guide, <a
 *href=../../classdocs/API_joci.html>Using WebLogic jDriver for Oracle</a>.
 *<p>
 *
 *<li>Compile this example by executing the following command in your development shell:
 *
 *<p>
 *
 *<font face="Courier New"><b>$ javac -d %CLIENT_CLASSES% rowid.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.dbkona.rowid</b></font>
 *
 *</ol>
 *
 * @author    Copyright (c) 1996-2000 by BEA Systems, Inc. All Rights Reserved.
 */

public class rowid {
  
  /**
   * First the JDBC connection is configured and made.
   * Then a dbKona TableDataSet is constructed using the JDBC
   * connection object and the name of the table. This example
   * illustrates how to add new records and save them selectively.
   * Then a KeyDef object (a unique attribute identifier) is
   * created for updates and deletes; the records that were
   * previously added are first updated, and then deleted to
   * illustrate code for these two tasks. A final call to the
   * <tt>save()</tt> method updates the DBMS with the changes made.
   * The last few lines of code close all the data objects that
   * were used in this example.
   */
  public static void main(String argv[]) {
    Connection conn = null;
    TableDataSet ts1 = null;
    TableDataSet ts2 = null;

    try {
      // For more information on making a connection, check the
      // dbKona Developers Guide.
      Class.forName("weblogic.jdbc.oci.Driver").newInstance();
      conn = DriverManager.getConnection("jdbc:weblogic:oracle:DEMO",
						    "scott",
						    "tiger");
    
      // A TableDataSet simplifies the task of inserting, updating, and
      // deleting records by automatically generating the DML SQL.
    
      // Here we insert 100 records.
      ts1 = new TableDataSet(conn, "emp");
      for (int i = 0; i < 100; i++) {
	Record rec = ts1.addRecord();
	rec.setValue("empno",  i)
	  .setValue("ename",  "Person " + i)
	  .setValue("deptno", i);
      }
    
      // Save new records. dbKona does selective saves, that is,
      // it saves only those records in the TableDataSet.
      System.out.println("Inserting " + ts1.size() + " records.");
      ts1.save();
    
      // Define a KeyDef for updates and deletes.
      // In this case, we use an Oracle-specific attribute, ROWID,
      // which can act as a primary key for updates and deletes.
      KeyDef key = new KeyDef().addAttrib("ROWID");
    
      // Update the 100 records we originally added.
      ts2 = new TableDataSet(conn, "emp", "ROWID, deptno", key);
      ts2.where("empno < 100");
      ts2.fetchRecords();
    
      for (int i = 0; i < ts2.size(); i++) {
	Record rec = ts2.getRecord(i);
	rec.setValue("deptno", rec.getValue("deptno").asInt() - 1 );
      }
    
      // Save the updated records.
      System.out.println("Update " + ts2.size() + " records.");
      ts2.save();
    
    // Delete the same 100 records.
      ts2.reset();
      ts2.fetchRecords();
    
      for (int i = 0; i < ts2.size(); i++) {
	Record rec = ts2.getRecord(i);
	rec.markToBeDeleted();
      }
    
      // Delete records from server.
      System.out.println("Delete " + ts2.size() + " records.");
      ts2.save();

    } catch(Exception e) {
      System.err.println("Trouble while executing example");
      e.printStackTrace();
    } finally {
      // You should always close DataSets, ResultSets, and Statements
      // when you have finished working with them.
      try {
	ts1.close();
      } catch (Exception e) {}

      try {
	ts2.close();
      } catch (Exception e) {}

      // Finally, close the connections.
      try {
	conn.close();
      } catch(Exception e) {}
    }
  }

}

