一般常用的连接池是DBCP和C3P0.


package cn.itcast.spring3.demo1;

import java.sql.DriverManager;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.jta.SpringJtaSynchronizationAdapter;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringTest1 { @Autowired
@Qualifier("jdbcTemplate")
private JdbcTemplate jdbcTemplate;//注入Jdbc模板
@Test
public void demo2(){
jdbcTemplate.execute("create table user (id int primary key auto_increment,name varchar(20))");
} @Test
public void demo1(){
// 创建连接池:
DriverManagerDataSource dataSource = new DriverManagerDataSource();//Spring自带的连接池
// 设置参数:
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///spring3_day02");
dataSource.setUsername("root");
dataSource.setPassword(""); //使用JDBC的模板:
//JdbcTemplate jdbcTemplate = new JdbcTemplate();
//jdbcTemplate.setDataSource(dataSource);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("create table user (id int primary key auto_increment,name varchar(20))");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引入beans的头 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置Spring默认的连接池 -->
<!-- 这个类由Spring来帮我们创建,它默认情况下只创建一次,因为是单例的. -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///spring3_day02"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property> </bean>
<!-- 配置DBCP连接池 -->
<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///spring3_day02"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property> </bean> <!-- 定义jdbctemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource1"></property><!-- 把上面定义好的连接池注入进来了 -->
</bean>
</beans>

最新文章

  1. ASP.NET MVC中给所有的cshtml页面引用命名空间
  2. HTML5,jQuery,ajax基础面试
  3. openstack cloudinit 遇坑记
  4. configure new linux
  5. pyenv的安装和使用
  6. android开发环境与工具使用相关备忘录
  7. 笑谈ArcToolbox (1) ArcToolbox 的发展方向
  8. webapi下的web请求
  9. JAVA之旅(二十二)——Map概述,子类对象特点,共性方法,keySet,entrySet,Map小练习
  10. Java 多线程 sleep方法与wait方法的区别
  11. windows下Graphviz安装及入门教程
  12. C++获取本机IP地址信息
  13. WPF设置控件获取键盘焦点时的样式FocusVisualStyle
  14. Vue2.0 v-for 中 :key 的作用
  15. JavaScript总结(七)
  16. html template &amp; iframe
  17. jQueryEasyUI创建菜单主页
  18. OpenMP 简单的规约
  19. CPU缓存刷新的误解
  20. Selenium模拟浏览器初识

热门文章

  1. 《机器学习及实践--从零开始通往Kaggle竞赛之路》
  2. linux命令行操作mysql数据库明细
  3. CF 549B Looksery Party
  4. tyvj 1423 GF和猫咪的玩具
  5. shell总结:读取文件、参数、if、分割字符串、数组长度、空文件、变量赋值、多进程、按行切割文件、查看线程
  6. jeecg流程梳理学习
  7. MySQL下载安装详情教程(Windows)
  8. idea中隐藏.idea文件夹和.iml文件
  9. Java基础-注解
  10. mysql连接出现Unknown system variable &#39;tx_isolation&#39;异常