DBCP

 使用DBCP必须用的三个包:

commons-dbcp-1.2.1.jar, commons-pool-1.2.jar, commons-collections-3.1.jar。
Java API: BasicDataSourceFactory.createDataSource(properties);
 
----------------------------------------------------------------------------------------

dbcpconfig.properties

#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc
username=root
password=******

#<!-- 初始化连接 -->
initialSize=10

#最大连接数量
maxActive=50

#<!-- 最大空闲连接 -->
maxIdle=20

#<!-- 最小空闲连接 -->
minIdle=5

#<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
maxWait=60000


#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;]
#注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。
connectionProperties=useUnicode=true;characterEncoding=gbk

#指定由连接池所创建的连接的自动提交(auto-commit)状态。
defaultAutoCommit=true

#driver default 指定由连接池所创建的连接的只读(read-only)状态。
#如果没有设置该值,则“setReadOnly”方法将不被调用。(某些驱动并不支持只读模式,如:Informix)
defaultReadOnly=

#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。
#可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation=READ_UNCOMMITTED

---------------------------------------------------------------------------------------

public final class JdbcUtils {
// private static String url = "jdbc:mysql://localhost:3306/jdbc";
// private static String user = "root";
// private static String password = "064417";
// private static MyDataSource2 myDataSource = null;
 private static DataSource myDataSource = null;
 private JdbcUtils() {}
 static {
  try {
   // 注册驱动
   Class.forName("com.mysql.jdbc.Driver");// 推荐
   // DriverManager.registerDriver(new com.mysql.jdbc.Driver());//2
   // System.setProperty("jdbc.drivers",
   // "com.mysql.jdbc.Driver[:...:...]");//注册方式3可以注册多个驱动用“:”分隔。
//   myDataSource = new MyDataSource2();
   Properties prop=new Properties();
//   prop.setProperty("driverClassName", "com.mysql.jdbc.Driver");
//   prop.setProperty("user", "user");
//   读取配置文件
   InputStream is=JdbcUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
   prop.load(is);
   myDataSource=BasicDataSourceFactory.createDataSource(prop);
  } catch (Exception e) {
   throw new ExceptionInInitializerError(e);
  }
 }
 public static Connection getConnection() throws SQLException {
  // return DriverManager.getConnection(url, user, password);
  return myDataSource.getConnection();
 }
 public static void free(ResultSet rs, Statement st, Connection conn) {
  try {
   if (rs != null)
    rs.close();
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   try {
    if (st != null)
     st.close();
   } catch (SQLException e) {
    e.printStackTrace();
   } finally {
    try {
     if (conn != null)
       conn.close();
//      myDataSource.free(conn);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  }
 }

---------------------------------------------------------------

public class Base {
 public static void main(String[] args) throws SQLException {
//  template();
  for(int i=0;i<10;i++){
   Connection conn=JdbcUtils.getConnection();
   System.out.println(conn.getClass().getName());
   JdbcUtils.free(null, null, conn);
  }
 }
 static void template(){
  Connection conn=null;
  Statement st=null;
  ResultSet rs=null;
  try {
//   建立连接
//   conn=JdbcUtils.getConnection();
   conn=JdbcUtilsSing.getInstance().getConnection();//使用单例
//   创建语句
   st=conn.createStatement();
//   执行语句
   rs=st.executeQuery("select * from user");
//   处理结果
   while(rs.next()){
//    参数中的1,2,3,4表示列的索引
    System.out.println(rs.getObject(1)+"\t"+rs.getObject(2)
      +"\t"+rs.getObject(3)+"\t"+rs.getObject(4));
   }
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
//   释放资源
   JdbcUtils.free(rs, st, conn);
  }
 }
}

原文地址:https://www.cnblogs.com/mingforyou/p/2290824.html