一个Oracle数据库链接的JavaBean实例

来源:Oracle认证    发布时间:2012-11-12    Oracle认证视频    评论

public class OracleConnectionManager {

private DBConnectionManager connMgr;
private final String poolName="Oracle";
private Connection conn;

public OracleConnectionManager() {

String userID = "system";
String password = "manager";
String JDBCDriver = "oracle.jdbc.driver.OracleDriver";
String JDBCDriverType = "jdbc:oracle:thin";
String DBHost = "127.0.0.1";
String Port = "1521";
String SID="sid";
String url = JDBCDriverType+":@"+DBHost + ":" + Port + ":" +SID;


connMgr = DBConnectionManager.getInstance();
if (DBConnectionManager.clients == 1){
connMgr.init(poolName, JDBCDriver, url, userID, password);
connMgr.init("egongOracle", JDBCDriver, JDBCDriverType+":@110.7.6.22:1521:egong", "abcusername", "abcpasswd");


}
}

public Connection getConnection() {
return (conn=connMgr.getConnection(poolName));
}

public Connection getConnection(String newOracle) {
return (conn=connMgr.getConnection(newOracle));
}


public void freeConnection() {
connMgr.freeConnection(poolName,conn);
}

public void freeConnection(String newOracle) {
connMgr.freeConnection(newOracle,conn);
}

}


public class DBConnectionManager {
static private DBConnectionManager instance=null; // The single instance
static public int clients=0;

private Vector drivers = new Vector();
private PrintWriter log;
private Hashtable pools = new Hashtable();
private final int MAX_CONN = 1000;

/**
* Returns the single instance, creating one if it´s the
* first time this method is called.
*
* @return DBConnectionManager The single instance.
*/
static synchronized public DBConnectionManager getInstance() {
if (instance == null) {
instance = new DBConnectionManager();
}
clients++;
return instance;
}

/**
* A private constructor since this is a Singleton
*/
private DBConnectionManager() {
// init();
}

/**
* Returns a connection to the named pool.
*
* @param name The pool name as defined in the properties file
* @param con The Connection
*/
public void freeConnection(String name, Connection con) {
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null) {
pool.freeConnection(con);
}
}

/**
* Returns an open connection. If no one is available, and the max
* number of connections has not been reached, a new connection is
* created.
*
* @param name The pool name as defined in the properties file
* @return Connection The connection or null
*/
public Connection getConnection(String name) {
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null) {
return pool.getConnection();
}
return null;
}

上一页1234下一页

视频学习

我考网版权与免责声明

① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;

② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。

最近更新

社区交流

考试问答