1. DbUtils类简介
DbUtils是一个简单的一个数据库帮助类
主要包含了:
- Connection,Statement,ResultSet资源的关闭,
- Connection提交,回滚
- 异常/警告的输出
- 加载驱动
- 静态内部类DriverProxy
2. 方法
2.1 构造函数
public DbUtils() {
// do nothing
}
2.2 close方法
在每个资源close之前都会判断是否已经关闭,防止因为重复关闭出错
public static void close(Connection conn) throws SQLException {
if (conn != null) {
conn.close();
}
}
public static void close(ResultSet rs) throws SQLException;
public static void close(Statement stmt) throws SQLException;
2.3 closeQuickly方法
和close方法类似,只是在内部调用close方法,且在方法内部捕获了异常,提供一次关闭多个资源的方法
public static void closeQuietly(Connection conn, Statement stmt, ResultSet rs) {
try {
closeQuietly(rs);
} finally {
try {
closeQuietly(stmt);
} finally {
closeQuietly(conn);
}
}
}
public static void closeQuietly(Connection conn);
public static void closeQuietly(ResultSet rs);
public static void closeQuietly(Statement stmt);
2.4 提交/回滚事务
提交后会关闭资源
public static void commitAndClose(Connection conn) throws SQLException {
if (conn != null) {
try {
conn.commit();
} finally {
conn.close();
}
}
}
public static void commitAndCloseQuietly(Connection conn) {
try {
commitAndClose(conn);
} catch (SQLException e) { // NOPMD
// quiet
}
}
public static void rollback(Connection conn) throws SQLException {
if (conn != null) {
conn.rollback();
}
}
public static void rollbackAndClose(Connection conn) throws SQLException;
public static void rollbackAndCloseQuietly(Connection conn);
2.5 加载驱动
public static boolean loadDriver(ClassLoader classLoader, String driverClassName) {
try {
Class<?> loadedClass = classLoader.loadClass(driverClassName);
if (!Driver.class.isAssignableFrom(loadedClass)) {
return false;
}
@SuppressWarnings("unchecked")
Class<Driver> driverClass = (Class<Driver>) loadedClass;
//获取驱动的构造函数
Constructor<Driver> driverConstructor = driverClass.getConstructor();
//构造函数是私有的者设置可访问性为true
boolean isConstructorAccessible = driverConstructor.isAccessible();
if (!isConstructorAccessible) {
driverConstructor.setAccessible(true);
}
//实例化驱动
try {
Driver driver = driverConstructor.newInstance();
//注册驱动
registerDriver(new DriverProxy(driver));
} finally {
driverConstructor.setAccessible(isConstructorAccessible);
}
return true;
} catch (RuntimeException e) {
return false;
} catch (Exception e) {
return false;
}
}
public static boolean loadDriver(String driverClassName);
2.6 输出异常
public static void printStackTrace(SQLException e, PrintWriter pw) {
SQLException next = e;
while (next != null) {
next.printStackTrace(pw);
next = next.getNextException();
if (next != null) {
pw.println("Next SQLException:");
}
}
}
public static void printStackTrace(SQLException e);
public static void printWarnings(Connection conn, PrintWriter pw) {
if (conn != null) {
try {
printStackTrace(conn.getWarnings(), pw);
} catch (SQLException e) {
printStackTrace(e, pw);
}
}
}
public static void printWarnings(Connection conn);
2.7 静态内部类DriverProxy
这个除了自定义一个字节的方法外,其余方法都是在显示接口方法的基础上直接调用接口方法,不是很懂这个类的作用
private static final class DriverProxy implements Driver {
private boolean parentLoggerSupported = true;
private final Driver adapted;
public DriverProxy(Driver adapted) {
this.adapted = adapted;
}
@Override
public boolean acceptsURL(String url) throws SQLException {
return adapted.acceptsURL(url);
}
@Override
public Connection connect(String url, Properties info) throws SQLException {
return adapted.connect(url, info);
}
@Override
public int getMajorVersion() {
return adapted.getMajorVersion();
}
@Override
public int getMinorVersion() {
return adapted.getMinorVersion();
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
return adapted.getPropertyInfo(url, info);
}
@Override
public boolean jdbcCompliant() {
return adapted.jdbcCompliant();
}
/**
* Java 1.7 method.
*/
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
if (parentLoggerSupported) {
try {
Method method = adapted.getClass().getMethod("getParentLogger", new Class[0]);
return (Logger)method.invoke(adapted, new Object[0]);
} catch (NoSuchMethodException e) {
parentLoggerSupported = false;
throw new SQLFeatureNotSupportedException(e);
} catch (IllegalAccessException e) {
parentLoggerSupported = false;
throw new SQLFeatureNotSupportedException(e);
} catch (InvocationTargetException e) {
parentLoggerSupported = false;
throw new SQLFeatureNotSupportedException(e);
}
}
throw new SQLFeatureNotSupportedException();
}
}