日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java dbcp连接池 使用_Java使用DBCP连接池

發(fā)布時間:2024/3/24 java 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java dbcp连接池 使用_Java使用DBCP连接池 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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;

/** 獲取數(shù)據(jù)庫連接 */

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() {

}

/** 帶參數(shù)初始化*/

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);

}

/**

* 配置數(shù)據(jù)源

*

* @param connectURI

*

* @return

*/

public synchronized static void initDS(String connectURI) {

initDS(connectURI, "root", "*********", "com.mysql.jdbc.Driver", 5, 100,

30, 10000, 1);

}

/**

* 指定所有參數(shù)連接數(shù)據(jù)源

*

* @param connectURI

* 數(shù)據(jù)庫

* @param username

* 用戶名

* @param pswd

* 密碼

* @param driverClass

* 數(shù)據(jù)庫連接驅(qū)動名

* @param initialSize

* 初始連接池連接個數(shù)

* @param maxtotal

* 最大活動連接數(shù)

* @param maxIdle

* 最大連接數(shù)

* @param maxWaitMillis

* 獲得連接的最大等待毫秒數(shù)

* @param minIdle

* 最小連接數(shù)

* @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); // 初始連接數(shù)

ds.setMaxTotal(maxtotal);

ds.setMaxIdle(maxIdle);

ds.setMaxWaitMillis(maxWaitMillis);

ds.setMinIdle(minIdle);

DS = ds;

}

/** 獲得數(shù)據(jù)源連接狀態(tài)*/

public synchronized static Map getDataSourceStats() throws SQLException {

BasicDataSource bds = (BasicDataSource) DS;

Map map = new HashMap(2);

map.put("active_number", bds.getNumActive());

map.put("idle_number", bds.getNumIdle());

return map;

}

/** 關閉數(shù)據(jù)源 */

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 map = new HashMap();

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;

}

}

總結(jié)

以上是生活随笔為你收集整理的java dbcp连接池 使用_Java使用DBCP连接池的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。