package examples.dbkona;

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

/**
 * This class demonstrates using a dbKona Sequence object to create,
 * use, and drop an Oracle sequence. A dbKona Sequence object is a
 * wrapper for an Oracle sequence.
 * <p>
 * To run this example, you'll need an Oracle database.  The example
 * is executed from the command line and results are displayed to
 * standard out.
 *<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% sequences.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.sequences</b></font>
 *
 *</ol>
 *
 * @author       Copyright (c) 1996-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class sequences {
  
  /**
   * Here the JDBC connection is configured and made. Before we
   * attempt to create a new dbKona Sequence, which is always 
   * used in context of the JDBC connection object, we first try 
   * to drop a Sequence of the same name. Then the Sequence is 
   * created and its next value is printed out in a loop from 
   * 0 - 9. Finally the Sequence is dropped and closed, and the 
   * JDBC connection is closed.
   */
  public static void main(String argv[]) {
    
    Connection conn = null;
    Sequence seq = null;

    try {
      // For more information on making a connection, see the
      // dbKona Developers Guide.
      Class.forName("weblogic.jdbc.oci.Driver").newInstance();
      conn = DriverManager.getConnection("jdbc:weblogic:oracle:DEMO",
						  "scott",
						  "tiger");
    
      // Drop the sequence from the server if it already exists.
      try {Sequence.drop(conn, "testseq");} catch (Exception e) {;}
    
      // Create a new sequence on the server. The first integer argument
      // is the increment, the second is the starting value.
      Sequence.create(conn, "testseq", 1, 1);
    
      seq = new Sequence(conn, "testseq");
    
      // Print out the next value in the sequence in a loop.
      for (int i = 0; i < 10; i++) {
	System.out.println(seq.nextValue());
      }
    
      System.out.println(seq.currentValue());
    } 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 {
	seq.close();
      } catch (Exception e) {}

      // Drop the sequence from the server.
      try {
	Sequence.drop(conn, "testseq");
      } catch (Exception e) {
	System.err.println("Trouble removing sequence from DBMS");
	e.printStackTrace();
      }

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

