欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring通過c3p0配置bean連接數(shù)據(jù)庫

 更新時(shí)間:2019年08月21日 11:14:23   作者:忘記時(shí)間的小白  
這篇文章主要為大家詳細(xì)介紹了Spring通過c3p0配置bean連接數(shù)據(jù)庫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Spring配置bean連接數(shù)據(jù)庫兩種方法:

(1)直接在.xml文件內(nèi)部配置:

<?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">
 
 
 <!-- 配置鏈接數(shù)據(jù)庫 -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 <property name="user" value="root"></property>
 <property name="password" value="wangjian"></property>
 <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false"></property>
 </bean>
 
</beans>

(2)將參數(shù)放在外部文件中,然后再讀入配置文件:

<?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">
 
 <!-- 導(dǎo)入屬性文件 -->
 
 <context:property-placeholder location="classpath:db.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>

外部的配置文件:

user=root
password=wangjian
driverclass=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false

測(cè)試函數(shù):

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 = (DataSource) ctx.getBean("dataSource");
 System.out.println(dataSource.getConnection());
 
 }
}

測(cè)試結(jié)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • IDEA創(chuàng)建Java?Web項(xiàng)目的超詳細(xì)圖文教學(xué)

    IDEA創(chuàng)建Java?Web項(xiàng)目的超詳細(xì)圖文教學(xué)

    IDEA是程序員們常用的java集成開發(fā)環(huán)境,也是被公認(rèn)為最好用的java開發(fā)工具,下面這篇文章主要給大家介紹了關(guān)于IDEA創(chuàng)建Java?Web項(xiàng)目的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • Java this、final等關(guān)鍵字總結(jié)

    Java this、final等關(guān)鍵字總結(jié)

    這篇文章主要對(duì)java中this、final等關(guān)鍵字進(jìn)行了總結(jié),需要的朋友可以參考下
    2017-04-04
  • Java?Mybatis框架由淺入深全解析下篇

    Java?Mybatis框架由淺入深全解析下篇

    MyBatis是一個(gè)優(yōu)秀的持久層框架,它對(duì)jdbc的操作數(shù)據(jù)庫的過程進(jìn)行封裝,使開發(fā)者只需要關(guān)注SQL本身,而不需要花費(fèi)精力去處理例如注冊(cè)驅(qū)動(dòng)、創(chuàng)建connection、創(chuàng)建statement、手動(dòng)設(shè)置參數(shù)、結(jié)果集檢索等jdbc繁雜的過程代碼,本文將作為最終篇為大家介紹MyBatis的使用
    2022-07-07
  • Java使用wait/notify實(shí)現(xiàn)線程間通信下篇

    Java使用wait/notify實(shí)現(xiàn)線程間通信下篇

    wait()和notify()是直接隸屬于Object類,也就是說所有對(duì)象都擁有這一對(duì)方法,下面這篇文章主要給大家介紹了關(guān)于使用wait/notify實(shí)現(xiàn)線程間通信的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • java 中volatile和lock原理分析

    java 中volatile和lock原理分析

    這篇文章主要介紹了java 中volatile和lock原理分析的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • 詳解Spring整合mybatis--Spring中的事務(wù)管理(xml形式)

    詳解Spring整合mybatis--Spring中的事務(wù)管理(xml形式)

    這篇文章主要介紹了Spring整合mybatis--Spring中的事務(wù)管理(xml形式),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-11-11
  • java實(shí)現(xiàn)日歷(某年的日歷,某月的日歷)用戶完全自定義

    java實(shí)現(xiàn)日歷(某年的日歷,某月的日歷)用戶完全自定義

    本篇文章介紹了,java實(shí)現(xiàn)日歷(某年的日歷,某月的日歷)用戶完全自定義。需要的朋友參考下
    2013-05-05
  • java中將一個(gè)List等分成n個(gè)list的工具方法(推薦)

    java中將一個(gè)List等分成n個(gè)list的工具方法(推薦)

    下面小編就為大家?guī)硪黄猨ava中將一個(gè)List等分成n個(gè)list的工具方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-03-03
  • MyBatis自定義類型轉(zhuǎn)換器實(shí)現(xiàn)加解密

    MyBatis自定義類型轉(zhuǎn)換器實(shí)現(xiàn)加解密

    這篇文章主要介紹了MyBatis自定義類型轉(zhuǎn)換器實(shí)現(xiàn)加解密的相關(guān)資料,需要的朋友可以參考下
    2016-07-07
  • IDEA個(gè)性化設(shè)置注釋模板詳細(xì)講解版

    IDEA個(gè)性化設(shè)置注釋模板詳細(xì)講解版

    IDEA自帶的注釋模板不是太好用,我本人到網(wǎng)上搜集了很多資料系統(tǒng)的整理了一下制作了一份比較完整的模板來分享給大家,下面這篇文章主要給大家介紹了IDEA個(gè)性化設(shè)置注釋模板的相關(guān)資料,需要的朋友可以參考下
    2024-01-01

最新評(píng)論