KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > util > DatabaseUtil


1 package com.protomatter.util;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.sql.*;
54 import java.util.*;
55 import com.protomatter.syslog.Syslog;
56
57 /**
58  * Database-related utility class.
59  */

60 public class DatabaseUtil
61 {
62   // make the constructor private so nobody calls it.
63
private DatabaseUtil()
64   {
65     super();
66   }
67
68   /**
69    * Ensure that the given driver has been registered.
70    * This method makes sure that the given driver is
71    * loaded and registered with the JDBC Driver Manager.
72    * Calling this method multiple times will <i>not</i>
73    * result in the driver being registered twice.
74    */

75   public static void registerDriver(String JavaDoc driverName)
76   throws Exception JavaDoc
77   {
78     boolean found = false;
79     Enumeration e = DriverManager.getDrivers();
80     while (e.hasMoreElements() && !found)
81     {
82       Object JavaDoc o = e.nextElement();
83       if (o.getClass().getName().equals(driverName))
84         found = true;
85     }
86
87     if (!found)
88     {
89       Driver d = (Driver)Class.forName(driverName).newInstance();
90       found = false;
91       e = DriverManager.getDrivers();
92       while (e.hasMoreElements() && !found)
93       {
94         Object JavaDoc o = e.nextElement();
95         if (o.getClass().getName().equals(driverName))
96           found = true;
97       }
98
99       if (!found)
100         DriverManager.registerDriver(d);
101     }
102   }
103
104   /**
105    * Close the given Connection. Return true when there's no
106    * exception thrown, false if there was one.
107    */

108   public static boolean close(Connection c)
109   {
110     return close(c, DatabaseUtil.class);
111   }
112
113   /**
114    * Close the given Connection. Return true when there's no
115    * exception thrown, false if there was one.
116    * The logger is passed onto a call to <tt>Syslog.log()</tt>
117    * in the event of an exception.
118    */

119   public static boolean close(Connection c, Object JavaDoc logger)
120   {
121     if (c == null) return true;
122     try
123     {
124       c.close();
125       return true;
126     }
127     catch (SQLException x)
128     {
129       Syslog.log(logger, x);
130     }
131     return false;
132   }
133
134   /**
135    * Close the given Statement. Return true when there's no
136    * exception thrown, false if there was one.
137    */

138   public static boolean close(Statement s)
139   {
140     return close(s, DatabaseUtil.class);
141   }
142
143   /**
144    * Close the given Statement. Return true when there's no
145    * exception thrown, false if there was one.
146    * The logger is passed onto a call to <tt>Syslog.log()</tt>
147    * in the event of an exception.
148    */

149   public static boolean close(Statement s, Object JavaDoc logger)
150   {
151     if (s == null) return true;
152     try
153     {
154       s.close();
155       return true;
156     }
157     catch (SQLException x)
158     {
159       Syslog.log(logger, x);
160     }
161     return false;
162   }
163
164   /**
165    * Close the given ResultSet. Return true when there's no
166    * exception thrown, false if there was one.
167    */

168   public static boolean close(ResultSet r)
169   {
170     return close(r, DatabaseUtil.class);
171   }
172
173   /**
174    * Close the given ResultSet. Return true when there's no
175    * exception thrown, false if there was one.
176    * The logger is passed onto a call to <tt>Syslog.log()</tt>
177    * in the event of an exception.
178    */

179   public static boolean close(ResultSet r, Object JavaDoc logger)
180   {
181     if (r == null) return true;
182     try
183     {
184       r.close();
185       return true;
186     }
187     catch (SQLException x)
188     {
189       Syslog.log(logger, x);
190     }
191     return false;
192   }
193 }
194
Popular Tags