標籤:rtt localhost com tiny div orm datetime host cas
包含三部分內容
1.spring jdbc
2. spring datasource
3.spring Connection pooling
完整的項目請往百度雲端硬碟下載:
測試方法
package com.guo.dataSourceDemo;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.jdbc.datasource.DriverManagerDataSource;import com.alibaba.druid.pool.DruidDataSource;public class JunitCase {@Test//1.spring jdbcpublic void test(){//a.連接字串String url = "jdbc:mysql://localhost:3306/yun?characterEncoding=UTF8"+ "&user=root&password=centos"+ "&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false";Connection con = null;Statement stmt = null;ResultSet result = null;try {//b.註冊驅動Class.forName("com.mysql.jdbc.Driver");//c.擷取串連con = DriverManager.getConnection(url);//d.擷取控制代碼stmt = con.createStatement();String query = "select * from eac_core_biz";//e.執行語句result = stmt.executeQuery(query);//f.擷取結果while(result.next()){String id = result.getString(1);String biz = result.getString(2);String name = result.getString(3);System.out.println("id:"+id+",biz:"+biz+",name:"+name);}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}finally{//g.關閉串連closeResultSet(result);closeStatement(stmt);closeConnection(con);}}@Test //2. spring datasourcepublic void test1(){ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");DriverManagerDataSource ds = (DriverManagerDataSource) context.getBean("dataSource");Connection con = null;Statement stmt = null;ResultSet result = null;try {con = ds.getConnection();stmt = con.createStatement();String query = "select * from eac_core_biz";result = stmt.executeQuery(query);while(result.next()){String id = result.getString(1);String biz = result.getString(2);String name = result.getString(3);System.out.println("id:"+id+",biz:"+biz+",name:"+name);}} catch (SQLException e) {e.printStackTrace();}finally{closeResultSet(result);closeStatement(stmt);closeConnection(con);}}@Test //3.spring Connection poolingpublic void test2(){ApplicationContext context = new ClassPathXmlApplicationContext("spring-pool.xml");DruidDataSource ds = (DruidDataSource) context.getBean("dataSource");Connection con = null;Statement stmt = null;ResultSet result = null;try {con = ds.getConnection();stmt = con.createStatement();String query = "select * from eac_core_biz";result = stmt.executeQuery(query);while(result.next()){String id = result.getString(1);String biz = result.getString(2);String name = result.getString(3);System.out.println("id:"+id+",biz:"+biz+",name:"+name);}} catch (SQLException e) {e.printStackTrace();}finally{closeResultSet(result);closeStatement(stmt);closeConnection(con);}}public void closeResultSet(ResultSet result){if(result!=null){try {result.close();} catch (SQLException e) {e.printStackTrace();}}}public void closeStatement(Statement stmt){if(stmt!=null){try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}}public void closeConnection(Connection con){if(con!=null){try {con.close();} catch (SQLException e) {e.printStackTrace();}}}}
application.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/yun jdbc.username=rootjdbc.password=centos
context.xml
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd "> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/yun?characterEncoding=UTF8" /> <property name="username" value="root" /> <property name="password" value="centos" /> </bean> </beans>
spring-pool.xml
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd "> <bean id="propertyConfigure" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>application.properties</value> </list> </property> </bean> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="1" /> <property name="minIdle" value="1" /> <property name="maxActive" value="10" /> <!-- 配置擷取串連等待逾時的時間 --> <property name="maxWait" value="10000" /> <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑串連,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一個串連在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="testWhileIdle" value="true" /> <!-- 這裡建議配置為TRUE,防止取到的串連不可用 --> <property name="testOnBorrow" value="true" /> <property name="testOnReturn" value="false" /> <!-- 開啟PSCache,並且指定每個串連上PSCache的大小 --> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <!-- 這裡配置提交方式,預設就是TRUE,可以不用配置 --> <property name="defaultAutoCommit" value="true" /> <!-- 驗證串連有效與否的SQL,不同的資料配置不同 --> <property name="validationQuery" value="select 1 " /> <property name="filters" value="stat" /> <property name="proxyFilters"> <list> <ref bean="logFilter" /> </list> </property> </bean> <bean id="logFilter" class="com.alibaba.druid.filter.logging.Slf4jLogFilter"> <property name="statementExecutableSqlLogEnable" value="false" /> </bean></beans>
spring資料來源