javajohn

          金色年華

          apache dbcp數據庫連接池的使用

          ?

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

          ??8 ???? private ? static ?PoolingDriver?driver? = ? null ;
          ??9
          ?10 ???? /**
          ?11 ?????*?設置一個數據庫連接池
          ?12 ?????*?
          ?13 ?????*? @param ?name
          ?14 ?????*????????????連接池的名稱
          ?15 ?????*? @param ?url
          ?16 ?????*????????????數據源
          ?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 ?????????????????*?首先創建一個對象池來保存數據庫連接?使用?commons.pool?的?GenericObjectPool對象
          ?25 ????????????????? */

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

          ?30 ????????????????ConnectionFactory?connectionFactory? = ? new ?DriverManagerConnectionFactory(
          ?31 ????????????????????????url,? null );
          ?32 ???????????????? /**
          ?33 ?????????????????*?創建一個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 ?????*?關閉所有數據庫連接池
          ?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 ?????*?取得一個數據庫連接對象。
          ?72 ?????*?
          ?73 ?????*?因為可能使用兩個不同的數據庫,?所以依據report的值來確定使用那個數據庫。
          ?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驅動
          ?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( " 無法裝入數據庫引擎 " );
          ?94 ????????}
          ? catch ?(SQLException?sqle)? {
          ?95 ???????????? throw ? new ?RuntimeException( " 無法打開數據庫連接 " );
          ?96 ????????}

          ?97 ????}

          ?98
          ?99 ???? /**
          100 ?????*?執行清理過程
          101 ?????*?<li>關閉數據庫連接</li>
          102 ?????*?<li>關閉語句對象</li>
          103 ?????*?<li>關閉結果集</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)  編輯  收藏 所屬分類: 數據庫

          Feedback

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

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


          My Links

          Blog Stats

          常用鏈接

          留言簿(7)

          隨筆分類(36)

          隨筆檔案(39)

          classmate

          good blog

          企業管理網站

          好友

          站點收藏

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 凉山| 昌图县| 德昌县| 武山县| 平顺县| 阿拉尔市| 溧水县| 朝阳区| 安阳市| 濮阳县| 镇坪县| 油尖旺区| 桐庐县| 渑池县| 凉城县| 乌兰浩特市| 博白县| 唐山市| 游戏| 四子王旗| 潜江市| 利辛县| 南平市| 东宁县| 鄯善县| 新昌县| 望都县| 江川县| 古田县| 巩留县| 商丘市| 芷江| 林州市| 中卫市| 满洲里市| 邛崃市| 天津市| 临安市| 姜堰市| 兴宁市| 兴文县|