<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    從心開始

     

    DBCP數(shù)據(jù)庫連接池

    DBCP是Apache的一個開源項目:
    commons.dbcp.jar
    http://jakarta.apache.org/commons/dbcp/index.html

    DBCP依賴Apache的另外2個開源項目
    commons.collections.jar和commons.pool.jar

    下載這些包并將這些包的路徑添加到classpath中就可以使用dbcp做為項目中的數(shù)據(jù)庫連接池使用了。

    以下是我的連接池

    java 代碼
    1. package ??selfservice;????
      ???
      import ??java.io.FileNotFoundException;????
      import ??java.io.IOException;????
      import ??java.sql.Connection;????
      import ??java.sql.DriverManager;????
      import ??java.sql.ResultSet;????
      import ??java.sql.SQLException;????
      import ??java.sql.Statement;????
      ???
      import ??org.apache.commons.dbcp.ConnectionFactory;????
      import ??org.apache.commons.dbcp.DriverManagerConnectionFactory;????
      import ??org.apache.commons.dbcp.PoolableConnectionFactory;????
      import ??org.apache.commons.dbcp.PoolingDriver;????
      import ??org.apache.commons.pool.ObjectPool;????
      import ??org.apache.commons.pool.impl.GenericObjectPool;????
      ???
      ???
      public ??? class ??PoolManager? {??????
      ?????
      private ??? static ??String?????
      ????????????driver
      = ? " oracle.jdbc.driver.OracleDriver " ?,? // 驅(qū)動????
      ????????????url? = ?? " jdbc:oracle:thin:@192.168.0.40:1521:drcom " ?,? // URL????
      ????????????Name = ? " drcom " ?,? // 用戶名????
      ????????????Password = ? " drcom " ?;? // 密碼????
      ????????
      ?????
      private ??? static ??Class?driverClass? = ?? null ?;????
      ?????
      private ??? static ??ObjectPool?connectionPool? = ?? null ?;????
      ???
      ?????
      public ??PoolManager() {???????????
      ????}
      ???????
      ????????
      ?????
      /** ???
      ?????*?裝配配置文件???
      ?????*?initProperties???????
      ?????
      */
      ????
      ?????
      private ??? static ??? void ??loadProperties() {????
      ?????????
      try ?? {????
      ????????????java.io.InputStream?stream?
      = ?? new ??java.io.FileInputStream(? " config.properties " ?);????
      ????????????java.util.Properties?props?
      = ?? new ??java.util.Properties();????
      ????????????props.load(stream);????
      ????????????????
      ????????????driver?
      = ?props.getProperty(? " ORACLE_DRIVER " ?);????
      ????????????url?
      = ?props.getProperty(? " ORACLE_URL " ?);????
      ????????????Name?
      = ?props.getProperty(? " ORACLE_LOGIN_NAME " ?);????
      ????????????Password?
      = ?props.getProperty(? " ORACLE_LOGIN_PASSWORD " ?);????
      ????????????????
      ????????}
      ?? catch ??(FileNotFoundException?e)? {????
      ????????????System.out.println(?
      " 讀取配置文件異常 " ?);?????????????
      ????????}
      ?? catch ?(IOException?ie) {????
      ????????????System.out.println(?
      " 讀取配置文件時IO異常 " ?);????
      ????????}
      ????
      ????}
      ????
      ????????
      ?????
      /** ????
      ?????*?初始化數(shù)據(jù)源????
      ?????
      */
      ??????
      ?????
      private ??? static ??? synchronized ??? void ??initDataSource()? {?????????????
      ?????????
      if ??(driverClass? == ?? null ?)? {?????
      ?????????????
      try ?? {?????
      ????????????????driverClass?
      = ?Class.forName(driver);?????
      ????????????}
      ?? catch ??(ClassNotFoundException?e)? {?????
      ????????????????e.printStackTrace();????
      ????????????}
      ?????
      ????????}
      ?????
      ????}
      ?????
      ?????
      ?????
      /** ????
      ?????*?連接池啟動????
      ?????*?
      @throws ?Exception????
      ?????
      */
      ??????
      ?????
      public ??? static ??? void ??StartPool()? {????
      ????????loadProperties();????
      ????????initDataSource();?????
      ?????????
      if ??(connectionPool? != ?? null ?)? {?????
      ????????????ShutdownPool();?????
      ????????}
      ??????
      ?????????
      try ?? {?????
      ????????????connectionPool?
      = ?? new ??GenericObjectPool(? null ?);?????
      ????????????ConnectionFactory?connectionFactory?
      = ?? new ??DriverManagerConnectionFactory(url,?Name,?Password);?????
      ????????????PoolableConnectionFactory?poolableConnectionFactory?
      = ?? new ??PoolableConnectionFactory(connectionFactory,?connectionPool,?? null ?,?? null ?,?? false ?,?? true ?);?????
      ????????????Class.forName(?
      " org.apache.commons.dbcp.PoolingDriver " ?);?????
      ????????????PoolingDriver?driver?
      = ?(PoolingDriver)?DriverManager.getDriver(? " jdbc:apache:commons:dbcp: " ?);?????
      ????????????driver.registerPool(?
      " dbpool " ?,?connectionPool);??????????????????
      ????????????System.out.println(?
      " 裝配連接池OK " ?);?????
      ????????}
      ?? catch ??(Exception?e)? {?????
      ????????????e.printStackTrace();????
      ????????}
      ?????
      ????}
      ?????
      ?????
      ?????
      /** ????
      ?????*?釋放連接池????
      ?????
      */
      ??????
      ?????
      public ??? static ??? void ??ShutdownPool()? {?????
      ?????????
      try ?? {?????
      ????????????PoolingDriver?driver?
      = ?(PoolingDriver)?DriverManager.getDriver(? " jdbc:apache:commons:dbcp: " ?);?????
      ????????????driver.closePool(?
      " dbpool " ?);????
      ????????}
      ?? catch ??(SQLException?e)? {?????
      ????????????e.printStackTrace();????
      ????????}
      ?????
      ????}
      ?????????
      ?????
      ?????
      /** ????
      ?????*?取得連接池中的連接????
      ?????*?
      @return ????
      ?????
      */
      ??????
      ?????
      public ??? static ??Connection?getConnection()? {?????
      ????????Connection?conn?
      = ?? null ?;?????
      ?????????
      if ?(connectionPool? == ?? null ?)?????
      ????????????StartPool();?????
      ?????????
      try ?? {?????
      ????????????conn?
      = ?DriverManager.getConnection(? " jdbc:apache:commons:dbcp:dbpool " ?);?????
      ????????}
      ?? catch ??(SQLException?e)? {?????
      ????????????e.printStackTrace();????
      ????????}
      ?????
      ?????????
      return ??conn;?????
      ????}
      ?????
      ????????
      ?????
      /** ???
      ?????*?獲取連接???
      ?????*?getConnection???
      ?????*?
      @param ?name???
      ?????*?
      @return ???
      ?????
      */
      ????
      ?????
      public ??? static ??Connection?getConnection(String?name) {????
      ?????????
      return ??getConnection();????
      ????}
      ????
      ?????
      /** ???
      ?????*?釋放連接???
      ?????*?freeConnection???
      ?????*?
      @param ?conn???
      ?????
      */
      ????
      ?????
      public ??? static ??? void ??freeConnection(Connection?conn) {????
      ?????????
      if ?(conn? != ?? null ?) {????
      ?????????????
      try ?? {????
      ????????????????conn.close();????
      ????????????}
      ?? catch ??(SQLException?e)? {??????????????????
      ????????????????e.printStackTrace();????
      ????????????}
      ????
      ????????}
      ????
      ????}
      ????
      ?????
      /** ???
      ?????*?釋放連接???
      ?????*?freeConnection???
      ?????*?
      @param ?name???
      ?????*?
      @param ?con???
      ?????
      */
      ????
      ?????
      public ??? static ??? void ??freeConnection?(String?name,Connection?con) {????
      ????????freeConnection(con);????
      ????}
      ????
      ????????
      ?????
      /** ???
      ?????*?例子???
      ?????*?main???
      ?????*?
      @param ?args???
      ?????
      */
      ????
      ?????
      public ??? static ??? void ??main(String[]?args)? {????????????
      ?????????
      try ?? {????
      ????????????Connection?conn?
      = ?PoolManager.getConnection();????
      ?????????????
      if ?(conn? != ?? null ?) {????
      ????????????????Statement?statement?
      = ?conn.createStatement();????
      ????????????????ResultSet?rs?
      = ?statement.executeQuery(? " select?*?from?tblgxinterface " ?);????
      ?????????????????
      int ??c? = ?rs.getMetaData().getColumnCount();????
      ?????????????????
      while ?(rs.next()) {???????????????????????
      ????????????????????System.out.println();????
      ?????????????????????
      for ?(? int ??i = ? 1 ?;i <= c;i ++ ) {????
      ????????????????????????System.out.print(rs.getObject(i));????
      ????????????????????}
      ????
      ????????????????}
      ????
      ????????????????rs.close();????
      ????????????}
      ????
      ????????????PoolManager.freeConnection(conn);????
      ????????}
      ?? catch ??(SQLException?e)? {??????????????
      ????????????e.printStackTrace();????
      ????????}
      ????
      ???
      ????}
      ????
      ???
      }
      ???
      ?

    posted on 2007-11-05 18:15 飄雪 閱讀(1560) 評論(0)  編輯  收藏 所屬分類: JAVA技術(shù)

    導航

    統(tǒng)計

    常用鏈接

    留言簿(1)

    隨筆分類(11)

    隨筆檔案(13)

    收藏夾

    firends

    搜索

    最新評論

    • 1.?re: udp及tcp穿越NAT
    • 您上述提到的是互聯(lián)網(wǎng)之間的公網(wǎng)與私網(wǎng)之間的NAT穿越,3g終端可以通過這種方式實現(xiàn)嗎?還有3g移動設(shè)備的IP是動態(tài)分配的,我怎么才能在公網(wǎng)服務(wù)器找到這個3G終端?
    • --svurm
    • 2.?re: udp及tcp穿越NAT
    • TCP穿越針對的是公網(wǎng)IP,而這個公網(wǎng)ip進過幾個NAT,多少層映射到局域網(wǎng)客戶端上對大洞無影響,因為這些映射是nat完成的,一層,二層,三層,最終都映射到公網(wǎng)ip上,所以幾層NAT對打洞并無影響。
    • --lch
    • 3.?re: udp及tcp穿越NAT
    • 您好,感謝您提供的好介紹。請問:如果P2P的兩點之間,存在3-4個NAT,P2P也可以通起來嗎?從您對NAT的理解,如果通信兩端之間存在4個NAT,對那些應(yīng)用有影響?
    • --xujf
    • 4.?re: 系統(tǒng)時間修改方法
    • good
    • --jone
    • 5.?re: udp及tcp穿越NAT
    • 評論內(nèi)容較長,點擊標題查看
    • --...

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲综合久久综合激情久久| 亚洲国产日韩综合久久精品| 久久ww精品w免费人成| 亚洲国产精品成人精品小说| 免费国产小视频在线观看| 国产日韩久久免费影院| 亚洲老熟女@TubeumTV| 暖暖在线日本免费中文| 久久www免费人成精品香蕉| 亚洲伊人精品综合在合线| 亚洲乱码中文字幕手机在线| 在线免费观看国产| 粉色视频成年免费人15次| 亚洲成色在线影院| 免费国产小视频在线观看| 免费黄色福利视频| 国产黄色免费观看| 亚洲狠狠成人综合网| 久久久无码精品亚洲日韩软件| 亚洲高清中文字幕免费| 精品无码一级毛片免费视频观看| 亚洲一级毛片在线播放| 久久精品国产精品亚洲精品| 处破痛哭A√18成年片免费| 性无码免费一区二区三区在线| 国产AV无码专区亚洲AV麻豆丫| 亚洲福利视频一区二区三区| 亚洲精品亚洲人成在线观看下载 | 亚洲sss综合天堂久久久| 亚洲人成网7777777国产| 看全色黄大色大片免费久久| 美丽姑娘免费观看在线观看中文版| 一级毛片免费不卡| 亚洲熟妇自偷自拍另欧美| 久久国产亚洲高清观看| 国产av无码专区亚洲av果冻传媒| 免费看大黄高清网站视频在线| 日本人的色道免费网站| 东方aⅴ免费观看久久av| 一级毛片在播放免费| 日韩久久无码免费毛片软件|