JDBC四种数据源连接数据库

maven工程依赖

pom.xml

    
        
            org.springframework
            spring-context
        
        
            org.slf4j
            slf4j-log4j12
        
        
            junit
            junit
        
        
            org.springframework
            spring-test
        
        
        
            org.springframework
            spring-jdbc
        
        
        
            com.alibaba
            druid
        

        
            mysql
            mysql-connector-java
        

        
        
        
            com.mchange
            c3p0
            0.9.5.2
        

        
        
        
            org.apache.commons
            commons-dbcp2
            2.1.1
        

    

TestJdbcTemplate.java

package com.day03.test;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import java.beans.PropertyVetoException;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 19:18 2018/11/14
 */

public class TestJdbcTemplate {

    //DBCP连接池
    @Test
    public void test0(){
        //dbcp连接池核心类
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/mybatis");
        dataSource.setUsername("root");
        dataSource.setPassword("sswqzx");

        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.update("insert into account values(null, ?,?)","sswer22",99999999);

    }


    //DriverManagerDataSource连接池
    @Test
    public void test1(){
        //创建连接池
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/mybatis");
        dataSource.setUsername("root");
        dataSource.setPassword("sswqzx");

        //创建jdbc模板
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.update("insert into account values(null, ?,?)","sswer",99999999);
    }

    //Druid数据库连接池
    @Test
    public void test2(){
        //创建jdbc模板对象
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        //创建druid数据源、其它数据源也行、
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/mybatis");
        dataSource.setUsername("root");
        dataSource.setPassword("sswqzx");
        //设置数据源
        jdbcTemplate.setDataSource(dataSource);
        //插入操作
        jdbcTemplate.update("insert into account values(null, ?,?)","ssw",2000000000);
    }

    //c3p0数据源连接池
    @Test
    public void test3(){
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        try {
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mybatis");
            dataSource.setUser("root");
            dataSource.setPassword("sswqzx");
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        //插入操作
        jdbcTemplate.update("insert into account values(null, ?,?)","ssw11",2000000000);
    }
}

JDBC四种数据源连接数据库_第1张图片

 

你可能感兴趣的:(Mysql,JDBC四种数据源连接数据库)