rwaldhoff    01/04/03 14:52:16

  Modified:    dbcp/doc README.txt
  Added:       dbcp/doc poolingDriverExample.jocl.sample
                        JOCLPoolingDriverExample.java
  Log:
  additional examples
  
  Revision  Changes    Path
  1.2       +11 -0     jakarta-commons-sandbox/dbcp/doc/README.txt
  
  Index: README.txt
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/dbcp/doc/README.txt,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- README.txt        2001/04/03 17:27:55     1.1
  +++ README.txt        2001/04/03 21:52:15     1.2
  @@ -14,6 +14,17 @@
    Look at the comments with that file for instructions on how to
    build and run it.
   
  +JOCLPoolingDriverExample.java
  +poolingDriverExample.jocl.sample
  +
  + Provides an example JOCL configuration and JDBC client that
  + shows how to use the DBCP PoolingDriver with an external
  + configuration. (JOCL will be replaced by Digester when it
  + is available in jakarta-commons.)
  +
  + Look at the comments with those files for instructions on how to
  + build and run it.
  +
   See also the JavaDoc documentation (use "ant doc" to generate it),
   especially the package documentation for org.apache.commons.dbcp
   for an overview of how it all works.
  
  
  
  1.1                  
jakarta-commons-sandbox/dbcp/doc/poolingDriverExample.jocl.sample
  
  Index: poolingDriverExample.jocl.sample
  ===================================================================
  <!--
   Sample JOCL configuration file for JOCLPoolingDriverExample.java.
  
   It's not pretty, but it works.  It will be replaced by Digester-based
   configuration as soon as it's available.
  
   See the JavaDocs for org.apache.commons.jocl.JOCLContentHandler for
   documentation on JOCL in general. The gist of it is an that it provides
   an XML description of a constructor to be invoked.
  
   See the JavaDocs for PoolableConnectionFactory et al for details
   on the object we are constructing. The inline comments may help as well.
  
   To use this with the JOCLPoolingDriverExample, make a copy of this
   file called it "poolingDriverExample.jocl", set the connection string below
   (search for "CHANGE ME"), and make sure the poolingDriverExample.jocl file
   is available in your classpath. (See JOCLPoolingDriverExample.java.)
  -->
  
  <!--
     The PoolingDriver expects PoolableConnectionFactory to be the root of the JOCL 
document.
   -->
  <object class="org.apache.commons.dbcp.PoolableConnectionFactory" 
xmlns="http://apache.org/xml/xmlns/jakarta/commons/jocl">
     <!--
        The first argument to PoolableConnectionFactory is a ConnectionFactory.
        We'll use a DriverManagerConnectionFactory, passing in the appropriate
        connect string for the underlying driver.
      -->
     <object class="org.apache.commons.dbcp.DriverManagerConnectionFactory">
        <string value="jdbc:oracle:thin:scott/tiger@myhost:1521:mysid"/> <!-- CHANGE 
ME TO THE CONNECT STRING FOR YOUR DRIVER -->
        <object class="java.util.Properties" null="true"/>
     </object>
     <!--
        The next argument is the pool to use.  We'll use a StackObjectPool,
        although any implementation of ObjectPool should suffice.
      -->
     <object class="org.apache.commons.pool.impl.StackObjectPool"/>
     <!--
        The next argument is the KeyedObjectPoolFactory to use to create pools
        for storing PreparedStatements.  This functionality is optional, we'll
        just use null.
      -->
     <object class="org.apache.commons.pool.KeyedObjectPoolFactory" null="true"/>
     <!--
        The next argument is the query to use to validate that a Connection is
        still up and running.  It should return at least one row.
        This functionality is optional. We'll just set it to null.
      -->
     <string null="true"/>
     <!-- The default "read only" value for Connections. -->
     <boolean value="false"/>
     <!-- The default "auto commit" value for Connections. -->
     <boolean value="true"/>
  </object>
  
  
  
  1.1                  jakarta-commons-sandbox/dbcp/doc/JOCLPoolingDriverExample.java
  
  Index: JOCLPoolingDriverExample.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons-sandbox/dbcp/doc/JOCLPoolingDriverExample.java,v 1.1 
2001/04/03 21:52:15 rwaldhoff Exp $
   * $Revision: 1.1 $
   * $Date: 2001/04/03 21:52:15 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  import java.sql.DriverManager;
  import java.sql.Connection;
  import java.sql.Statement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  
  
  //
  // Here's a simple example of how to use the PoolingDriver.
  // In this example, we'll construct the PoolingDriver implictly
  // using the JOCL configuration mechanism.
  //
  // Note that there is absolutely nothing DBCP specific about
  // this code, it's just straight JDBC.  You can simply
  // switch connection strings to use the "native" drivers
  // directly.
  //
  
  //
  // To compile this example, you'll need nothing but the JDK (1.2+)
  // in your classpath.
  //
  // To run this example, you'll want:
  //  * commons-collections.jar
  //  * commons-pool.jar
  //  * commons-dbcp.jar
  //  * the classes for your (underlying) JDBC driver
  //  * sax2.jar (the SAX 2 API)
  //  * a SAX2 friendly XML parser (jaxp.jar and parser.jar,
  //    for example)
  //  * the JOCL configuration for your database connection pool
  //    (poolingDriverExample.jocl, for example)
  // in your classpath.
  //
  // Invoke the class using two arguments:
  //  * the connect string for the JDBC driver (see below)
  //  * the query you'd like to execute
  // You'll also want to ensure your both your underlying JDBC
  // driver and the org.apache.commons.dbcp.PoolingDriver
  // are registered.  You can use the "jdbc.drivers"
  // property to do this.  Note that jdbc.drivers is colon
  // seperated list, on all platforms.
  //
  // Depending upon your XML parser, you may need to register
  // the "default" SAX driver, using the "org.xml.sax.driver"
  // property.
  //
  // For example, to invoke this class with an Oracle driver only
  // (no pooling):
  //
  //  java -Djdbc.drivers=oracle.jdbc.driver.OracleDriver \
  //       -classpath oracle-jdbc.jar:. \
  //       JOCLPoolingDriverExample
  //       "jdbc:oracle:thin:scott/tiger@myhost:1521:mysid"
  //       "SELECT * FROM DUAL"
  //
  // For pooling:
  //
  //  java 
-Djdbc.drivers=oracle.jdbc.driver.OracleDriver:org.apache.commons.dbcp.PoolingDriver \
  //       -classpath 
commons-collections.jar:commons-pool.jar:commons-dbcp.jar:oracle-jdbc.jar:jaxp.jar:parser.jar:sax2.jar:.
 \
  //       JOCLPoolingDriverExample
  //       "jdbc:apache:commons:dbcp:/poolingDriverExample"
  //       "SELECT * FROM DUAL"
  //
  // The last token in DBCP connect string (when suffixed with ".jocl")
  // is the resource the PoolingDriver reads as the JOCL configuration.
  // See Class.getResource for details on resource loading.
  //
  public class JOCLPoolingDriverExample {
  
      public static void main(String[] args) {
          //
          // Just plain-old JDBC.
          //
  
          Connection conn = null;
          Statement stmt = null;
          ResultSet rset = null;
  
          try {
              System.out.println("Creating connection.");
              conn = DriverManager.getConnection(args[0]);
              System.out.println("Creating statement.");
              stmt = conn.createStatement();
              System.out.println("Executing statement.");
              rset = stmt.executeQuery(args[1]);
              System.out.println("Results:");
              int numcols = rset.getMetaData().getColumnCount();
              while(rset.next()) {
                  for(int i=1;i<=numcols;i++) {
                      System.out.print("\t" + rset.getString(i));
                  }
                  System.out.println("");
              }
          } catch(SQLException e) {
              e.printStackTrace();
          } finally {
              try { rset.close(); } catch(Exception e) { }
              try { stmt.close(); } catch(Exception e) { }
              try { conn.close(); } catch(Exception e) { }
          }
      }
  }
  
  
  

Reply via email to