多数据源查询

多数据源

package com.common.utils.dynamic;

import com.common.utils.MyPropertiesUtils;
import com.common.utils.log.MyLogUtils;
import org.apache.commons.dbcp.BasicDataSource;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.ResourceBundle;

/**
 * 多数据源
 *
 * @author nachuan
 * @create 2019-04-09 14:02
 */
@Component
public class MyDynamicDataUtils {
    private static  final Logger log = MyLogUtils.getSlf4jLogger(MyDynamicDataUtils.class);



   
    

    public static void main(String[] args) {

        
        //jdbc.DriverClass.oracle
        JdbcTemplate jdbcTemplate = getJdbcTemplate("jdbc:mysql://ip:端口/testdb?useUnicode=true&characterEncoding=UTF-8", "mycat"
                , "***", "com.mysql.jdbc.Driver");
        for (int i = 1; i <8000*10000 ; i++) {
            String sql = "INSERT INTO  company  VALUES ("+i+",null,'cc','cc');";
            jdbcTemplate.execute(sql);
        }

    }

    public static JdbcTemplate getJdbcTemplate(String jdbcUrl,String  jdbcUserName,String jdbcPwd ,String jdbcDriverClass){
        BasicDataSource dataSource = new BasicDataSource();

        dataSource.setDriverClassName(jdbcDriverClass);

        JdbcTemplate jdbcTemplate = null;
        try {
            dataSource.setUrl(jdbcUrl);
            dataSource.setUsername(jdbcUserName);
            dataSource.setPassword(jdbcPwd);

            jdbcTemplate = new JdbcTemplate(dataSource);
        } catch (Exception e) {

            e.printStackTrace();
            log.warn("获取数据源属性失败");
            return null;
        }

        return jdbcTemplate;
    }
}

 

你可能感兴趣的:(我的工具类,java,java,常用工具类)