java dbcp连接池 使用_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 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;
}
/** 關閉數據源 */
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;
}
}
總結
以上是生活随笔為你收集整理的java dbcp连接池 使用_Java使用DBCP连接池的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 集成百度离在线语音唤醒/语音合成sdk
- 下一篇: MyCobot六轴机械臂的基本操作(二)