Coverage Report - org.apache.commons.dbutils.DbUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
DbUtils
0%
0/77
0%
0/18
2.353
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.dbutils;
 18  
 
 19  
 import java.io.PrintWriter;
 20  
 import java.sql.Connection;
 21  
 import java.sql.ResultSet;
 22  
 import java.sql.SQLException;
 23  
 import java.sql.Statement;
 24  
 
 25  
 /**
 26  
  * A collection of JDBC helper methods.  This class is thread safe.
 27  
  */
 28  0
 public final class DbUtils {
 29  
 
 30  
     /**
 31  
      * Close a <code>Connection</code>, avoid closing if null.
 32  
      *
 33  
      * @param conn Connection to close.
 34  
      * @throws SQLException if a database access error occurs
 35  
      */
 36  
     public static void close(Connection conn) throws SQLException {
 37  0
         if (conn != null) {
 38  0
             conn.close();
 39  
         }
 40  0
     }
 41  
 
 42  
     /**
 43  
      * Close a <code>ResultSet</code>, avoid closing if null.
 44  
      *
 45  
      * @param rs ResultSet to close.
 46  
      * @throws SQLException if a database access error occurs
 47  
      */
 48  
     public static void close(ResultSet rs) throws SQLException {
 49  0
         if (rs != null) {
 50  0
             rs.close();
 51  
         }
 52  0
     }
 53  
 
 54  
     /**
 55  
      * Close a <code>Statement</code>, avoid closing if null.
 56  
      *
 57  
      * @param stmt Statement to close.
 58  
      * @throws SQLException if a database access error occurs
 59  
      */
 60  
     public static void close(Statement stmt) throws SQLException {
 61  0
         if (stmt != null) {
 62  0
             stmt.close();
 63  
         }
 64  0
     }
 65  
 
 66  
     /**
 67  
      * Close a <code>Connection</code>, avoid closing if null and hide
 68  
      * any SQLExceptions that occur.
 69  
      *
 70  
      * @param conn Connection to close.
 71  
      */
 72  
     public static void closeQuietly(Connection conn) {
 73  
         try {
 74  0
             close(conn);
 75  0
         } catch (SQLException e) {
 76  
             // quiet
 77  0
         }
 78  0
     }
 79  
 
 80  
     /**
 81  
      * Close a <code>Connection</code>, <code>Statement</code> and 
 82  
      * <code>ResultSet</code>.  Avoid closing if null and hide any 
 83  
      * SQLExceptions that occur.
 84  
      *
 85  
      * @param conn Connection to close.
 86  
      * @param stmt Statement to close.
 87  
      * @param rs ResultSet to close.
 88  
      */
 89  
     public static void closeQuietly(Connection conn, Statement stmt,
 90  
             ResultSet rs) {
 91  
 
 92  
         try {
 93  0
             closeQuietly(rs);
 94  
         } finally {
 95  0
             try {
 96  0
                 closeQuietly(stmt);
 97  
             } finally {
 98  0
                 closeQuietly(conn);
 99  0
             }
 100  0
         }
 101  
 
 102  0
     }
 103  
 
 104  
     /**
 105  
      * Close a <code>ResultSet</code>, avoid closing if null and hide any
 106  
      * SQLExceptions that occur.
 107  
      *
 108  
      * @param rs ResultSet to close.
 109  
      */
 110  
     public static void closeQuietly(ResultSet rs) {
 111  
         try {
 112  0
             close(rs);
 113  0
         } catch (SQLException e) {
 114  
             // quiet
 115  0
         }
 116  0
     }
 117  
 
 118  
     /**
 119  
      * Close a <code>Statement</code>, avoid closing if null and hide
 120  
      * any SQLExceptions that occur.
 121  
      *
 122  
      * @param stmt Statement to close.
 123  
      */
 124  
     public static void closeQuietly(Statement stmt) {
 125  
         try {
 126  0
             close(stmt);
 127  0
         } catch (SQLException e) {
 128  
             // quiet
 129  0
         }
 130  0
     }
 131  
 
 132  
     /**
 133  
      * Commits a <code>Connection</code> then closes it, avoid closing if null.
 134  
      *
 135  
      * @param conn Connection to close.
 136  
      * @throws SQLException if a database access error occurs
 137  
      */
 138  
     public static void commitAndClose(Connection conn) throws SQLException {
 139  0
         if (conn != null) {
 140  
             try {
 141  0
                 conn.commit();
 142  
             } finally {
 143  0
                 conn.close();
 144  0
             }
 145  
         }
 146  0
     }
 147  
 
 148  
     /**
 149  
      * Commits a <code>Connection</code> then closes it, avoid closing if null 
 150  
      * and hide any SQLExceptions that occur.
 151  
      *
 152  
      * @param conn Connection to close.
 153  
      */
 154  
     public static void commitAndCloseQuietly(Connection conn) {
 155  
         try {
 156  0
             commitAndClose(conn);
 157  0
         } catch (SQLException e) {
 158  
             // quiet
 159  0
         }
 160  0
     }
 161  
 
 162  
     /**
 163  
      * Loads and registers a database driver class.
 164  
      * If this succeeds, it returns true, else it returns false.
 165  
      *
 166  
      * @param driverClassName of driver to load
 167  
      * @return boolean <code>true</code> if the driver was found, otherwise <code>false</code>
 168  
      */
 169  
     public static boolean loadDriver(String driverClassName) {
 170  
         try {
 171  0
             Class.forName(driverClassName).newInstance();
 172  0
             return true;
 173  
 
 174  0
         } catch (ClassNotFoundException e) {
 175  0
             return false;
 176  
 
 177  0
         } catch (IllegalAccessException e) {
 178  
             // Constructor is private, OK for DriverManager contract
 179  0
             return true;
 180  
 
 181  0
         } catch (InstantiationException e) {
 182  0
             return false;
 183  
 
 184  0
         } catch (Throwable e) {
 185  0
             return false;
 186  
         }
 187  
     }
 188  
 
 189  
     /**
 190  
      * Print the stack trace for a SQLException to STDERR.
 191  
      *
 192  
      * @param e SQLException to print stack trace of
 193  
      */
 194  
     public static void printStackTrace(SQLException e) {
 195  0
         printStackTrace(e, new PrintWriter(System.err));
 196  0
     }
 197  
 
 198  
     /**
 199  
      * Print the stack trace for a SQLException to a 
 200  
      * specified PrintWriter. 
 201  
      *
 202  
      * @param e SQLException to print stack trace of
 203  
      * @param pw PrintWriter to print to
 204  
      */
 205  
     public static void printStackTrace(SQLException e, PrintWriter pw) {
 206  
 
 207  0
         SQLException next = e;
 208  0
         while (next != null) {
 209  0
             next.printStackTrace(pw);
 210  0
             next = next.getNextException();
 211  0
             if (next != null) {
 212  0
                 pw.println("Next SQLException:");
 213  
             }
 214  
         }
 215  0
     }
 216  
 
 217  
     /**
 218  
      * Print warnings on a Connection to STDERR.
 219  
      *
 220  
      * @param conn Connection to print warnings from
 221  
      */
 222  
     public static void printWarnings(Connection conn) {
 223  0
         printWarnings(conn, new PrintWriter(System.err));
 224  0
     }
 225  
 
 226  
     /**
 227  
      * Print warnings on a Connection to a specified PrintWriter. 
 228  
      *
 229  
      * @param conn Connection to print warnings from
 230  
      * @param pw PrintWriter to print to
 231  
      */
 232  
     public static void printWarnings(Connection conn, PrintWriter pw) {
 233  0
         if (conn != null) {
 234  
             try {
 235  0
                 printStackTrace(conn.getWarnings(), pw);
 236  0
             } catch (SQLException e) {
 237  0
                 printStackTrace(e, pw);
 238  0
             }
 239  
         }
 240  0
     }
 241  
 
 242  
     /**
 243  
      * Rollback any changes made on the given connection.
 244  
      * @param conn Connection to rollback.  A null value is legal.
 245  
      * @throws SQLException if a database access error occurs
 246  
      */
 247  
     public static void rollback(Connection conn) throws SQLException {
 248  0
         if (conn != null) {
 249  0
             conn.rollback();
 250  
         }
 251  0
     }
 252  
     
 253  
     /**
 254  
      * Performs a rollback on the <code>Connection</code> then closes it, 
 255  
      * avoid closing if null.
 256  
      *
 257  
      * @param conn Connection to rollback.  A null value is legal.
 258  
      * @throws SQLException if a database access error occurs
 259  
      * @since DbUtils 1.1
 260  
      */
 261  
     public static void rollbackAndClose(Connection conn) throws SQLException {
 262  0
         if (conn != null) {
 263  
             try {
 264  0
                 conn.rollback();
 265  
             } finally {
 266  0
                 conn.close();
 267  0
             }
 268  
         }
 269  0
     }
 270  
 
 271  
     /**
 272  
      * Performs a rollback on the <code>Connection</code> then closes it, 
 273  
      * avoid closing if null and hide any SQLExceptions that occur.
 274  
      *
 275  
      * @param conn Connection to rollback.  A null value is legal.
 276  
      * @since DbUtils 1.1
 277  
      */
 278  
     public static void rollbackAndCloseQuietly(Connection conn) {
 279  
         try {
 280  0
             rollbackAndClose(conn);
 281  0
         } catch (SQLException e) {
 282  
             // quiet
 283  0
         }
 284  0
     }
 285  
 
 286  
 }