?? dbaccess.java
字號(hào):
package com.hoten.util;import com.hoten.db.*;import java.util.*;import java.sql.*;/** * <p>Title:數(shù)據(jù)庫(kù)操作</p> * <p>Description:對(duì)數(shù)據(jù)庫(kù)操作進(jìn)行全面封裝 </p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: www.ddtong.com</p> * @author lqf * @version 1.0 */public class DBAccess { private DBConnectionManager dbcm = DBConnectionManager.getInstance(); private String[] poolsName=null; private String logFile = null; private int dbTime = 1000; private String defPoolsName=null;//默認(rèn)連接池 /** * 默認(rèn)構(gòu)造函數(shù) */ public DBAccess() { poolsName = dbcm.getPoolName(); defPoolsName = poolsName[0]; logFile = dbcm.getLogFile(); } /** * 設(shè)置默認(rèn)連接池名稱(chēng) * @param name 默認(rèn)連接池名稱(chēng) * @return boolean 判斷設(shè)置是否成功 */ public boolean setDefPoolName(String name){//設(shè)置默認(rèn)連接池名稱(chēng) if(checkPoolName(name)){ defPoolsName=name; return true; } else return false; } /** * 取得數(shù)據(jù)庫(kù)訪問(wèn)的次數(shù) * @return int 訪問(wèn)次數(shù) */ public int getAccessNum(){ return dbcm.getAccessNum(); } /** * 取得指定連接池的有效連接個(gè)數(shù) * @param poolName 連接池名稱(chēng) * @return int 有效連接個(gè)數(shù) */ public int getFreeConNum(String poolName){ return dbcm.getFreeConNum(poolName); } /** * 取得默認(rèn)連接池的有效連接個(gè)數(shù) * @return int 有效連接個(gè)數(shù) */ public int getFreeConNum(){ return dbcm.getFreeConNum(defPoolsName); } /** * 取得默認(rèn)連接池的已使用連接個(gè)數(shù) * @return int 已使用連接個(gè)數(shù) */ public int getUsedConNum(){ return dbcm.getUsedConNum(defPoolsName); } /** * 取得指定連接池的已使用連接個(gè)數(shù) * @param poolName 連接池名稱(chēng) * @return int 已使用連接個(gè)數(shù) */ public int getUsedConNum(String poolName){ return dbcm.getUsedConNum(poolName); } /** * 取得默認(rèn)連接池名稱(chēng) * @return String 連接池名稱(chēng) */ public String getDefPoolName(){//取得默認(rèn)連接池名稱(chēng) return defPoolsName; } /** * 取得默認(rèn)連接池的一個(gè)連接 * @return Connection 可用的連接 */ public Connection getConnection(){//取得默認(rèn)的連接 return getConnection(defPoolsName); } /** * 取得指定連接池的連接 * @param poolName 連接池名稱(chēng) * @return Connection 可用的連接 */ public Connection getConnection(String poolName){//取得指定連接池的連接 if(!checkPoolName(poolName)){ Log.printEvent("Thread :"+Thread.currentThread().getName()+" dbPoolName is error in getConnetion()! the errorName is "+poolName,logFile); poolName = defPoolsName; } Connection con = null; while(con==null){ con=dbcm.getConnection(poolName,dbTime); } return con; } /** * 釋放連接到默認(rèn)的連接池 * @param con 釋放的連接 */ public void freeConnection(Connection con){//釋放連接到默認(rèn)的連接池 freeConnection(con,defPoolsName); } /** * 釋放連接到指定的連接池 * @param con 要釋放的連接 * @param String poolName 連接池名稱(chēng) */ public void freeConnection(Connection con,String poolName){ if(!checkPoolName(poolName)){ Log.printEvent("Thread :"+Thread.currentThread().getName()+" dbPoolName is error in freeConnection()! the errorName is "+poolName,logFile); poolName = defPoolsName; } dbcm.freeConnection(poolName,con); } /** * 注銷(xiāo)此連接操作對(duì)象 */ public void release(){ dbcm.release(); } /** * 插入操作使用默認(rèn)連接池 * @param sql 插入的sql語(yǔ)句 * @throws Exception 發(fā)生錯(cuò)誤時(shí)拋出異常 */ public void insert(String sql)throws Exception{ change_Access(sql,defPoolsName); } /* * 插入操作使用指定的連接池 * @param sql 插入的sql語(yǔ)句 * @param poolName 連接池的名稱(chēng) * @exception Exception 發(fā)生錯(cuò)誤時(shí)拋出異常 */ public void insert(String sql,String poolName)throws Exception{//插入操作使用指定連接池 change_Access(sql,poolName); } /** * 更新操作使用默認(rèn)連接池 * @param sql 插入的sql語(yǔ)句 * @throws Exception 發(fā)生錯(cuò)誤時(shí)拋出異常 */ public void update(String sql)throws Exception{//更新操作使用默認(rèn)連接池 change_Access(sql,defPoolsName); } /** * 更新操作使用指定的連接池 * @param sql 插入的sql語(yǔ)句 * @param poolName 連接池的名稱(chēng) * @throws Exception 發(fā)生錯(cuò)誤時(shí)拋出異常 */ public void update(String sql,String poolName)throws Exception{//更新操作使用指定連接池 change_Access(sql,poolName); } /** * 刪除操作使用默認(rèn)連接池 * @param sql 插入的sql語(yǔ)句 * @throws Exception 發(fā)生錯(cuò)誤時(shí)拋出異常 */ public void delete(String sql)throws Exception{//刪除操作使用默認(rèn)連接池 change_Access(sql,defPoolsName); } /** * 刪除操作使用指定的連接池 * @param sql 刪除的sql語(yǔ)句 * @param poolName 連接池的名稱(chēng) * @throws Exception 發(fā)生錯(cuò)誤時(shí)拋出異常 */ public void delete(String sql,String poolName)throws Exception{//刪除操作使用指定連接池 change_Access(sql,poolName); } private void change_Access(String sql,String poolName)throws Exception{//非查詢(xún)操作使用指定連接池 Connection con = null; Statement stmt = null; try { con = getConnection(poolName); stmt = con.createStatement(); stmt.executeUpdate(sql); } catch (Exception ex) { throw ex; } finally { try { stmt.close(); } catch (Exception ex) { } freeConnection(con,poolName); } } /** * 查詢(xún)一行一列,使用默認(rèn)連接池 * @param sql 插入的sql語(yǔ)句 * @return String 返回查詢(xún)的值(沒(méi)有為null,反之為一字符串) * @throws Exception 發(fā)生錯(cuò)誤時(shí)拋出異常 */ public String select(String sql) throws Exception{//查詢(xún)一行一列,使用默認(rèn)連接池 Vector v = select(sql,1,1,defPoolsName); if(v==null) return null; return (String)v.get(0); } /** * 查詢(xún)一行一列,使用指定的連接池 * @param sql 插入的sql語(yǔ)句 * @param poolName 連接池名稱(chēng) * @return String 返回查詢(xún)的值(沒(méi)有為null,反之為一字符串) * @throws Exception 發(fā)生錯(cuò)誤時(shí)拋出異常 */ public String select(String sql,String poolName) throws Exception{//查詢(xún)一行一列,使用指定連接池 Vector v =select(sql,1,1,poolName); if(v==null) return null; return (String)v.get(0); } /** * 查詢(xún)一行多列,使用默認(rèn)連接池(lineNum為列數(shù)) * @param sql 插入的sql語(yǔ)句 * @param lineNum 列數(shù) * @return Vector 返回查詢(xún)的值(沒(méi)有為null) * @throws Exception 發(fā)生錯(cuò)誤時(shí)拋出異常 */ public Vector select(String sql,int lineNum) throws Exception{//查詢(xún)一行多列,使用默認(rèn)連接池(lineNum 為列數(shù)) return select(sql,lineNum,1,defPoolsName); } /** * 查詢(xún)一行多列,使用指定的連接池(lineNum為列數(shù)) * @param sql 插入的sql語(yǔ)句 * @param lineNum 列數(shù) * @param poolName 名稱(chēng)連接池 * @return Vector 返回查詢(xún)的值(沒(méi)有為null) * @throws Exception 發(fā)生錯(cuò)誤時(shí)拋出異常 */ public Vector select(String sql,int lineNum,String poolName) throws Exception{//查詢(xún)一行多列,使用指定連接池(lineNum 為列數(shù)) return select(sql,lineNum,1,poolName); } /** * 查詢(xún)r(jià)owNum行l(wèi)ineNum列,使用默認(rèn)連接池 * @param sql 插入的sql語(yǔ)句 * @param lineNum 列數(shù) * @param rowNum 行數(shù) * @return Vector 返回查詢(xún)的值(沒(méi)有為null) * @throws Exception 發(fā)生錯(cuò)誤時(shí)拋出異常 */ public Vector select(String sql,int lineNum,int rowNum) throws Exception{//查詢(xún)r(jià)owNum行l(wèi)ineNum列,使用默認(rèn)連接池(lineNum 為列數(shù),rowNum為行數(shù)) return select(sql,lineNum,rowNum,defPoolsName); } /** * 查詢(xún)r(jià)owNum行l(wèi)ineNum列,使用指定連接池 * @param sql 插入的sql語(yǔ)句 * @param lineNum 列數(shù) */ public Vector select(String sql,int lineNum,int rowNum,String poolName) throws Exception{ Vector rows = new Vector();//行 Connection con = null; Statement stmt = null; ResultSet rs = null; try { con = getConnection(poolName); stmt = con.createStatement(); rs = stmt.executeQuery(sql); if(rowNum==1){//一行時(shí)使用rows來(lái)存放返回的數(shù)據(jù) if(rs.next()){ for(int i=0;i<lineNum;i++){ String value = rs.getString(i+1); if(value!=null) rows.addElement(Chinese.fromDatabase(value)); else rows.addElement(null); } }else{ return null; } }else{ while(rs.next()){ Vector line = new Vector(); for(int i=0;i<lineNum;i++){ String value = rs.getString(i+1); if(value!=null) line.addElement(Chinese.fromDatabase(value)); else line.addElement(null); } rows.addElement(line); if((--rowNum)==0) break; } } } catch (Exception ex) { throw ex; } finally { try { rs.close(); stmt.close(); } catch (Exception ex) { } freeConnection(con,poolName); } if(rows.size()==0) return null; return rows; } private boolean checkPoolName(String name){//檢測(cè)輸入的連接池名字是否正確 boolean flag =false; int size = poolsName.length; for(int i=0;i<size;i++){ if(poolsName[i].equals(name)){ flag=true; break; } } return flag; }}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -