java 通过连接池来创建连接数据库

一.导入包

 proxool-0.9.1.jar
 proxool-cglib.jar

二.创建类

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class DBConnection {
private static String driverClass = "org.logicalcobwebs.proxool.ProxoolDriver";

/**
* 获取数据库连接ytcd,ytcd

* @return Connection
*/
public Connection getConnOne() {
Connection connOne = null;
try {
Class.forName(driverClass);


connOne = DriverManager.getConnection("proxool.connOne");
} catch (Exception e) {
e.printStackTrace();
}
return connOne;
}


/**
* 获取数据库连接

* @return Connection
*/
public Connection getConnTwo() {
Connection connTwo = null;
try {
Class.forName(driverClass);


connTwo = DriverManager.getConnection("proxool.connTwo");
} catch (Exception e) {
e.printStackTrace();
}
return connTwo;
}


/**
* 释放连接 Connection

* @param Connection
*/
public void CloseConn(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}


/**
* 释放连接 CallableStatement

* @param CallableStatement
*/
public void CloseCallSta(CallableStatement cstmt) {
if (cstmt != null) {
try {
cstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}


/**
* 释放连接 PreparedStatement

* @param PreparedStatement
*/
public void ClosePrepSta(PreparedStatement pstmt) {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}


/**
* 释放连接 ResultSet

* @param ResultSet
*/
public void CloseResuSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}

三.配置proxool.xml




connOne
jdbc:oracle:thin:@162.68.420.49:1599:gggg
oracle.jdbc.driver.OracleDriver





20

2

90000

10

5

false

select id from t_sysadmin




connTwo
jdbc:oracle:thin:@1455.64.552232.64:1591:tgggg
oracle.jdbc.driver.OracleDriver





20

2

90000

10

5

false

select id from t_sysadmin


 


四.web.xm 中配置


ServletConfigurator
org.logicalcobwebs.proxool.configuration.ServletConfigurator

xmlFile
WEB-INF/proxool.xml

1


五.类中调用

    。。。。

你可能感兴趣的:(java 通过连接池来创建连接数据库)