当前位置: 移动技术网 > IT编程>开发语言>Java > Spring使用DriverManagerDataSource和C3P0分别配置MySql6.0.6数据源

Spring使用DriverManagerDataSource和C3P0分别配置MySql6.0.6数据源

2018年09月13日  | 移动技术网IT编程  | 我要评论

首先,看一下项目路径

先说spring配置文件吧,这个比较重要

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
 9         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
10         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
11 
12     <!-- 导入属性文件 -->
13     <context:property-placeholder location="classpath:properties/jdbc.properties"/>
14 
15     <bean id="datasourcespring" class="org.springframework.jdbc.datasource.drivermanagerdatasource">
16         <property name="username" value="${jdbc.user}"></property>
17         <property name="password" value="${jdbc.pwd}"></property>
18         <property name="url" value="${jdbc.url}"></property>
19         <property name="driverclassname" value="${jdbc.driver}"></property>
20     </bean>
21     <bean id="datasourcec3p0" class="com.mchange.v2.c3p0.combopooleddatasource">
22         <property name="user" value="root"></property>
23         <property name="password" value="1234"></property>
24         <property name="jdbcurl" value="jdbc:mysql://127.0.0.1:3306/test?servertimezone=hongkong"></property>
25         <property name="driverclass" value="com.mysql.cj.jdbc.driver"></property>
26     </bean>
27 </beans>

bean的id使用不同的来进行区分,主要区别在于class指向不同,datasourcespring 并没有连接池作用,另一个使用了c3p0进行配置

properties文件

1 jdbc.user=root
2 jdbc.pwd=1234
3 jdbc.url=jdbc:mysql://127.0.0.1:3306/test?servertimezone=hongkong
4 jdbc.driver=com.mysql.cj.jdbc.driver

main方法文件

 1 package com.tse.test;
 2 
 3 import java.sql.sqlexception;
 4 
 5 import javax.sql.datasource;
 6 
 7 import org.springframework.context.applicationcontext;
 8 import org.springframework.context.support.classpathxmlapplicationcontext;
 9 
10 public class main {
11     public static void main(string[] args) throws sqlexception {
12         applicationcontext acontext = new classpathxmlapplicationcontext("com/tse/spring/applicationcontext_jdbc.xml");
13         datasource datasources = (datasource) acontext.getbean("datasourcespring");
14         datasource datasources1 = (datasource) acontext.getbean("datasourcec3p0");
15         system.out.println(datasources.getconnection());
16         system.out.println(datasources1.getconnection());
17     }
18 }

执行结果

九月 13, 2018 12:04:35 下午 org.springframework.context.support.classpathxmlapplicationcontext preparerefresh
信息: refreshing org.springframework.context.support.classpathxmlapplicationcontext@1a6c5a9e: startup date [thu sep 13 12:04:35 cst 2018]; root of context hierarchy
九月 13, 2018 12:04:35 下午 org.springframework.beans.factory.xml.xmlbeandefinitionreader loadbeandefinitions
信息: loading xml bean definitions from class path resource [com/tse/spring/applicationcontext_jdbc.xml]
九月 13, 2018 12:04:35 下午 org.springframework.jdbc.datasource.drivermanagerdatasource setdriverclassname
信息: loaded jdbc driver: com.mysql.cj.jdbc.driver
九月 13, 2018 12:04:35 下午 com.mchange.v2.log.mlog <clinit>
信息: mlog clients using java 1.4+ standard logging.
九月 13, 2018 12:04:35 下午 com.mchange.v2.c3p0.c3p0registry banner
信息: initializing c3p0-0.9.1.2 [built 21-may-2007 15:04:56; debug? true; trace: 10]
thu sep 13 12:04:36 cst 2018 warn: establishing ssl connection without server's identity verification is not recommended. according to mysql 5.5.45+, 5.6.26+ and 5.7.6+ requirements ssl connection must be established by default if explicit option isn't set. for compliance with existing applications not using ssl the verifyservercertificate property is set to 'false'. you need either to explicitly disable ssl by setting usessl=false, or set usessl=true and provide truststore for server certificate verification.
com.mysql.cj.jdbc.connectionimpl@1ffe63b9
九月 13, 2018 12:04:36 下午 com.mchange.v2.c3p0.impl.abstractpoolbackeddatasource getpoolmanager
信息: initializing c3p0 pool... com.mchange.v2.c3p0.combopooleddatasource [ acquireincrement -> 3, acquireretryattempts -> 30, acquireretrydelay -> 1000, autocommitonclose -> false, automatictesttable -> null, breakafteracquirefailure -> false, checkouttimeout -> 0, connectioncustomizerclassname -> null, connectiontesterclassname -> com.mchange.v2.c3p0.impl.defaultconnectiontester, datasourcename -> 1hge13c9x1m0nuop1yft6fk|2173f6d9, debugunreturnedconnectionstacktraces -> false, description -> null, driverclass -> com.mysql.cj.jdbc.driver, factoryclasslocation -> null, forceignoreunresolvedtransactions -> false, identitytoken -> 1hge13c9x1m0nuop1yft6fk|2173f6d9, idleconnectiontestperiod -> 0, initialpoolsize -> 3, jdbcurl -> jdbc:mysql://127.0.0.1:3306/test?servertimezone=hongkong, maxadministrativetasktime -> 0, maxconnectionage -> 0, maxidletime -> 0, maxidletimeexcessconnections -> 0, maxpoolsize -> 15, maxstatements -> 0, maxstatementsperconnection -> 0, minpoolsize -> 3, numhelperthreads -> 3, numthreadsawaitingcheckoutdefaultuser -> 0, preferredtestquery -> null, properties -> {user=******, password=******}, propertycycle -> 0, testconnectiononcheckin -> false, testconnectiononcheckout -> false, unreturnedconnectiontimeout -> 0, usestraditionalreflectiveproxies -> false ]
thu sep 13 12:04:36 cst 2018 warn: establishing ssl connection without server's identity verification is not recommended. according to mysql 5.5.45+, 5.6.26+ and 5.7.6+ requirements ssl connection must be established by default if explicit option isn't set. for compliance with existing applications not using ssl the verifyservercertificate property is set to 'false'. you need either to explicitly disable ssl by setting usessl=false, or set usessl=true and provide truststore for server certificate verification.
thu sep 13 12:04:36 cst 2018 warn: establishing ssl connection without server's identity verification is not recommended. according to mysql 5.5.45+, 5.6.26+ and 5.7.6+ requirements ssl connection must be established by default if explicit option isn't set. for compliance with existing applications not using ssl the verifyservercertificate property is set to 'false'. you need either to explicitly disable ssl by setting usessl=false, or set usessl=true and provide truststore for server certificate verification.
thu sep 13 12:04:36 cst 2018 warn: establishing ssl connection without server's identity verification is not recommended. according to mysql 5.5.45+, 5.6.26+ and 5.7.6+ requirements ssl connection must be established by default if explicit option isn't set. for compliance with existing applications not using ssl the verifyservercertificate property is set to 'false'. you need either to explicitly disable ssl by setting usessl=false, or set usessl=true and provide truststore for server certificate verification.
com.mchange.v2.c3p0.impl.newproxyconnection@769f71a9

代码上传位置,请自行获取

https://download.csdn.net/download/lijian0420/10664103

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网