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. package org.apache.commons.dbutils;
  62. import java.io.PrintWriter;
  63. import java.sql.Connection;
  64. import java.sql.ResultSet;
  65. import java.sql.SQLException;
  66. import java.sql.Statement;
  67. /**
  68. * A collection of JDBC helper methods. This class is thread safe.
  69. *
  70. * @author Henri Yandell
  71. * @author Juozas Baliuka
  72. * @author Steven Caswell
  73. * @author David Graham
  74. */
  75. public final class DbUtils {
  76. /**
  77. * Close a <code>Connection</code>, avoid closing if null.
  78. */
  79. public static void close(Connection conn) throws SQLException {
  80. if (conn != null) {
  81. conn.close();
  82. }
  83. }
  84. /**
  85. * Close a <code>ResultSet</code>, avoid closing if null.
  86. */
  87. public static void close(ResultSet rs) throws SQLException {
  88. if (rs != null) {
  89. rs.close();
  90. }
  91. }
  92. /**
  93. * Close a <code>Statement</code>, avoid closing if null.
  94. */
  95. public static void close(Statement stmt) throws SQLException {
  96. if (stmt != null) {
  97. stmt.close();
  98. }
  99. }
  100. /**
  101. * Close a <code>Connection</code>, avoid closing if null and hide
  102. * any SQLExceptions that occur.
  103. */
  104. public static void closeQuietly(Connection conn) {
  105. try {
  106. close(conn);
  107. } catch (SQLException sqle) {
  108. // quiet
  109. }
  110. }
  111. /**
  112. * Close a <code>Connection</code>, <code>Statement</code> and
  113. * <code>ResultSet</code>. Avoid closing if null and hide any
  114. * SQLExceptions that occur.
  115. */
  116. public static void closeQuietly(
  117. Connection conn,
  118. Statement stmt,
  119. ResultSet rs) {
  120. closeQuietly(rs);
  121. closeQuietly(stmt);
  122. closeQuietly(conn);
  123. }
  124. /**
  125. * Close a <code>ResultSet</code>, avoid closing if null and hide
  126. * any SQLExceptions that occur.
  127. */
  128. public static void closeQuietly(ResultSet rs) {
  129. try {
  130. close(rs);
  131. } catch (SQLException sqle) {
  132. // quiet
  133. }
  134. }
  135. /**
  136. * Close a <code>Statement</code>, avoid closing if null and hide
  137. * any SQLExceptions that occur.
  138. */
  139. public static void closeQuietly(Statement stmt) {
  140. try {
  141. close(stmt);
  142. } catch (SQLException sqle) {
  143. // quiet
  144. }
  145. }
  146. /**
  147. * Commits a <code>Connection</code> then closes it, avoid closing if null.
  148. */
  149. public static void commitAndClose(Connection conn) throws SQLException {
  150. if (conn != null) {
  151. conn.commit();
  152. conn.close();
  153. }
  154. }
  155. /**
  156. * Commits a <code>Connection</code> then closes it, avoid closing if null
  157. * and hide any SQLExceptions that occur.
  158. */
  159. public static void commitAndCloseQuietly(Connection conn) {
  160. try {
  161. commitAndClose(conn);
  162. } catch (SQLException sqle) {
  163. // quiet
  164. }
  165. }
  166. /**
  167. * Loads and registers a database driver class.
  168. * If this succeeds, it returns true, else it returns false.
  169. */
  170. public static boolean loadDriver(String driverClassName) {
  171. try {
  172. Class.forName(driverClassName).newInstance();
  173. return true;
  174. } catch (ClassNotFoundException e) {
  175. // TODO Logging?
  176. //e.printStackTrace();
  177. return false;
  178. } catch (IllegalAccessException e) {
  179. // TODO Logging?
  180. //e.printStackTrace();
  181. // Constructor is private, OK for DriverManager contract
  182. return true;
  183. } catch (InstantiationException e) {
  184. // TODO Logging?
  185. //e.printStackTrace();
  186. return false;
  187. } catch (Throwable t) {
  188. return false;
  189. }
  190. }
  191. public static void printStackTrace(SQLException sqle) {
  192. printStackTrace(sqle, new PrintWriter(System.err));
  193. }
  194. public static void printStackTrace(SQLException sqle, PrintWriter pw) {
  195. SQLException next = sqle;
  196. while (next != null) {
  197. next.printStackTrace(pw);
  198. next = next.getNextException();
  199. if (next != null) {
  200. pw.println("Next SQLException:");
  201. }
  202. }
  203. }
  204. public static void printWarnings(Connection connection) {
  205. printWarnings(connection, new PrintWriter(System.err));
  206. }
  207. public static void printWarnings(Connection conn, PrintWriter pw) {
  208. if (conn != null) {
  209. try {
  210. printStackTrace(conn.getWarnings(), pw);
  211. } catch (SQLException sqle) {
  212. printStackTrace(sqle, pw);
  213. }
  214. }
  215. }
  216. /**
  217. * Rollback any changes made on the given connection.
  218. * @param conn The database Connection to rollback. A null value is legal.
  219. * @throws SQLException
  220. */
  221. public static void rollback(Connection conn) throws SQLException {
  222. if (conn != null) {
  223. conn.rollback();
  224. }
  225. }
  226. }