javajohn

          金色年華

          apache dbcp數(shù)據(jù)庫連接池的使用

          ?

          ??1 public ? class ?DaoUtil? {
          ??2
          ??3 ???? /**
          ??4 ?????*?數(shù)據(jù)庫連接池
          ??5 ?????*?
          ??6 ?????*? @see ? http://jakarta.apache.org/commons/dbcp/index.html
          ??7 ????? */

          ??8 ???? private ? static ?PoolingDriver?driver? = ? null ;
          ??9
          ?10 ???? /**
          ?11 ?????*?設(shè)置一個數(shù)據(jù)庫連接池
          ?12 ?????*?
          ?13 ?????*? @param ?name
          ?14 ?????*????????????連接池的名稱
          ?15 ?????*? @param ?url
          ?16 ?????*????????????數(shù)據(jù)源
          ?17 ?????*? @throws ?SQLException
          ?18 ????? */

          ?19 ???? private ? static ? void ?setUpDriverPool(String?name,?String?url)
          ?20 ???????????? throws ?SQLException? {
          ?21 ???????? if ?((driver? == ? null )? || ?driver.getPoolNames().length? < ? 2 )? {
          ?22 ???????????? try ? {
          ?23 ???????????????? /**
          ?24 ?????????????????*?首先創(chuàng)建一個對象池來保存數(shù)據(jù)庫連接?使用?commons.pool?的?GenericObjectPool對象
          ?25 ????????????????? */

          ?26 ????????????????ObjectPool?connectionPool? = ? new ?GenericObjectPool();
          ?27 ???????????????? /**
          ?28 ?????????????????*?創(chuàng)建一個?DriverManagerConnectionFactory對象?連接池將用它來獲取一個連接
          ?29 ????????????????? */

          ?30 ????????????????ConnectionFactory?connectionFactory? = ? new ?DriverManagerConnectionFactory(
          ?31 ????????????????????????url,? null );
          ?32 ???????????????? /**
          ?33 ?????????????????*?創(chuàng)建一個PoolableConnectionFactory?對象。
          ?34 ????????????????? */

          ?35 ????????????????PoolableConnectionFactory?poolableConnectionFactory? = ? new ?PoolableConnectionFactory(
          ?36 ????????????????????????connectionFactory,?connectionPool,? null ,? null ,? false ,
          ?37 ???????????????????????? true );
          ?38 ???????????????? /**
          ?39 ?????????????????*?注冊PoolingDriver。
          ?40 ????????????????? */

          ?41 ????????????????Class.forName( " org.apache.commons.dbcp.PoolingDriver " );
          ?42 ????????????????driver? = ?(PoolingDriver)?DriverManager.getDriver( " jdbc:apache:commons:dbcp: " );
          ?43 ????????????????driver.registerPool(name,?connectionPool);
          ?44 ????????????}
          ? catch ?(ClassNotFoundException?e)? {
          ?45 ???????????????? throw ? new ?RuntimeException(e);
          ?46 ????????????}

          ?47 ????????}

          ?48 ????}

          ?49
          ?50 ???? /**
          ?51 ?????*?關(guān)閉所有數(shù)據(jù)庫連接池
          ?52 ?????*?
          ?53 ????? */

          ?54 ???? public ? static ? void ?shutDownDriver()? {
          ?55
          ?56 ???????? try ? {
          ?57 ????????????PoolingDriver?driver? = ?(PoolingDriver)?DriverManager
          ?58 ????????????????????.getDriver( " jdbc:apache:commons:dbcp: " );
          ?59 ????????????String[]?poolNames? = ?driver.getPoolNames();
          ?60 ???????????? if (poolNames.length? > ? 1 ) {
          ?61 ???????????????? for ?( int ?i? = ? 0 ;?i? < ?poolNames.length;?i ++ )? {
          ?62 ????????????????????driver.closePool( " pool " );
          ?63 ????????????????}

          ?64 ????????????}

          ?65 ????????}
          ? catch ?(SQLException?sqle)? {
          ?66 ???????????? throw ? new ?RuntimeException(sqle);
          ?67 ????????}

          ?68 ????}

          ?69
          ?70 ???? /**
          ?71 ?????*?取得一個數(shù)據(jù)庫連接對象。
          ?72 ?????*?
          ?73 ?????*?因為可能使用兩個不同的數(shù)據(jù)庫,?所以依據(jù)report的值來確定使用那個數(shù)據(jù)庫。
          ?74 ?????*?
          ?75 ?????*? @param ?report
          ?76 ?????*? @return
          ?77 ????? */

          ?78 ???? public ? static ?Connection?getConnection()? {
          ?79 ????????Connection?con? = ? null ;
          ?80 ???????? try ? {
          ?81 ????????????ReadConfig?readConfig? = ? new ?ReadConfig();
          ?82 ????????????readConfig.init( null );
          ?83 ???????????? // ?裝載mysql的jdbc驅(qū)動
          ?84 ????????????String?driver? = ?readConfig.getDBDriver();
          ?85 ????????????String?url? = ?readConfig.getDBUrl();
          ?86 ????????????String?poolName? = ? " pool " ;
          ?87 ????????????Class.forName(driver);
          ?88 ????????????setUpDriverPool(poolName,?url);
          ?89 ????????????con? = ?DriverManager.getConnection( " jdbc:apache:commons:dbcp: "
          ?90 ???????????????????? + ?poolName);
          ?91 ???????????? return ?con;
          ?92 ????????}
          ? catch ?(ClassNotFoundException?cnfe)? {
          ?93 ???????????? throw ? new ?RuntimeException( " 無法裝入數(shù)據(jù)庫引擎 " );
          ?94 ????????}
          ? catch ?(SQLException?sqle)? {
          ?95 ???????????? throw ? new ?RuntimeException( " 無法打開數(shù)據(jù)庫連接 " );
          ?96 ????????}

          ?97 ????}

          ?98
          ?99 ???? /**
          100 ?????*?執(zhí)行清理過程
          101 ?????*?<li>關(guān)閉數(shù)據(jù)庫連接</li>
          102 ?????*?<li>關(guān)閉語句對象</li>
          103 ?????*?<li>關(guān)閉結(jié)果集</li>
          104 ?????*?
          105 ?????*? @param ?con
          106 ?????*? @param ?s
          107 ?????*? @param ?rs
          108 ????? */

          109 ???? public ? static ? void ?closeAll(Connection?con,?Statement?s,?ResultSet?rs)? {
          110 ???????? try ? {
          111 ???????????? if ?(rs? != ? null )? {
          112 ????????????????rs.close();
          113 ????????????????rs? = ? null ;
          114 ????????????}

          115 ???????????? if ?(s? != ? null )? {
          116 ????????????????s.close();
          117 ????????????????s? = ? null ;
          118 ????????????}

          119 ???????????? if ?(con? != ? null )? {
          120 ????????????????con.close();
          121 ????????????????con? = ? null ;
          122 ????????????}

          123 ????????}
          ? catch ?(SQLException?sqle)? {
          124 ???????????? // ?nothing?to?do,?forget?it;
          125 ????????}

          126 ????}

          127
          128 ???? public ? static ? void ?main(String[]?args)? {
          129 // ????????DaoUtil?daoUtil?=?new?DaoUtil();
          130 // ????????Connection?connection?=?null;
          131 // ????????Statement?statement?=?null;
          132 // ????????connection?=?daoUtil.getConnection();
          133 // ????????ResultSet?rs?=?null;
          134 // ????????try?{
          135 // ????????????statement?=?connection.createStatement();
          136 // ????????????rs?=?statement.executeQuery("select?*?from?admin");
          137 // ????????????while(rs.next()){
          138 // ????????????????System.out.println(rs.getString("adminName"));
          139 // ????????????}
          140 // ????????}?catch?(SQLException?e)?{
          141 // ????????????e.printStackTrace();
          142 // ????????}
          143
          144 ????}

          145
          146 }

          posted on 2006-07-17 11:19 javajohn 閱讀(3842) 評論(1)  編輯  收藏 所屬分類: 數(shù)據(jù)庫

          Feedback

          # re: apache dbcp數(shù)據(jù)庫連接池的使用 2007-04-15 17:11 clover

          很不錯,我正在學(xué)習(xí)數(shù)據(jù)庫連接池,需要參照借鑒你的思想和代碼,非常感謝。希望以后還能看到你的杰作。對我們初學(xué)者很有益。再次表示感謝!  回復(fù)  更多評論   



          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

          My Links

          Blog Stats

          常用鏈接

          留言簿(7)

          隨筆分類(36)

          隨筆檔案(39)

          classmate

          good blog

          企業(yè)管理網(wǎng)站

          好友

          站點收藏

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 米脂县| 咸丰县| 达孜县| 屏边| 桑日县| 凌海市| 宜兴市| 德令哈市| 新乐市| 上犹县| 莆田市| 上虞市| 汕头市| 东乡族自治县| 涞水县| 邵阳市| 绿春县| 汉川市| 满洲里市| 深圳市| 抚宁县| 宜川县| 廉江市| 电白县| 三都| 田东县| 鸡东县| 福海县| 肥东县| 岳阳市| 澳门| 奉贤区| 庄河市| 鱼台县| 平乡县| 孝义市| 扬州市| 基隆市| 河北区| 金山区| 东台市|