1 61 62 package org.apache.commons.dbutils; 63 64 import java.io.PrintWriter ; 65 import java.sql.Connection ; 66 import java.sql.ResultSet ; 67 import java.sql.SQLException ; 68 import java.sql.Statement ; 69 70 78 public final class DbUtils { 79 80 83 public static void close(Connection conn) throws SQLException { 84 if (conn != null) { 85 conn.close(); 86 } 87 } 88 89 92 public static void close(ResultSet rs) throws SQLException { 93 if (rs != null) { 94 rs.close(); 95 } 96 } 97 98 101 public static void close(Statement stmt) throws SQLException { 102 if (stmt != null) { 103 stmt.close(); 104 } 105 } 106 107 111 public static void closeQuietly(Connection conn) { 112 try { 113 close(conn); 114 } catch (SQLException sqle) { 115 } 117 } 118 119 124 public static void closeQuietly( 125 Connection conn, 126 Statement stmt, 127 ResultSet rs) { 128 129 closeQuietly(rs); 130 closeQuietly(stmt); 131 closeQuietly(conn); 132 } 133 134 138 public static void closeQuietly(ResultSet rs) { 139 try { 140 close(rs); 141 } catch (SQLException sqle) { 142 } 144 } 145 146 150 public static void closeQuietly(Statement stmt) { 151 try { 152 close(stmt); 153 } catch (SQLException sqle) { 154 } 156 } 157 158 161 public static void commitAndClose(Connection conn) throws SQLException { 162 if (conn != null) { 163 conn.commit(); 164 conn.close(); 165 } 166 } 167 168 172 public static void commitAndCloseQuietly(Connection conn) { 173 try { 174 commitAndClose(conn); 175 } catch (SQLException sqle) { 176 } 178 } 179 180 184 public static boolean loadDriver(String driverClassName) { 185 try { 186 Class.forName(driverClassName).newInstance(); 187 return true; 188 189 } catch (ClassNotFoundException e) { 190 return false; 193 194 } catch (IllegalAccessException e) { 195 198 return true; 200 201 } catch (InstantiationException e) { 202 return false; 205 206 } catch (Throwable t) { 207 return false; 208 } 209 } 210 211 public static void printStackTrace(SQLException sqle) { 212 printStackTrace(sqle, new PrintWriter (System.err)); 213 } 214 215 public static void printStackTrace(SQLException sqle, PrintWriter pw) { 216 217 SQLException 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 connection) { 228 printWarnings(connection, new PrintWriter (System.err)); 229 } 230 231 public static void printWarnings(Connection conn, PrintWriter pw) { 232 if (conn != null) { 233 try { 234 printStackTrace(conn.getWarnings(), pw); 235 } catch (SQLException sqle) { 236 printStackTrace(sqle, pw); 237 } 238 } 239 } 240 241 246 public static void rollback(Connection conn) throws SQLException { 247 if (conn != null) { 248 conn.rollback(); 249 } 250 } 251 252 } 253 | Popular Tags |