package examples.jdbc.oracle20;
import java.sql.*;

/**
 * This example demonstrates the use of the JDBC 2.0 Batch Update feature with an Oracle database. 
 * <p>To run this example:
 * <ol>
 * <li>Set up your development shell as described in <a
 * href=../../../techstart/environment.html>Setting up your environment</a>. You must run this example under a supported JDK 1.2 JVM <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% batchUpdate.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.oracle20.batchUpdate</b></font>
 *
 *</ol> 
 * @author Copyright (c) 2000 by BEA Systems, Inc.  All Rights Reserved.
 */

public class batchUpdate {
  
  public static void main(String argv[])
       throws Exception
  {

     /* To run this example, substitute: 
           the host name of your Oracle server for "myServer"
           the login for your Oracle server for "USERNAME"
           the password for "user" for "PASSWORD"
      */   

      // get a connection     
      Class.forName("weblogic.jdbc20.oci.Driver").newInstance();
      Connection conn = DriverManager.getConnection
          ("jdbc20:weblogic:oracle:myServer", "USERNAME", "PASSWORD");
      
      // create a table 
      try {
          // if table does not exist, create it. 
          Statement crstmt = conn.createStatement();
          System.out.println("\nCreating table ...");
          crstmt.execute
              ("create table batchTest (id VARCHAR(10), col2 NUMBER, col3 NUMBER)");
          crstmt.close();
      }
      catch (Exception e) {
          System.out.println("Exception: " + e);
          System.out.println("Table already exists. Dropping it and re-creating...");
          Statement crstmt2 = conn.createStatement();
          crstmt2.execute("drop table batchTest");
          crstmt2.execute
              ("create table batchTest (id VARCHAR(10), col2 NUMBER, col3 NUMBER)");
          crstmt2.close();
      }
      System.out.println("Table created.");

      // create a Statement object
      Statement stmt = conn.createStatement();

      // add SQL statements to the batch
      stmt.addBatch("INSERT INTO batchTest VALUES ('JOE', 20,35)");
      stmt.addBatch("INSERT INTO batchTest VALUES ('Bob', 30,44)");
      stmt.addBatch("INSERT INTO batchTest VALUES ('Ed',  34,22)");


      // send the statements to the DBMS in a try/catch block 
      // to catch exceptions
      try {
          stmt.executeBatch();
      } catch (Exception e) {
          System.out.println("Exception while executing batch:\n " + e);
      }

      //display the updated rows
      Statement dstmt;
      dstmt = conn.createStatement();
      dstmt.execute("SELECT * FROM batchTest");
      ResultSet rs =  dstmt.getResultSet();

      while (rs.next() ) {
          System.out.println(rs.getString("id") + " " 
                             + rs.getInt("col2") + " " +
                             rs.getInt("col3"));
      }
                       
      // Drop the table and clean up connections
      System.out.println("\nDropping table...");
      Statement dropstmt = conn.createStatement();
      dropstmt.execute("drop table batchTest");
      System.out.println("Table dropped.");
      conn.close();


  }
    
}










