java bonecp mysql_Java中的BoneCP数据库连接池

packagecom.bonecp;importjava.sql.Connection;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importcom.jolbox.bonecp.BoneCP;importcom.jolbox.bonecp.BoneCPConfig;/***@authorsxyx2008

**/publicclassExampleJDBC {publicstaticvoidmain(String[] args) {

BoneCP connectionPool=null;

Connection connection=null;try{//load the database driver (make sure this is in your classpath!)Class.forName("oracle.jdbc.driver.OracleDriver");

}catch(Exception e) {

e.printStackTrace();return;

}try{//setup the connection poolBoneCPConfig config=null;try{

config=newBoneCPConfig("bonecp-config.xml");

}catch(Exception e) {

e.printStackTrace();

}/*config.setJdbcUrl("jdbc:oracle:thin:@127.0.0.1:1521:orcl"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb

config.setUsername("scott");

config.setPassword("tiger");

//设置每60秒检查数据库中的空闲连接数

config.setIdleConnectionTestPeriod(60);

//设置连接空闲时间

config.setIdleMaxAge(240);

//设置每个分区中的最大连接数 30

config.setMaxConnectionsPerPartition(30);

//设置每个分区中的最小连接数 10

config.setMinConnectionsPerPartition(10);

//当连接池中的连接耗尽的时候 BoneCP一次同时获取的连接数

config.setAcquireIncrement(5);

//连接释放处理

config.setReleaseHelperThreads(3);

//设置分区  分区数为3

config.setPartitionCount(3);*///设置配置参数connectionPool=newBoneCP(config);//setup the connection poollongstartTime=System.currentTimeMillis();//创建100个连接for(inti=0; i<100; i++) {

connection=connectionPool.getConnection();//fetch a connection}longendtTime=System.currentTimeMillis();

System.out.println("-------->total seconds :"+(endtTime-startTime));if(connection!=null){

System.out.println("Connection successful!");

Statement stmt=connection.createStatement();

ResultSet rs=stmt.executeQuery("select * from emp");//do something with the connection.while(rs.next()){

System.out.println(rs.getString(1));//should print out "1"'System.out.println(rs.getString(2));//should print out "1"'}

}

connectionPool.shutdown();//shutdown connection pool.}catch(SQLException e) {

e.printStackTrace();

}finally{if(connection!=null) {try{

connection.close();

}catch(SQLException e) {

e.printStackTrace();

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值