001: package com.protomatter.util;
002:
003: /**
004: * {{{ The Protomatter Software License, Version 1.0
005: * derived from The Apache Software License, Version 1.1
006: *
007: * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
008: *
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution,
022: * if any, must include the following acknowledgment:
023: * "This product includes software developed for the
024: * Protomatter Software Project
025: * (http://protomatter.sourceforge.net/)."
026: * Alternately, this acknowledgment may appear in the software itself,
027: * if and wherever such third-party acknowledgments normally appear.
028: *
029: * 4. The names "Protomatter" and "Protomatter Software Project" must
030: * not be used to endorse or promote products derived from this
031: * software without prior written permission. For written
032: * permission, please contact support@protomatter.com.
033: *
034: * 5. Products derived from this software may not be called "Protomatter",
035: * nor may "Protomatter" appear in their name, without prior written
036: * permission of the Protomatter Software Project
037: * (support@protomatter.com).
038: *
039: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
040: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
041: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
042: * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
043: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
044: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
045: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
046: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
047: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
048: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
049: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
050: * SUCH DAMAGE. }}}
051: */
052:
053: import java.sql.*;
054: import java.util.*;
055: import com.protomatter.syslog.Syslog;
056:
057: /**
058: * Database-related utility class.
059: */
060: public class DatabaseUtil {
061: // make the constructor private so nobody calls it.
062: private DatabaseUtil() {
063: super ();
064: }
065:
066: /**
067: * Ensure that the given driver has been registered.
068: * This method makes sure that the given driver is
069: * loaded and registered with the JDBC Driver Manager.
070: * Calling this method multiple times will <i>not</i>
071: * result in the driver being registered twice.
072: */
073: public static void registerDriver(String driverName)
074: throws Exception {
075: boolean found = false;
076: Enumeration e = DriverManager.getDrivers();
077: while (e.hasMoreElements() && !found) {
078: Object o = e.nextElement();
079: if (o.getClass().getName().equals(driverName))
080: found = true;
081: }
082:
083: if (!found) {
084: Driver d = (Driver) Class.forName(driverName).newInstance();
085: found = false;
086: e = DriverManager.getDrivers();
087: while (e.hasMoreElements() && !found) {
088: Object o = e.nextElement();
089: if (o.getClass().getName().equals(driverName))
090: found = true;
091: }
092:
093: if (!found)
094: DriverManager.registerDriver(d);
095: }
096: }
097:
098: /**
099: * Close the given Connection. Return true when there's no
100: * exception thrown, false if there was one.
101: */
102: public static boolean close(Connection c) {
103: return close(c, DatabaseUtil.class);
104: }
105:
106: /**
107: * Close the given Connection. Return true when there's no
108: * exception thrown, false if there was one.
109: * The logger is passed onto a call to <tt>Syslog.log()</tt>
110: * in the event of an exception.
111: */
112: public static boolean close(Connection c, Object logger) {
113: if (c == null)
114: return true;
115: try {
116: c.close();
117: return true;
118: } catch (SQLException x) {
119: Syslog.log(logger, x);
120: }
121: return false;
122: }
123:
124: /**
125: * Close the given Statement. Return true when there's no
126: * exception thrown, false if there was one.
127: */
128: public static boolean close(Statement s) {
129: return close(s, DatabaseUtil.class);
130: }
131:
132: /**
133: * Close the given Statement. Return true when there's no
134: * exception thrown, false if there was one.
135: * The logger is passed onto a call to <tt>Syslog.log()</tt>
136: * in the event of an exception.
137: */
138: public static boolean close(Statement s, Object logger) {
139: if (s == null)
140: return true;
141: try {
142: s.close();
143: return true;
144: } catch (SQLException x) {
145: Syslog.log(logger, x);
146: }
147: return false;
148: }
149:
150: /**
151: * Close the given ResultSet. Return true when there's no
152: * exception thrown, false if there was one.
153: */
154: public static boolean close(ResultSet r) {
155: return close(r, DatabaseUtil.class);
156: }
157:
158: /**
159: * Close the given ResultSet. Return true when there's no
160: * exception thrown, false if there was one.
161: * The logger is passed onto a call to <tt>Syslog.log()</tt>
162: * in the event of an exception.
163: */
164: public static boolean close(ResultSet r, Object logger) {
165: if (r == null)
166: return true;
167: try {
168: r.close();
169: return true;
170: } catch (SQLException x) {
171: Syslog.log(logger, x);
172: }
173: return false;
174: }
175: }
|