View Javadoc

1   package com.ning.metrics.goodwill.dao;
2   
3   
4   import com.ning.metrics.goodwill.store.MySQLStore;
5   import org.apache.log4j.Logger;
6   
7   import java.sql.Connection;
8   import java.sql.ResultSet;
9   import java.sql.SQLException;
10  import java.sql.Statement;
11  
12  public final class DAOUtil
13  {
14      private static Logger log = Logger.getLogger(MySQLStore.class);
15  
16      /**
17       * Quietly close the Connection
18       *
19       * @param connection The Connection to be closed quietly
20       */
21      public static void close(Connection connection)
22      {
23          if (connection != null) {
24              try {
25                  connection.close();
26              }
27              catch (SQLException e) {
28                  log.warn("Closing Connection failed: " + e.getLocalizedMessage(), e);
29              }
30          }
31      }
32  
33      /**
34       * Quietly close the Statement
35       *
36       * @param statement The Statement to be closed quietly
37       */
38      public static void close(Statement statement)
39      {
40          if (statement != null) {
41              try {
42                  statement.close();
43              }
44              catch (SQLException e) {
45                  log.warn("Closing Statement failed: " + e.getLocalizedMessage(), e);
46              }
47          }
48      }
49  
50      /**
51       * Quietly close the ResultSet.
52       *
53       * @param resultSet The ResultSet to be closed quietly
54       */
55      public static void close(ResultSet resultSet)
56      {
57          if (resultSet != null) {
58              try {
59                  resultSet.close();
60              }
61              catch (SQLException e) {
62                  log.warn("Closing ResultSet failed: " + e.getLocalizedMessage(), e);
63              }
64          }
65      }
66  
67      /**
68       * Quietly close the Connection and Statement.
69       *
70       * @param connection The Connection to be closed quietly
71       * @param statement  The Statement to be closed quietly
72       */
73      public static void close(Connection connection, Statement statement)
74      {
75          close(statement);
76          close(connection);
77      }
78  
79      /**
80       * Quietly close the Connection, Statement and ResultSet
81       *
82       * @param connection The Connection to be closed quietly
83       * @param statement  The Statement to be closed quietly
84       * @param resultSet  The ResultSet to be closed quietly
85       */
86      public static void close(Connection connection, Statement statement, ResultSet resultSet)
87      {
88          close(resultSet);
89          close(statement);
90          close(connection);
91      }
92  }