介绍
本篇文章给大家分享的是有关怎么在春天中通过c3p0配置豆连接数据库,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
春配置豆连接数据库两种方法:
(1)直接在。xml文件内部配置:
& lt; ? xml version=?.0“,编码=癠TF-8" ?比; http://www.springframework.org/schema/beans" & lt; beans  xmlns=?; ,xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance" ,xmlns:上下文=癶ttp://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"的在 , , ,& lt; !——,配置链接数据库,——比; ,& lt; bean id=癲ataSource",类=癱om.mchange.v2.c3p0.ComboPooledDataSource"比; ,& lt; property name=皍ser",价值=https://www.yisu.com/zixun/"根"> 属性> <属性名="密码" value=" wangjian "> 属性> <属性名=" driverClass " value=" com.mysql.jdbc.Driver "> 属性> <属性名=" jdbcUrl " value=" jdbc: mysql://localhost: 3306/测试? useUnicode=true& characterEncoding=utf-8& useSSL=false "> 属性> 豆> 豆>
(2)将参数放在外部文件中,然后再读入配置文件:
& lt; ? xml version=?.0“,编码=癠TF-8" ?比; http://www.springframework.org/schema/beans" & lt; beans  xmlns=?; ,xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance" ,xmlns:上下文=癶ttp://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"的在 , ,& lt; !——,导入属性文件,——比; , ,& lt;上下文:property-placeholder 位置=袄嗦肪?db.properties",/比; ,& lt; !——,使用外部化属性文件的属性,——比; ,& lt; bean id=癲ataSource",类=癱om.mchange.v2.c3p0.ComboPooledDataSource"比; ,& lt; property name=皍ser", https://www.yisu.com/zixun/value=" $ {user} "> 属性> <属性名="密码" value=" ${密码}"> 属性> <属性名=" driverClass " value=" $ {driverClass} "> 属性> <属性名=" jdbcUrl " value=" $ {jdbcUrl} "> 属性> 豆> 豆>
外部的配置文件:
用户=根 密码=wangjian driverclass=com.mysql.jdbc.Driver jdbcurl=jdbc: mysql://localhost: 3306/测试? useUnicode=true&音箱;characterEncoding=utf-8&音箱;useSSL=false
测试函数:
package com.primary.spring.beans.properties; , 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 =,(数据源),ctx.getBean (“dataSource"); ,System.out.println (dataSource.getConnection ()); , ,} }
测试结果:
以上就是怎么在春天中通过c3p0配置豆连接数据库,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。