KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > dbutils > DbUtils


1 /*
2  * $Header: /home/cvs/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/DbUtils.java,v 1.2 2003/11/03 00:22:57 dgraham Exp $
3  * $Revision: 1.2 $
4  * $Date: 2003/11/03 00:22:57 $
5  *
6  * ====================================================================
7  *
8  * The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
11  * reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowledgement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowledgement may appear in the software itself,
30  * if and wherever such third-party acknowledgements normally appear.
31  *
32  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache"
38  * nor may "Apache" appear in their names without prior written
39  * permission of the Apache Software Foundation.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  *
60  */

61
62 package org.apache.commons.dbutils;
63
64 import java.io.PrintWriter JavaDoc;
65 import java.sql.Connection JavaDoc;
66 import java.sql.ResultSet JavaDoc;
67 import java.sql.SQLException JavaDoc;
68 import java.sql.Statement JavaDoc;
69
70 /**
71  * A collection of JDBC helper methods. This class is thread safe.
72  *
73  * @author Henri Yandell
74  * @author Juozas Baliuka
75  * @author Steven Caswell
76  * @author David Graham
77  */

78 public final class DbUtils {
79
80     /**
81      * Close a <code>Connection</code>, avoid closing if null.
82      */

83     public static void close(Connection JavaDoc conn) throws SQLException JavaDoc {
84         if (conn != null) {
85             conn.close();
86         }
87     }
88
89     /**
90      * Close a <code>ResultSet</code>, avoid closing if null.
91      */

92     public static void close(ResultSet JavaDoc rs) throws SQLException JavaDoc {
93         if (rs != null) {
94             rs.close();
95         }
96     }
97
98     /**
99      * Close a <code>Statement</code>, avoid closing if null.
100      */

101     public static void close(Statement JavaDoc stmt) throws SQLException JavaDoc {
102         if (stmt != null) {
103             stmt.close();
104         }
105     }
106
107     /**
108      * Close a <code>Connection</code>, avoid closing if null and hide
109      * any SQLExceptions that occur.
110      */

111     public static void closeQuietly(Connection JavaDoc conn) {
112         try {
113             close(conn);
114         } catch (SQLException JavaDoc sqle) {
115             // quiet
116
}
117     }
118
119     /**
120      * Close a <code>Connection</code>, <code>Statement</code> and
121      * <code>ResultSet</code>. Avoid closing if null and hide any
122      * SQLExceptions that occur.
123      */

124     public static void closeQuietly(
125         Connection JavaDoc conn,
126         Statement JavaDoc stmt,
127         ResultSet JavaDoc rs) {
128             
129         closeQuietly(rs);
130         closeQuietly(stmt);
131         closeQuietly(conn);
132     }
133
134     /**
135      * Close a <code>ResultSet</code>, avoid closing if null and hide
136      * any SQLExceptions that occur.
137      */

138     public static void closeQuietly(ResultSet JavaDoc rs) {
139         try {
140             close(rs);
141         } catch (SQLException JavaDoc sqle) {
142             // quiet
143
}
144     }
145
146     /**
147      * Close a <code>Statement</code>, avoid closing if null and hide
148      * any SQLExceptions that occur.
149      */

150     public static void closeQuietly(Statement JavaDoc stmt) {
151         try {
152             close(stmt);
153         } catch (SQLException JavaDoc sqle) {
154             // quiet
155
}
156     }
157
158     /**
159      * Commits a <code>Connection</code> then closes it, avoid closing if null.
160      */

161     public static void commitAndClose(Connection JavaDoc conn) throws SQLException JavaDoc {
162         if (conn != null) {
163             conn.commit();
164             conn.close();
165         }
166     }
167
168     /**
169      * Commits a <code>Connection</code> then closes it, avoid closing if null
170      * and hide any SQLExceptions that occur.
171      */

172     public static void commitAndCloseQuietly(Connection JavaDoc conn) {
173         try {
174             commitAndClose(conn);
175         } catch (SQLException JavaDoc sqle) {
176             // quiet
177
}
178     }
179
180     /**
181      * Loads and registers a database driver class.
182      * If this succeeds, it returns true, else it returns false.
183      */

184     public static boolean loadDriver(String JavaDoc driverClassName) {
185         try {
186             Class.forName(driverClassName).newInstance();
187             return true;
188
189         } catch (ClassNotFoundException JavaDoc e) {
190             // TODO Logging?
191
//e.printStackTrace();
192
return false;
193
194         } catch (IllegalAccessException JavaDoc e) {
195             // TODO Logging?
196
//e.printStackTrace();
197

198             // Constructor is private, OK for DriverManager contract
199
return true;
200
201         } catch (InstantiationException JavaDoc e) {
202             // TODO Logging?
203
//e.printStackTrace();
204
return false;
205
206         } catch (Throwable JavaDoc t) {
207             return false;
208         }
209     }
210
211     public static void printStackTrace(SQLException JavaDoc sqle) {
212         printStackTrace(sqle, new PrintWriter JavaDoc(System.err));
213     }
214
215     public static void printStackTrace(SQLException JavaDoc sqle, PrintWriter JavaDoc pw) {
216
217         SQLException JavaDoc next = sqle;
218         while (next != null) {
219             next.printStackTrace(pw);
220             next = next.getNextException();
221             if (next != null) {
222                 pw.println("Next SQLException:");
223             }
224         }
225     }
226
227     public static void printWarnings(Connection JavaDoc connection) {
228         printWarnings(connection, new PrintWriter JavaDoc(System.err));
229     }
230
231     public static void printWarnings(Connection JavaDoc conn, PrintWriter JavaDoc pw) {
232         if (conn != null) {
233             try {
234                 printStackTrace(conn.getWarnings(), pw);
235             } catch (SQLException JavaDoc sqle) {
236                 printStackTrace(sqle, pw);
237             }
238         }
239     }
240
241     /**
242      * Rollback any changes made on the given connection.
243      * @param conn The database Connection to rollback. A null value is legal.
244      * @throws SQLException
245      */

246     public static void rollback(Connection JavaDoc conn) throws SQLException JavaDoc {
247         if (conn != null) {
248             conn.rollback();
249         }
250     }
251
252 }
253
Popular Tags