欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入 2.C3P0 在Hibernate和Spring中默认支持该数据库连接池 需要引入:c3p0-0.9.1.2.jar包,如果报错再引入mchange-commons-0.2.jar 1. 在类路径下编写一个c3p0-config.xml文件 c3p0-config !-- default-co
欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入
2.C3P0
在Hibernate和Spring中默认支持该数据库连接池
需要引入:c3p0-0.9.1.2.jar包,如果报错再引入mchange-commons-0.2.jar
1. 在类路径下编写一个c3p0-config.xml文件
2.获取默认的配置:
public static void getConnection1() throws Exception {
ComboPooledDataSource source = new ComboPooledDataSource();
Connection conn = source.getConnection();
String sql = "insert into users (name,address) values (?,?)";
PreparedStatement state = conn.prepareStatement(sql);
state.setString(1, "c3p0");
state.setString(2, "c3p0");
state.executeUpdate();
source.close();
System.out.println("OK");
}
3.获取指定名的配置:
public static void getConnection2() throws Exception {
ComboPooledDataSource source = new ComboPooledDataSource("mysql");
Connection conn = source.getConnection();
String sql = "insert into users (name,address) values (?,?)";
PreparedStatement state = conn.prepareStatement(sql);
state.setString(1, "c3p02");
state.setString(2, "c3p02");
state.executeUpdate();
source.close();
System.out.println("OK");
}
总结:连接池技术可以快速的获取数据库连接的重量级资源但是操作数据库依旧比较繁琐……
[1] [2]