一、在Spring的基础上还要另外导入c3p0包和mysql的驱动包。

二、配置文件,

jdbc.propertices:这里只做了一些简单配置

user=root
password=123
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc\:mysql\:///test

三、spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 导入外部配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
</bean> </beans>

四、测试:

package spring.beans.properties.test;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) throws SQLException {
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans-properties.xml"); DataSource dataSource=(DataSource) ctx.getBean("dataSource");
System.out.println(dataSource.getConnection());
} }

五、输出

  数据库中有对应数据库test,并配置成功,会输出

com.mchange.v2.c3p0.impl.NewProxyConnection@28ac3dc3

最新文章

  1. c++宏定义命令
  2. 【前端积累】常用事件的js公用方法
  3. 向指定URL发送GET方法获取资源,编码问题。 Rest风格
  4. 购物车catslider简单的多商品分类滑动
  5. Pyqt QSS简单的Ui美化
  6. qemu-kvm简单使用
  7. 关于python的环境变量问题
  8. IOS AFNetworking简介及使用
  9. 目前IT行业的几个大方向
  10. Java中实例方法,实例变量,静态方法,静态变量,final方法重写的问题,覆盖
  11. C#生成Code39(extend)条形码【非条形码字体】
  12. visual studio 未将对象引用设置到对象的实例
  13. 浅析 SpringMVC 原理和配置.
  14. ubuntu安装postgresql以及pgadmin4当前最新(4.3)网页版
  15. JAVA实现网络文件下载
  16. 查看linux中tcp连接数
  17. MVC和WebForm区别
  18. Codeforces 314 E. Sereja and Squares
  19. kbmMW集成JWT
  20. Javac之glb与lub

热门文章

  1. python学习【第十一篇】网络编程
  2. hihocoder 1279(状压)
  3. jetty;tomcat;热部署
  4. EasyNVR摄像机无插件直播按需RTSP拉流播放流程详解
  5. 巨蟒python全栈开发linux之cento8
  6. Git is fundamentally a content-addressable filesystem with a VCS user interface written on top of it
  7. 我的Java开发学习之旅------>Eclipse 项目有红感叹号解决之道
  8. 找出n的阶乘末尾有几个零
  9. iOS应用生命周期
  10. DP专题(不定期更新)