Java使用DBCP连接池

    DBCP 是 apache 上的一个 java 连接池项目,也是 tomcat 使用的连接池组件。单独使用dbcp需要3个包:common-dbcp.jar,common-pool.jar,common-collections.jar。

package cn.com.***;

import com.google.gson.Gson;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.thrift.TException;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by t450 on 2016/8/17.
 */
public class UserCheckImpl implements UserCheckService.Iface {
	
	private static DataSource DS; 
	
	/** 获取数据库连接 */  
    public Connection getConn() {  
        Connection con = null;  
        if (DS != null) {  
            try {  
                con = DS.getConnection();  
                con.setAutoCommit(false);
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }

        return con;  
    }
    
    /** 初始化 */  
    public UserCheckImpl() {  
    }  
  
    /** 带参数初始化*/  
    public UserCheckImpl(String connectURI) {  
        initDS(connectURI);  
    } 
    
    /** 初始化方法 */  
    public UserCheckImpl(String connectURI, String username, String pswd,  
            String driverClass, int initialSize, int maxActive, int maxIdle,  
            int maxWait, int minIdle) {  
        initDS(connectURI, username, pswd, driverClass, initialSize, maxActive,  
                maxIdle, maxWait, minIdle);  
    }  
  
    /** 
     * 配置数据源
     *  
     * @param connectURI 
     *         
     * @return 
     */  
    public  synchronized static void initDS(String connectURI) {
        initDS(connectURI, "root", "*********", "com.mysql.jdbc.Driver", 5, 100,  
                30, 10000, 1);  
    }  
  
    /** 
     * 指定所有参数连接数据源 
     *  
     * @param connectURI 
     *            数据库 
     * @param username 
     *            用户名 
     * @param pswd 
     *            密码 
     * @param driverClass 
     *            数据库连接驱动名 
     * @param initialSize 
     *            初始连接池连接个数 
     * @param maxtotal 
     *            最大活动连接数 
     * @param maxIdle 
     *            最大连接数 
     * @param maxWaitMillis 
     *            获得连接的最大等待毫秒数 
     * @param minIdle 
     *            最小连接数 
     * @return 
     */  
    public synchronized static void initDS(String connectURI, String username, String pswd,
            String driverClass, int initialSize, int maxtotal, int maxIdle,  
            int maxWaitMillis , int minIdle) {
        if ( DS != null ){
           return;
        }
        BasicDataSource ds = new BasicDataSource();  
        ds.setDriverClassName(driverClass);  
        ds.setUsername(username);  
        ds.setPassword(pswd);  
        ds.setUrl(connectURI);  
        ds.setInitialSize(initialSize); // 初始连接数
        ds.setMaxTotal(maxtotal);  
        ds.setMaxIdle(maxIdle);  
        ds.setMaxWaitMillis(maxWaitMillis);  
        ds.setMinIdle(minIdle);  
        DS = ds;  
    }  
  
    /** 获得数据源连接状态*/  
    public synchronized static Map<String, Integer> getDataSourceStats() throws SQLException {
        BasicDataSource bds = (BasicDataSource) DS;  
        Map<String, Integer> map = new HashMap<String, Integer>(2);  
        map.put("active_number", bds.getNumActive());  
        map.put("idle_number", bds.getNumIdle());  
        return map;  
    }  
  
    /** 关闭数据源 */  
    protected synchronized static void shutdownDataSource() throws SQLException {
        BasicDataSource bds = (BasicDataSource) DS;  
        bds.close();  
    }  
    
    @Override
    public String check(String api_inner_id, String api_key) throws TException {
    	//UserCheckImpl db = new UserCheckImpl("jdbc:mysql://localhost/*****");
        Connection conn = null;
        Statement stmt = null;  
        ResultSet rs = null;  
        HashMap<String,String> map = new HashMap<String,String>();
        Gson gson = new Gson();
        String result = "";
        try {  
            conn = getConn();
            stmt = conn.createStatement();  
            
            int flag = 0;
    		stmt = (Statement) conn.createStatement();
    		
    		String sql = "select count(1) from t_user_api where api_key = " + "'" + api_key + "'"
    		+ " and api_id = (select api_id from t_api where api_inner_id = " + "'" + api_inner_id + "'" + ")";
    		//System.out.println(sql);
    		
    		rs = stmt.executeQuery(sql);
    		//String count = "";
    		while (rs.next()) {
    			int count = rs.getInt(1);
        		if(count != 0)
        		{
        			flag = 1;
        		}
    		}
    		
    		map.put("Status", String.valueOf(flag));
    		result = gson.toJson(map);

        } catch (SQLException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if (rs != null)  
                    rs.close();  
                if (stmt != null)  
                    stmt.close();  
                if (conn != null)  
                    conn.close();  
            } catch (Exception e) {
                e.printStackTrace();  
            }  
        }
		return result;   
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值