File: JOCLPoolingDriverExample.java

package info (click to toggle)
libcommons-dbcp-java 1.4-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,952 kB
  • sloc: java: 16,699; xml: 2,245; jsp: 84; sh: 12; makefile: 2
file content (119 lines) | stat: -rw-r--r-- 4,424 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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-pool-1.5.4.jar
//  * commons-dbcp-1.2.2.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-pool-1.5.4.jar:commons-dbcp-1.2.2.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 { if (rset != null) rset.close(); } catch(Exception e) { }
            try { if (stmt != null) stmt.close(); } catch(Exception e) { }
            try { if (conn != null) conn.close(); } catch(Exception e) { }
        }
    }
}