zoukankan      html  css  js  c++  java
  • spring4笔记----PropertyPlaceholderConfigurer 属性占位符配置器

    driverClassName=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/spring
    username=root
    password=123456
    <!-- 
    --------PropertyOverrideConfigurer ------------------
    dataSource.driverClass=com.mysql.jdbc.Driver
    dataSource.jdbcUrl=jdbc:mysql://localhost:3306/spring
    dataSource.user=root
    dataSource.password=123456
    
    
    
     -->
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
    <!--<bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">  -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <value>dataSource.properties</value>
    </list>
    </property>
    </bean>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
     p:driverClass="${driverClassName}" p:jdbcUrl="${url}" p:user="${username}" p:password="${password}"
    />
    </beans>

    package com.ij34.bean;
    import java.sql.*;
    import javax.sql.DataSource;
    
    import org.springframework.context.*;
    import org.springframework.context.support.*;
    
    public class test {
    
    public static void main(String[] args) throws Exception {
       @SuppressWarnings("resource")
    ApplicationContext  ac=new ClassPathXmlApplicationContext("beans.xml");
       DataSource ds=(DataSource) ac.getBean("dataSource");
       Connection conn=ds.getConnection();
       PreparedStatement ps= conn.prepareStatement("insert into news_inf value(null,?,?)");
       ps.setString(1, "加油2017");
       ps.setString(2, "2017,这是挑战与机遇的一年!");
       ps.executeUpdate();
       ps.close();
       conn.close();
    }
    }
  • 相关阅读:
    c数据结构 顺序表和链表 相关操作
    查找字符串中是否存在指定的字符
    比较两人生日相差多少天
    十进制转化为二进制(栈算法)
    c数据结构栈的基本操作(字符逆序输出)
    Django笔记-登陆、注册(利用cookie实现)
    Django笔记-常见错误整理
    Django笔记-post与get方法相关出错记录
    Django笔记-登陆注册-1
    Django笔记-helloworld
  • 原文地址:https://www.cnblogs.com/tk55/p/6507454.html
Copyright © 2011-2022 走看看