尚硅谷之JDBC

2、JDBC修改成DbUtils版

package com.atguigu.utils;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import org.apache.commons.dbutils.DbUtils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DBUtilsTool {

//创建数据源,用的是c3p0-config.xml文件中<default-config>

private static ComboPooledDataSource dataSource = new ComboPooledDataSource();

//获取数据源对象

public static ComboPooledDataSource getDataSource() {

return dataSource;

}

//获取连接

public static Connection getConnection() throws SQLException{

return dataSource.getConnection();

}

public static void closeQuietly(Connection conn){

DbUtils.closeQuietly(conn);

}

public static void closeQuietly(Statement st){

DbUtils.closeQuietly(st);

}

public static void closeQuietly(ResultSet rs){

DbUtils.closeQuietly(rs);

}

}

应用案例演示:书城项目