一、前言
基于commons-dbutils(1.3)包中的org.apache.commons.dbutils.DbUtils数据工具类,实现数据库连接有效关闭closeQuietly、记录集的关闭ResultSet、Statement语句的关闭、驱动类loadDriver加载、连接的回滚关闭rollbackAndCloseQuietly等操作。
二、源码说明
package org.apache.commons.dbutils;@b@@b@import java.io.PrintWriter;@b@import java.sql.Connection;@b@import java.sql.ResultSet;@b@import java.sql.SQLException;@b@import java.sql.Statement;@b@@b@public final class DbUtils@b@{@b@ public static void close(Connection conn)@b@ throws SQLException@b@ {@b@ if (conn != null)@b@ conn.close();@b@ }@b@@b@ public static void close(ResultSet rs)@b@ throws SQLException@b@ {@b@ if (rs != null)@b@ rs.close();@b@ }@b@@b@ public static void close(Statement stmt)@b@ throws SQLException@b@ {@b@ if (stmt != null)@b@ stmt.close();@b@ }@b@@b@ public static void closeQuietly(Connection conn)@b@ {@b@ try@b@ {@b@ close(conn);@b@ }@b@ catch (SQLException e)@b@ {@b@ }@b@ }@b@@b@ public static void closeQuietly(Connection conn, Statement stmt, ResultSet rs)@b@ {@b@ try@b@ {@b@ closeQuietly(rs);@b@ } finally {@b@ try {@b@ closeQuietly(stmt);@b@ } finally {@b@ closeQuietly(conn);@b@ }@b@ }@b@ }@b@@b@ public static void closeQuietly(ResultSet rs)@b@ {@b@ try@b@ {@b@ close(rs);@b@ }@b@ catch (SQLException e)@b@ {@b@ }@b@ }@b@@b@ public static void closeQuietly(Statement stmt)@b@ {@b@ try@b@ {@b@ close(stmt);@b@ }@b@ catch (SQLException e)@b@ {@b@ }@b@ }@b@@b@ public static void commitAndClose(Connection conn)@b@ throws SQLException@b@ {@b@ if (conn != null)@b@ try {@b@ conn.commit();@b@ } finally {@b@ conn.close();@b@ }@b@ }@b@@b@ public static void commitAndCloseQuietly(Connection conn)@b@ {@b@ try@b@ {@b@ commitAndClose(conn);@b@ }@b@ catch (SQLException e)@b@ {@b@ }@b@ }@b@@b@ public static boolean loadDriver(String driverClassName)@b@ {@b@ try@b@ {@b@ Class.forName(driverClassName).newInstance();@b@ return true;@b@ }@b@ catch (ClassNotFoundException e) {@b@ return false;@b@ }@b@ catch (IllegalAccessException e)@b@ {@b@ return true;@b@ }@b@ catch (InstantiationException e) {@b@ return false;@b@ } catch (Throwable e) {@b@ }@b@ return false;@b@ }@b@@b@ public static void printStackTrace(SQLException e)@b@ {@b@ printStackTrace(e, new PrintWriter(System.err));@b@ }@b@@b@ public static void printStackTrace(SQLException e, PrintWriter pw)@b@ {@b@ SQLException next = e;@b@ while (true) { do { if (next == null) return;@b@ next.printStackTrace(pw);@b@ next = next.getNextException(); }@b@ while (next == null);@b@ pw.println("Next SQLException:");@b@ }@b@ }@b@@b@ public static void printWarnings(Connection conn)@b@ {@b@ printWarnings(conn, new PrintWriter(System.err));@b@ }@b@@b@ public static void printWarnings(Connection conn, PrintWriter pw)@b@ {@b@ if (conn != null)@b@ try {@b@ printStackTrace(conn.getWarnings(), pw);@b@ } catch (SQLException e) {@b@ printStackTrace(e, pw);@b@ }@b@ }@b@@b@ public static void rollback(Connection conn)@b@ throws SQLException@b@ {@b@ if (conn != null)@b@ conn.rollback();@b@ }@b@@b@ public static void rollbackAndClose(Connection conn)@b@ throws SQLException@b@ {@b@ if (conn != null)@b@ try {@b@ conn.rollback();@b@ } finally {@b@ conn.close();@b@ }@b@ }@b@@b@ public static void rollbackAndCloseQuietly(Connection conn)@b@ {@b@ try@b@ {@b@ rollbackAndClose(conn);@b@ }@b@ catch (SQLException e)@b@ {@b@ }@b@ }@b@}