心的方向

          新的征途......
          posts - 75,comments - 19,trackbacks - 0

          1.將數(shù)據(jù)庫(kù)驅(qū)動(dòng)程序的JAR文件放在Tomcat的 common/lib 中

          2.在server.xml中設(shè)置數(shù)據(jù)源,以MySQL數(shù)據(jù)庫(kù)為例,如下:
          在<GlobalNamingResources> </GlobalNamingResources>節(jié)點(diǎn)中加入,
                <Resource
                name="jdbc/DBPool"
                type="javax.sql.DataSource"
                password="root"
                driverClassName="com.mysql.jdbc.Driver"
                maxIdle="2"
                maxWait="5000"
                username="root"
                url="jdbc:mysql://127.0.0.1:3306/test"
                maxActive="4"/>
             屬性說明:name,數(shù)據(jù)源名稱,通常取”jdbc/XXX”的格式;
                      type,”javax.sql.DataSource”;
                      password,數(shù)據(jù)庫(kù)用戶密碼;
                      driveClassName,數(shù)據(jù)庫(kù)驅(qū)動(dòng);
                      maxIdle,最大空閑數(shù),數(shù)據(jù)庫(kù)連接的最大空閑時(shí)間。超過空閑時(shí)間,數(shù)據(jù)庫(kù)連
                               接將被標(biāo)記為不可用,然后被釋放。設(shè)為0表示無限制。
                      MaxActive,連接池的最大數(shù)據(jù)庫(kù)連接數(shù)。設(shè)為0表示無限制。
                      maxWait ,最大建立連接等待時(shí)間。如果超過此時(shí)間將接到異常。設(shè)為-1表示
                               無限制。

          3.在你的web應(yīng)用程序的web.xml中設(shè)置數(shù)據(jù)源參考,如下:
            在<web-app></web-app>節(jié)點(diǎn)中加入,
            <resource-ref>
              <description>MySQL DB Connection Pool</description>
              <res-ref-name>jdbc/DBPool</res-ref-name>
              <res-type>javax.sql.DataSource</res-type>
              <res-auth>Container</res-auth>
              <res-sharing-scope>Shareable</res-sharing-scope>
           </resource-ref>
            子節(jié)點(diǎn)說明: description,描述信息;
                         res-ref-name,參考數(shù)據(jù)源名字,同上一步的屬性name;
                         res-type,資源類型,”javax.sql.DataSource”;
                         res-auth,”Container”;
                         res-sharing-scope,”Shareable”;

          4.在web應(yīng)用程序的context.xml中設(shè)置數(shù)據(jù)源鏈接,如下:
            在<Context></Context>節(jié)點(diǎn)中加入,
            <ResourceLink
             name="jdbc/DBPool" 
             type="javax.sql.DataSource" 
             global="jdbc/DBPool"/>
             屬性說明:name,同第2步和第3步的屬性name值,和子節(jié)點(diǎn)res-ref-name值;
                       type,同樣取”javax.sql.DataSource”;
                       global,同name值。
           
          至此,設(shè)置完成,下面是如何使用數(shù)據(jù)庫(kù)連接池
          1.建立一個(gè)連接池類,DBPool.java,用來創(chuàng)建連接池,代碼如下:
          import javax.naming.Context;
          import javax.naming.InitialContext;
          import javax.naming.NamingException;
          import javax.sql.DataSource;

          public class DBPool {
              private static DataSource pool;
              static {
                   Context env = null;
                    try {
                        env = (Context) new InitialContext().lookup("java:comp/env");
                        pool = (DataSource)env.lookup("jdbc/DBPool");
                        if(pool==null) 
                            System.err.println("'DBPool' is an unknown DataSource");
                         } catch(NamingException ne) {
                            ne.printStackTrace();
                    }
                }
              public static DataSource getPool() {
                  return pool;
              }
          }

          2.在要用到數(shù)據(jù)庫(kù)操作的類或jsp頁(yè)面中,用DBPool.getPool().getConnection(),獲得一個(gè)Connection對(duì)象,就可以進(jìn)行數(shù)據(jù)庫(kù)操作,最后別忘了對(duì)Connection對(duì)象調(diào)用close()方法,注意:這里不會(huì)關(guān)閉這個(gè)Connection,而是將這個(gè)Connection放回?cái)?shù)據(jù)庫(kù)連接池。

          posted on 2008-02-19 15:47 阿偉 閱讀(224) 評(píng)論(0)  編輯  收藏 所屬分類: J2EE
          主站蜘蛛池模板: 三原县| 巢湖市| 叙永县| 光山县| 西丰县| 柳江县| 南部县| 商水县| 焉耆| 义乌市| 社旗县| 高邑县| 庆阳市| 达州市| 桐城市| 木里| 桂林市| 岫岩| 嘉祥县| 宜宾县| 久治县| 湖州市| 广宁县| 石柱| 宜春市| 宜宾县| 盐亭县| 疏勒县| 台南市| 阿瓦提县| 军事| 冀州市| 呼玛县| 万全县| 鲜城| 凤冈县| 文登市| 建湖县| 陕西省| 乃东县| 黄山市|