阿里巴巴连接池之一Druid

package com.tenwa.reckon;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import com.alibaba.druid.pool.DruidDataSource;

public class DruidERPDataSource {

    private static DruidDataSource dataSource;
    private Connection connection;
    static {
        Properties prop = new Properties();
        InputStream is = null;
        try {
            is = new FileInputStream(DruidERPDataSource.class.getClassLoader().getResource("datasource.properties").getFile());
            prop.load(is);
            dataSource = new DruidDataSource();
            dataSource.setUrl(prop.getProperty("druid.url"));
            dataSource.setDriverClassName(prop.getProperty("druid.driver"));
            dataSource.setUsername(prop.getProperty("druid.user"));
            dataSource.setPassword(prop.getProperty("druid.pwd"));
            dataSource.setMinIdle(Integer.parseInt((prop.getProperty("druid.minIdle"))));
            dataSource.setMaxActive(Integer.parseInt((prop.getProperty("druid.maxActive"))));
            dataSource.setInitialSize(Integer.parseInt((prop.getProperty("druid.initialSize"))));
            dataSource.setMaxWait((Long.parseLong((prop.getProperty("druid.maxWait")))));
            dataSource.setTimeBetweenEvictionRunsMillis((Long.parseLong((prop.getProperty("druid.timeBetweenEvictionRunsMillis")))));
            dataSource.setMinEvictableIdleTimeMillis((Long.parseLong((prop.getProperty("druid.minEvictableIdleTimeMillis")))));
            dataSource.setTestWhileIdle((Boolean.parseBoolean((prop.getProperty("druid.testWhileIdle")))));
            dataSource.setTestOnBorrow((Boolean.parseBoolean((prop.getProperty("druid.testOnBorrow")))));
            dataSource.setTestOnReturn((Boolean.parseBoolean((prop.getProperty("druid.testOnReturn")))));
            dataSource.setPoolPreparedStatements((Boolean.parseBoolean((prop.getProperty("druid.poolPreparedStatements")))));
            dataSource.setMaxPoolPreparedStatementPerConnectionSize(Integer.parseInt((prop.getProperty("druid.maxPoolPreparedStatementPerConnectionSize"))));
            dataSource.setDefaultAutoCommit((Boolean.parseBoolean((prop.getProperty("druid.defaultAutoCommit")))));
            dataSource.setValidationQuery(prop.getProperty("druid.validationQuery"));
            try {
                dataSource.setFilters(prop.getProperty("druid.filters"));
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            dataSource.setRemoveAbandonedTimeout(Integer.parseInt((prop.getProperty("druid.removeAbandonedTimeout"))));
            dataSource.setLogAbandoned((Boolean.parseBoolean((prop.getProperty("druid.logAbandoned")))));
        } catch(IOException e) {
            
        }
    }
    
    public static void main(String[] args) throws Exception {
        for(int i =0;i<400;i++){
            DruidERPDataSource erp = new DruidERPDataSource();
            ResultSet rs = erp.executeQuery("select * from proj_info");
            while(rs.next()) {
                System.out.println(rs.getString("proj_id"));
            }
            
        }
        for(int i =0;i<900;i++){
            DruidERPDataSource erp = new DruidERPDataSource();
            ResultSet rs = erp.executeQuery("select * from proj_info");
            while(rs.next()) {
                System.out.println(rs.getString("proj_id"));
            }
            
        }
        //erp.close();
    }
    
    public void getConnection() {
        try {
            connection = dataSource.getConnection(); 
        } catch (SQLException e) { 
          e.printStackTrace();
        } 
    } 
    
    public  void close() { 
        if(null != connection) { 
            try {
                if(!connection.isClosed()) {
                    connection.close();
                    this.connection = null;
                }
            } catch (SQLException e) { 
                e.printStackTrace();
            } 
        } 
    } 
    
    public DruidERPDataSource() throws Exception {
        getConnection();
        if(this.connection == null) {
            throw new Exception("未获取到连接");
        }
    }
    
    public int executeUpdate(String sql) throws SQLException {
        Statement stat = null;
        int result= 0;
        try {
            if(connection == null) {
                getConnection();
            }
            stat = this.connection.createStatement();
            result = stat.executeUpdate(sql);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    public ResultSet executeQuery(String sql) throws SQLException {
        Statement stat = null;
        ResultSet result = null;
        try {
            if(connection == null) {
                getConnection();
            }
            stat = this.connection.createStatement();
            result = stat.executeQuery(sql);
        }catch(Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值