连接池工具类

title: 连接池工具类
date: 2015-12-22 8:26:20
categories: Utils
tags: 连接池


xl_echo编辑整理,欢迎转载,转载请声明文章来源。更多案例、资料请联系QQ:1280023003

JDBCUtils配置文件

  • 使用JDBCUtils时需要导入一个包:mysql-connector-java-5.1.37-bin.jar
package connectorJDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

public class JDBCUtils {

    public static void loadDriver(){
        //注册驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public static Connection getConnection(){
        loadDriver();
        //获得数据库链接
        Connection conn = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myxlsorry","root","password");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return conn;
    }

    //释放资源
        public static void getClose(Connection conn, Statement stmt){
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                conn = null;
            }

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

    //释放资源
    public static void getClose(Connection conn, Statement stmt, ResultSet rs){
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }

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

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


}

DBCP配置文件

  • 使用DBCP时需要导入两个包:
    • commons-pool-1.6.jar
    • commons-dbcp-1.4.jar

//配置文件名,prop.properties
/*
*配置文件内容
*driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/databaseName
username=root
password=password
*/
package connectorJDBC;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.junit.Test;

public class JDBCUtils_dbcp1 {

    @Test
    public static Connection getConnection(){
        Connection conn = null;
        Properties prop = new Properties();

        InputStream is = JDBCUtils_dbcp.class.getClassLoader().getResourceAsStream("prop.properties");
        try {
            prop.load(is);
            DataSource dataSource =  BasicDataSourceFactory.createDataSource(prop);
            conn = dataSource.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return conn;
    }

    //释放资源
    public static void getClose(Connection conn, Statement stmt){
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }

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

    //释放资源
    public static void getClose(Connection conn, Statement stmt, ResultSet rs){
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }

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

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

C3P0配置文件

  • 使用C3P0时需要导入一个包:
    • c3p0-0.9.1.2.jar
/*
c3p0配置文件,简写版


  

    com.mysql.jdbc.Driver
    jdbc:mysql://localhost:3306/databasename
    root
    password

  


*/

package connectorJDBC;

import org.junit.Test;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JDBCUtils_C3P0 {

    @Test
    public static Connection getConnection(){
        Connection conn = null;

        ComboPooledDataSource DATA_SOURCE = new ComboPooledDataSource();
        try {
            conn = DATA_SOURCE.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return conn;
    }

}

你可能感兴趣的:(连接池)