Proxool老牌的數據庫連接池了
環境
MySQL
JDK
Proxool
一
proxool
<?xml version=
<something
<proxool>
<alias>ds</alias>
<!
<driver
<!
<driver
<!
<driver
<property name=
<!
<property name=
<!
</driver
<!
<simultaneous
<!
<maximum
<!
<minimum
<!
<house
<!
<prototype
<!
<test
<!
<house
</proxool>
</something
粗體部分是變化部分
二
package lavasoft;
import mons
import mons
import org
import org
import java
import java
import java
import java
import java
/**
* 簡單的JDBC工具類
*
* @author leizhimin
*/
public class MyDB {
private static final Log log = LogFactory
private static final boolean useDBPool = true; //是否使用數據庫連接池
private static String dburl = null;
private static String user = null;
private static String password = null;
private static Properties props = new Properties();
static {
init();
}
public static void init() {
if (useDBPool) {
try {
nfigure(
// nfigure(
} catch (ProxoolException e) {
e
}
return;
}
try {
// props
props
} catch (IOException e) {
log
}
dburl = props
user = props
password = props
System
System
System
//注冊驅動類
try {
Class
} catch (ClassNotFoundException e) {
log
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws FileNotFoundException {
for (int i =
Connection conn = getConnection();
System
// System
// closeConnection(conn);
}
}
/**
* 創建一個數據庫連接
*
* @return 一個數據庫連接
*/
public static Connection getConnection() {
Connection conn = null;
//根據連接池配置創建數據庫連接
if (useDBPool) {
try {
conn = DriverManager
} catch (SQLException e) {
log
throw new RuntimeException(e);
}
return conn;
}
//根據JDBC配置創建數據庫連接
try {
conn = DriverManager
} catch (SQLException e) {
log
throw new RuntimeException(e);
}
return conn;
}
/**
* 在一個數據庫連接上執行一個靜態SQL語句查詢
*
* @param conn 數據庫連接
* @param staticSql 靜態SQL語句字符串
* @return 返回查詢結果集ResultSet對象
*/
public static ResultSet executeQuery(Connection conn
ResultSet rs = null;
try {
//創建執行SQL的對象
Statement stmt = conn
//執行SQL
rs = stmt
} catch (SQLException e) {
log
throw new RuntimeException(e);
}
return rs;
}
/**
* 在一個數據庫連接上執行一個靜態SQL語句
*
* @param conn 數據庫連接
* @param staticSql 靜態SQL語句字符串
*/
public static void executeSQL(Connection conn
try {
//創建執行SQL的對象
Statement stmt = conn
//執行SQL
stmt
} catch (SQLException e) {
log
throw new RuntimeException(e);
}
}
/**
* 在一個數據庫連接上執行一批靜態SQL語句
*
* @param conn 數據庫連接
* @param sqlList 靜態SQL語句字符串集合
*/
public static void executeBatchSQL(Connection conn
try {
//創建執行SQL的對象
Statement stmt = conn
for (String sql : sqlList) {
stmt
}
//執行SQL
stmt
} catch (SQLException e) {
log
}
}
public static void closeConnection(Connection conn) {
if (conn == null) return;
try {
if (!conn
//關閉數據庫連接
conn
}
} catch (SQLException e) {
log
throw new RuntimeException(e);
}
}
}
運行結果
[INFO]
連上了
連上了
連上了
連上了
連上了
[INFO]
[INFO]
[WARN]
[INFO]
[INFO]
Process finished with exit code
Proxool提供的配置方式很多
三
proxool的配置文件加載做的比較差勁
org
org
org
org
org
這幾個類加載配置文件時候
public static void init() {
//初始化數據庫連接配置參數
InputStream in = MyDB
Reader reader = null;
try {
reader = new InputStreamReader(in
} catch (UnsupportedEncodingException e) {
e
}
try {
nfigure(reader
} catch (ProxoolException e) {
e
}
}
在初始化Proxool環境的時候
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25682.html