Chinaunix首页 | 论坛 | 博客
  • 博客访问: 78818
  • 博文数量: 8
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 599
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-27 16:46
个人简介

哎……马上就要毕业了!求工作啊^0^

文章分类

全部博文(8)

文章存档

2013年(8)

我的朋友

分类: Java

2013-08-20 21:53:48


所有的数据库连接池实现都要指定sun定义的连接池接口-javax.sql.DataSource

1.DBCP

DBCP连接池是tomcat默认支持的连接池,在tomcat的lib目录下可看见tomcat-dbcp.jar;
如果没有的话,须执行引入commons-dbcp-1.2.2.jarcommons-pool.jar 早期版本可能还需引入commons-collection.jar
简单入门实例:

点击(此处)折叠或打开

  1. public class DBCP {
  2.     public static void getConnection() throws Exception {
  3.         // 1. 获取连接池对象

  4.         BasicDataSource source = new BasicDataSource();
  5.         // 2. 设置连接池对象

  6.         source.setDriverClassName("com.mysql.jdbc.Driver");
  7.         source.setUrl("jdbc:mysql:///dbutils");
  8.         source.setUsername("root");
  9.         source.setPassword("root");
  10.         source.setDefaultAutoCommit(true);
  11.         
  12.         source.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
  13.         source.setInitialSize(10);
  14.         source.setMaxActive(5);
  15.         source.setMaxIdle(10);
  16.         source.setMinIdle(0);
  17.         source.setMaxWait(5000);
  18.         
  19.         Connection conn = source.getConnection();
  20.         String sql = "insert into test(name) values(?)";
  21.         PreparedStatement state = conn.prepareStatement(sql);
  22.         state.setString(1, "scoield");
  23.         state.executeUpdate();
  24.         state.close();
  25.         System.out.println("插入数据成功");
  26.         
  27.     }
  28.     public static void main(String[] args) throws Exception {
  29.         getConnection();

  30.     }
  31. }


上面的代码发现设置参数的方法过多,得一个个的设置,过于繁琐!

配置文件方式代码实现:
dbcp.properties

点击(此处)折叠或打开

  1. username=root
  2. password=root
  3. url=jdbc\:mysql\:///dbutils
  4. driverClassName=com.mysql.jdbc.Driver
  5. defaultAutoCommit=true
  6. defaultTransactionIsolation=REPEATABLE_READ
  7. initialSize=10
  8. maxActive=5
  9. maxIdle=5
  10. minIdle=0
  11. maxWait=5000

java代码:

点击(此处)折叠或打开

  1. public class DBCP {
  2.     public static void getConnection2() throws Exception{
  3.         Properties prop = new Properties();
  4.         InputStream url = DBCP.class.getClassLoader().getResourceAsStream("dbcp.properties");
  5.         prop.load(url);
  6.         BasicDataSource source = (BasicDataSource) BasicDataSourceFactory.createDataSource(prop);
  7.      Connection conn = source.getConnection();
  8.      String sql = "insert into test(name) values(?)";
  9.         PreparedStatement state = conn.prepareStatement(sql);
  10.         state.setString(1, "scoield1");
  11.         state.executeUpdate();
  12.         state.close();
  13.         System.out.println("插入数据成功");
  14.     }
  15.     public static void main(String[] args) throws Exception {
  16.         getConnection2();

  17.     }
  18. }

2.C3P0
在Hibernate和Spring中默认支持该数据库连接池
需要引入:c3p0-0.9.1.2.jar包,如果报错再引入mchange-commons-0.2.jar

1. 在类路径下编写一个c3p0-config.xml文件


点击(此处)折叠或打开

  1. <c3p0-config>
  2.   <!--
  3.   <default-config>
  4.     <property name="driverClass">com.mysql.jdbc.Driver</property>
  5.     <property name="jdbcUrl">jdbc:mysql:///dbutils</property>
  6.     <property name="user">root</property>
  7.     <property name="password">root</property>
  8.     
  9.     <property name="checkoutTimeout">30000</property>
  10.     <property name="idleConnectionTestPeriod">30</property>
  11.     <property name="initialPoolSize">10</property>
  12.     <property name="maxIdleTime">30</property>
  13.     <property name="maxPoolSize">100</property>
  14.     <property name="minPoolSize">10</property>
  15.     <property name="maxStatements">200</property>
  16.   </default-config>
  17.    -->
  18.    <named-config name="mysql">
  19.         <property name="driverClass">com.mysql.jdbc.Driver</property>
  20.         <property name="jdbcUrl">jdbc:mysql:///dbutils</property>
  21.         <property name="user">root</property>
  22.         <property name="password">root</property>
  23.         
  24.         <property name="acquireIncrement">5</property>
  25.         <property name="initialPoolSize">6</property>
  26.         <property name="minPoolSize">5</property>
  27.         <property name="maxPoolSize">10</property>
  28.     </named-config>
  29. </c3p0-config>

2.获取默认的配置:

点击(此处)折叠或打开

  1. public static void getConnection1() throws Exception {
  2.         ComboPooledDataSource source = new ComboPooledDataSource();
  3.         Connection conn = source.getConnection();
  4.         String sql = "insert into users (name,address) values (?,?)";
  5.         PreparedStatement state = conn.prepareStatement(sql);
  6.         state.setString(1, "c3p0");
  7.         state.setString(2, "c3p0");
  8.         state.executeUpdate();
  9.         source.close();
  10.         System.out.println("OK");
  11.     }

3.获取指定名的配置:

点击(此处)折叠或打开

  1. public static void getConnection2() throws Exception {
  2.         ComboPooledDataSource source = new ComboPooledDataSource("mysql");
  3.         Connection conn = source.getConnection();
  4.         String sql = "insert into users (name,address) values (?,?)";
  5.         PreparedStatement state = conn.prepareStatement(sql);
  6.         state.setString(1, "c3p02");
  7.         state.setString(2, "c3p02");
  8.         state.executeUpdate();
  9.         source.close();
  10.         System.out.println("OK");
  11.     }

总结:连接池技术可以快速的获取数据库连接的重量级资源但是操作数据库依旧比较繁琐……
  解决方案---DBUtils!



阅读(2733) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~