KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > examples > Simple


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool.examples;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import java.sql.Connection JavaDoc;
12 import java.sql.DriverManager JavaDoc;
13 import java.sql.SQLException JavaDoc;
14
15 /**
16  * The simplest example of all. Just gets a Connection.
17  *
18  * @version $Revision: 1.7 $, $Date: 2006/01/18 14:40:03 $
19  * @author billhorsman
20  * @author $Author: billhorsman $ (current maintainer)
21  */

22 public class Simple {
23
24     private static final Log LOG = LogFactory.getLog(Simple.class);
25
26     private static void withoutProxool() {
27
28         Connection JavaDoc connection = null;
29         try {
30             Class.forName("org.hsqldb.jdbcDriver");
31             try {
32                 connection = DriverManager.getConnection("jdbc:hsqldb:test");
33             } catch (SQLException JavaDoc e) {
34                 LOG.error("Problem getting connection", e);
35             }
36
37             if (connection != null) {
38                 LOG.info("Got connection :)");
39             } else {
40                 LOG.error("Didn't get connection, which probably means that no Driver accepted the URL");
41             }
42
43         } catch (ClassNotFoundException JavaDoc e) {
44             LOG.error("Couldn't find driver", e);
45         } finally {
46             try {
47                 // Check to see we actually got a connection before we
48
// attempt to close it.
49
if (connection != null) {
50                     connection.close();
51                 }
52             } catch (SQLException JavaDoc e) {
53                 LOG.error("Problem closing connection", e);
54             }
55         }
56     }
57
58     private static void withProxool() {
59
60         Connection JavaDoc connection = null;
61         try {
62             // NOTE THIS LINE
63
Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
64             try {
65                 // NOTE THIS LINE
66
connection = DriverManager.getConnection("proxool.example:org.hsqldb.jdbcDriver:jdbc:hsqldb:test");
67             } catch (SQLException JavaDoc e) {
68                 LOG.error("Problem getting connection", e);
69             }
70
71             if (connection != null) {
72                 LOG.info("Got connection :)");
73             } else {
74                 LOG.error("Didn't get connection, which probably means that no Driver accepted the URL");
75             }
76
77         } catch (ClassNotFoundException JavaDoc e) {
78             LOG.error("Couldn't find driver", e);
79         } finally {
80             try {
81                 // Check to see we actually got a connection before we
82
// attempt to close it.
83
if (connection != null) {
84                     connection.close();
85                 }
86             } catch (SQLException JavaDoc e) {
87                 LOG.error("Problem closing connection", e);
88             }
89         }
90     }
91
92     /**
93      * Tests getting a connection with and without Proxool
94      */

95     public static void main(String JavaDoc[] args) {
96         withoutProxool();
97         withProxool();
98     }
99
100 }
101
102 /*
103  Revision history:
104  $Log: Simple.java,v $
105  Revision 1.7 2006/01/18 14:40:03 billhorsman
106  Unbundled Jakarta's Commons Logging.
107
108  Revision 1.6 2003/03/03 11:12:02 billhorsman
109  fixed licence
110
111  Revision 1.5 2003/02/06 17:41:03 billhorsman
112  now uses imported logging
113
114  Revision 1.4 2003/02/06 15:42:48 billhorsman
115  updated (overdue!)
116
117  Revision 1.3 2002/12/03 10:54:04 billhorsman
118  use hypersonic driver
119
120  Revision 1.2 2002/09/19 10:01:37 billhorsman
121  improved error handling and logging
122
123  Revision 1.1 2002/09/19 08:51:09 billhorsman
124  created new examples package
125
126  Revision 1.1.1.1 2002/09/13 08:14:27 billhorsman
127  new
128
129  Revision 1.4 2002/07/10 16:14:47 billhorsman
130  widespread layout changes and move constants into ProxoolConstants
131
132  Revision 1.3 2002/07/02 11:19:08 billhorsman
133  layout code and imports
134
135  Revision 1.2 2002/06/28 11:19:47 billhorsman
136  improved doc
137
138 */

139
Popular Tags