?? poolman.java
字號:
/**
* Created at Nov 30, 2008
*/
package com.jdev.db;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
/**
* <p>Title: PoolMan</p>
* <p>Description: </p>
* @author Lawrence
* @version
*/
public class PoolMan extends ConnectPool {
private ConnectPool connMgr;
private Statement stmt;
private Connection con;
private ResultSet rst;
/**
* 對象連接初始化
*/
public Connection getPool(String name) throws Exception {
try {
connMgr = ConnectPool.getInstance();
con = connMgr.getConnection(name);
} catch (Exception e) {
System.err.println("不能創(chuàng)建連接!請嘗試重啟應(yīng)用服務(wù)器");
}
return con;
}
/**
* 同以上方法,加入連接空閑等待時間 待用方法
*/
public Connection getPool_t(String name, long time) throws Exception {
try {
connMgr = ConnectPool.getInstance();
con = connMgr.getConnection(name, time);
} catch (Exception e) {
System.err.println("不能創(chuàng)建連接!");
}
return con;
}
/**
* 執(zhí)行查詢方法1
*/
public ResultSet executeQuery(String SqlStr) throws Exception {
ResultSet result = null;
try {
stmt = con.createStatement();
result = stmt.executeQuery(SqlStr);
// here add one line by jnma 12.11
con.commit();
} catch (java.sql.SQLException e) {
throw new Exception("執(zhí)行查詢語句出錯");
}
return result;
}
/**
* 執(zhí)行查詢方法2
*/
public ResultSet getRst(String SqlStr) throws Exception {
// ResultSet result = null;
try {
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rst = stmt.executeQuery(SqlStr);
// here add one line by jnma 12.11
con.commit();
} catch (java.sql.SQLException e) {
throw new Exception("執(zhí)行查詢語句出錯");
}
return rst;
}
/**
* 執(zhí)行更新
*/
public int Update(String SqlStr) throws Exception {
int result = -1;
try {
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
result = stmt.executeUpdate(SqlStr);
// here add one line by jnma 12.11
con.commit();
if (result == 0)
System.out.println("執(zhí)行delete,update,insert SQL出錯");
} catch (java.sql.SQLException e) {
System.err.println("執(zhí)行delete,update,insert SQL出錯");
}
return result;
}
/**
* 執(zhí)行事務(wù)處理
*/
public boolean handleTransaction(Vector SqlArray) throws Exception {
boolean result = false;
int ArraySize = SqlArray.size();
try {
stmt = con.createStatement();
con.setAutoCommit(false);
System.out.println("ArraySize is" + ArraySize);
for (int i = 0; i < ArraySize; i++) {
System.out.println(" 開始執(zhí)行語句" + (String) SqlArray.elementAt(i));
stmt.executeUpdate((String) SqlArray.elementAt(i));
System.out.println(" 執(zhí)行成功");
}
con.commit();
con.setAutoCommit(true);// 必須
System.out.println("事務(wù)執(zhí)行成功");
result = true;
} catch (java.sql.SQLException e) {
try {
System.out.println(e.toString());
System.out.println("數(shù)據(jù)庫操作失敗");
con.rollback();
} catch (java.sql.SQLException Te) {
System.err.println("事務(wù)出錯回滾異常");
}
}
try {
con.setAutoCommit(true);
} catch (java.sql.SQLException e) {
System.err.println("設(shè)置自動提交失敗");
}
return result;
}
/**
* 釋放連接
*/
public void close(String name) throws Exception {
try {
if (stmt != null)
stmt.close();
if (con != null) {
connMgr.freeConnection(name, con);
System.out.println(" [c 正在釋放一個連接 ] ");
}
} catch (java.sql.SQLException e) {
System.err.println("釋放連接出錯");
}
}
/*
* (non-Javadoc)
*
* @see com.jdev.db.ConnectPool#freeConnection(java.lang.String,
* java.sql.Connection)
*/
@Override
public void freeConnection(String name, Connection con) {
// TODO Auto-generated method stub
super.freeConnection(name, con);
}
/*
* (non-Javadoc)
*
* @see com.jdev.db.ConnectPool#getConnection(java.lang.String, long)
*/
@Override
public Connection getConnection(String name, long time) {
// TODO Auto-generated method stub
return super.getConnection(name, time);
}
/*
* (non-Javadoc)
*
* @see com.jdev.db.ConnectPool#getConnection(java.lang.String)
*/
@Override
public Connection getConnection(String name) {
// TODO Auto-generated method stub
return super.getConnection(name);
}
/*
* (non-Javadoc)
*
* @see com.jdev.db.ConnectPool#release()
*/
@Override
public synchronized void release() {
// TODO Auto-generated method stub
super.release();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -