Home  >  Article  >  Database  >  常见的数据库连接池

常见的数据库连接池

WBOY
WBOYOriginal
2016-06-07 15:24:201478browse

欢迎进入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文件

  

  

  

  com.mysql.jdbc.Driver

  jdbc:mysql:///dbutils

  root

  root

  5

  6

  5

  10

  

  

  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] 

常见的数据库连接池

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn