鏈接池能夠大大提升數據庫的性能和鏈接速度,將那些已鏈接的數據庫鏈接存放在一個鏈接池裏,之後別人要鏈接數據庫的時候,將不會從新創建數據庫鏈接,直接從鏈接池中取出可用的鏈接,用戶使用完畢後,會釋放鏈接從新放到鏈接池中。java
下載必要jar包mchange-commons-java-0.2.14.jar和c3p0-0.9.2.1.jarmysql
快捷鍵ctrl+alt+shift+s導入jar包spring
編寫UserDao.javasql
1 package com.jdbc.c3p0; 2 import org.springframework.jdbc.core.JdbcTemplate; 3 4 public class UserDao { 5 private JdbcTemplate jdbcTemplate; 6 public void setJdbcTemplate(JdbcTemplate jdbcTemplate){ 7 this.jdbcTemplate = jdbcTemplate; 8 } 9 10 public void add(){ 11 //建立jdbcTemplate對象 12 //JdbcTemplate jdbcTemplate = new JdbcTemplate(); 13 String sql = "insert into user values(?,?)"; 14 jdbcTemplate.update(sql,"Lilei","bbbbbb"); 15 } 16 }
編寫UserService.java數據庫
1 package com.jdbc.c3p0; 2 3 public class UserService { 4 private UserDao userDao; 5 public void setUserDao(UserDao userDao){ 6 this.userDao = userDao; 7 } 8 9 public void add(){ 10 userDao.add(); 11 } 12 }
編寫配置文件bean.xml性能
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:tx="http://www.springframework.org/schema/tx" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context.xsd 12 http://www.springframework.org/schema/tx 13 http://www.springframework.org/schema/tx/spring-tx.xsd 14 http://www.springframework.org/schema/aop 15 http://www.springframework.org/schema/aop/spring-aop.xsd "> 16 17 <!-- 配置c3p0鏈接池 --> 18 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 19 <!-- 注入dao對象 --> 20 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 21 <property name="jdbcUrl" value="jdbc:mysql:///test"></property> 22 <property name="user" value="root"></property> 23 <property name="password" value="xxxxx"></property> 24 </bean> 25 26 <bean id="userService" class="com.jdbc.c3p0.UserService"> 27 <property name="userDao" ref="userDao"></property> 28 </bean> 29 <bean id="userDao" class="com.jdbc.c3p0.UserDao"> 30 <!-- 注入jdbcTemplate對象--> 31 <property name="jdbcTemplate" ref="jdbcTemplate"></property> 32 </bean> 33 34 <!-- 建立jdbcTemplate對象 --> 35 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 36 <!-- 把dataSource傳遞到模板對象中--> 37 <property name="dataSource" ref="dataSource"></property> 38 </bean> 39 </beans>
編寫測試文件TestService.java測試
1 package com.jdbc.c3p0; 2 3 import org.junit.Test; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 public class TestService { 8 @Test 9 public void testDemo(){ 10 ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); 11 UserService userService = (UserService) context.getBean("userService"); 12 userService.add(); 13 } 14 }
運行可見添加一條記錄this