JDBC:DAO(一)JDBCTools工具类

/**
 * JDBC 的工具类
 *
 * 其中包含: 获取数据库连接, 关闭数据库资源,事务处理的等方法.
 */
public class JDBCTools {
    //处理数据库事务的
    //提交事务
    public static void commit(Connection connection){
        if(connection != null){
            try {
                connection.commit();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    //回滚事务
    public static void rollback(Connection connection){
        if(connection != null){
            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    //开始事务
    public static void beginTx(Connection connection){
        if(connection != null){
            try {
                connection.setAutoCommit(false);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    //////////////////////////////////////////////////////////////使用数据库连接池技术获取connection//////////////////////////////////////////////////////////////////////////////////
    private static DataSource dataSource = null;

    //数据库连接池应只被初始化一次.
    static{
        dataSource = new ComboPooledDataSource("helloc3p0");
    }
    
    public static Connection getConnection() throws Exception {
        return dataSource.getConnection();

    }


    public static Connection createConnection() throws Exception {

/////////////////////////////////////////////////////////////下面这些属性可以设置成静态的,只需要初始化一次///////////////////////////////////////////////////////////////////

        Properties properties = new Properties();
        InputStream inStream = JDBCTools.class.getClassLoader()
                .getResourceAsStream("jdbc.properties");
        properties.load(inStream);

        // 1. 准备获取连接的 4 个字符串: user, password, jdbcUrl, driverClass
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String jdbcUrl = properties.getProperty("jdbcUrl");
        String driverClass = properties.getProperty("driverClass");


        // 2. 加载驱动: Class.forName(driverClass)
        Class.forName(driverClass);

        // 3. 调用
        // DriverManager.getConnection(jdbcUrl, user, password)
        // 获取数据库连接
        Connection connection = DriverManager.getConnection(jdbcUrl, user,
                password);
        return connection;
    }

    public static void releaseDB(ResultSet resultSet, Statement statement,
            Connection connection) {


        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (connection != null) {

            try {

                //数据库连接池的 Connection 对象进行 close 时
                //并不是真的进行关闭, 而是把该数据库连接会归还到数据库连接池中.

                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}



这个工具类为上层DAO提供connection,事务等//////////////

你可能感兴趣的:(JDBC:DAO(一)JDBCTools工具类)