最后讲的是BoneCP,到目前为止老一代的四种连接池(c3p0,DBCP,proxool,BoneCP)就到这里了
把我这几篇博客都看了的小伙伴或许会发现,这四种连接池的配置极其的相似,套路完全一样,总的来说连接池的操作还是比较简单的,下面我把BoneCP说一下
BoneCP网上的资料真的是少之又少,毕竟这个很少使用
1.导入jar包,BoneCP要的jar包比较的多
bonecp-provider-0.7.1.jar guava-r07.jar log4j-1.2.17.jar slf4j.api-1.6.1.jar slf4j-log4j12-1.6.1.jar
2.配置BoneCP文件,bonecp-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<bean id="mainDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1/usta" />
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="idleConnectionTestPeriod" value="60"/>
<property name="idleMaxAge" value="240"/>
<property name="maxConnectionsPerPartition" value="30"/>
<property name="minConnectionsPerPartition" value="10"/>
<property name="partitionCount" value="3"/>
<property name="acquireIncrement" value="5"/>
<property name="statementsCacheSize" value="100"/>
<property name="releaseHelperThreads" value="3"/>
</bean>
3.写一个BoneCP工具类
package com.xx.train.bdqn.jdbc.utils;
//BoneCP工具类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class BONECPUtils {
public static Connection getConnection() {
Connection con = null;
try {
Class.forName("com.jolbox.bonecp.BoneCPDataSource");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/usta", "root", "root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
}
4.写一个测试类
package com.xx.train.bdqn.jdbc.utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class AddBONECP {
public void addUser() throws Exception {
Connection con = null;
PreparedStatement pstmt = null;
con = BONECPUtils.getConnection();
String sql = "insert into user value(null,?,?)";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, "superman");
pstmt.setString(2, "bathman");
int n = pstmt.executeUpdate();
if(n>0) {
System.out.println("更新成功");
}else {
System.out.println("添加失败");
}
}
}
5.调用测试类
package com.xx.train.bdqn.jdbc.test;
import com.xx.train.bdqn.jdbc.utils.AddBONECP;
public class TestBONECP {
public static void main(String[] args) throws Exception {
AddBONECP a = new AddBONECP();
a.addUser();
}
}