spring配置c3p0连接池

*在xml配置文件中

1. 配置c3p0连接池

<bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 注入属性值 -->
        <!-- 每行的name固定写法 不可以改变 -->
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost/testloginid"></property>
        <property name="user" value="root"></property>
        <property name="password" value="***123456"></property>
    </bean>

对应在类中直接实现代码

ComboPooledDataSource dataSource =new ComboPooledDataSource();//创建类
        try {
            dataSource.setDriverClass("com.mysql.jdbc.Driver");         dataSource.setJdbcUrl("jdbc:mysql://localhost/testloginid");
            dataSource.setUser("root");
            dataSource.setPassword("***123456");
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }

2.创建jdbc模板对象

<bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!-- 注入c3p0值 -->
        <property name="dataSource" ref="ds"></property>
    </bean>

对应直接实现代码

JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);//创建JdbcTemplate类并注入一个ComboPooledDataSource类型数据也就是第一步配置的数据

3.在创建的类中注入JdbcTemplate对象

<bean id="JdbcTemplateDemo1" class="srj.testJdbc.JdbcTemplateDemo1">
        <property name="jdbcTemplate" ref="JdbcTemplate"></property>
    </bean>

java完整代码如下

xml文件名为 testxml.xml
java文件路径为 srj.testJdbc.JdbcTemplateDemo1

简单练习 没有分层直接在一个文件中实验

package srj.testJdbc;

import java.beans.PropertyVetoException;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JdbcTemplateDemo1 {


    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void add() {//实现的功能

        /*
        DriverManagerDataSource dataSource=new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost/testloginid");
        dataSource.setUsername("root");
        dataSource.setPassword("srj123456");
         */

        /*ComboPooledDataSource dataSource =new ComboPooledDataSource();
        try {
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql://localhost/testloginid");
            dataSource.setUser("root");
            dataSource.setPassword("srj123456");
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }*/

/*      JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);*/



        String sql="insert into nuser(nusername,nuserpassword) values(?,?)";

        int rows=jdbcTemplate.update(sql,"Edu","123456" );//影响的行数
        System.out.println(rows);

    }
    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("testxml.xml");
        JdbcTemplateDemo1 jd=(JdbcTemplateDemo1) ac.getBean("JdbcTemplateDemo1");//创建JdbcTemplateDemo1类并调用方法
        jd.add();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值