package examples.dbkona;

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

/**
 * This simple example shows how to connect and execute queries with
 * dbKona. You will need to have Oracle running the DEMO database,
 * with WebLogic jDriver for Oracle (or another Oracle JDBC driver). This example
 * is executed from the command line, and 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% query.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.query</b></font>
 *
 *</ol>
 *
 * @author        Copyright (c) 1996-2000 by BEA Systems, Inc. All Rights Reserved.
 */

public class query {
  
  /**
   * Here the JDBC connection is set up and created. A JDBC
   * Statement is executed, and its resulting data is used to
   * construct a dbKona QueryDataSet from the "emp" table
   * in the Oracle DEMO database.
   * <p>
   * A QueryDataSet simplifies the client-side management of
   * JDBC results by providing the infrastructure of a memory
   * cache. QueryDataSets also allow records to be fetched
   * incrementally. dbKona also provides methods associated
   * with DataSets to automatically generate SQL.
   * A QueryDataSet can be constructed with or without
   * a JDBC ResultSet, which is shown in this method.
   * <p>
   * After working with the DataSet, we close it, as well as
   * the JDBC ResultSet and the Statement used to execute the
   * query.
   */
  public static void main(String argv[]) {

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

    // A QueryDataSet simplifies the client-side management of result
    // sets by providing an the infrastructure of a memory cache.  A
    // QueryDataSet can be constructed with or without a JDBC
    // ResultSet, as shown below.
    QueryDataSet qs = 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");
    
      // This a QueryDataSet can be constructed directly from a Connection
      qs = new QueryDataSet(conn, "select * from emp");
      qs.fetchRecords();
      System.out.println("Record count = " + qs.size());
    
      // Close the DataSet.
      qs.close();
    
      // QueryDataSets allow records to be fetched incrementally.
      qs = new QueryDataSet(conn, "select * from emp");
      while (!qs.allRecordsRetrieved()) {
	// Fetch 2 records at a time . . .
	qs.fetchRecords(2);
	System.out.println("Record count = " + qs.size());
      
	// Clear the records in the cache.
	qs.clearRecords();  
      }

      // Close the DataSet.
      qs.close();

      stmt = conn.createStatement();
      stmt.execute("select * from emp");
      rs = stmt.getResultSet();
    
      // This QueryDataSet is constructed with a JDBC ResultSet.
      qs = new QueryDataSet(rs);
      qs.fetchRecords();
    
      System.out.println(qs.schema());
    
      for (int i = 0; i < qs.size(); i++) {
	Record rec = qs.getRecord(i);
	System.out.println(rec.getValue("empno").asString() + " - " + 
			   rec.getValue("ename").asString()  + " - " + 
			   rec.getValue("hiredate").asString());
      }
    
    } 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 {
	qs.close();
      } catch (Exception e) {}

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

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

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