• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            woaidongmao

            文章均收錄自他人博客,但不喜標(biāo)題前加-[轉(zhuǎn)貼],因其丑陋,見(jiàn)諒!~
            隨筆 - 1469, 文章 - 0, 評(píng)論 - 661, 引用 - 0
            數(shù)據(jù)加載中……

            TOMCAT數(shù)據(jù)庫(kù)連接池的配置方法總結(jié)(待續(xù))

            這幾天在弄個(gè)小東西,要用到數(shù)據(jù)庫(kù),以前就聽(tīng)說(shuō)過(guò)數(shù)據(jù)庫(kù)連接池這個(gè)概念,所以就打算在這個(gè)小東西中加入數(shù)據(jù)庫(kù)連接池。呵呵。從網(wǎng)上搜了一些資料。今天就整理一下。我搜到的設(shè)置基本上主要有兩種方法我們以MySQL+TOMCAT為例
            1.
            DataSource設(shè)置到我們的WEB項(xiàng)目中,下面詳細(xì)的介紹下:
            第一步:在我們的WEB項(xiàng)目中的META-INF文件夾下建立一個(gè)context.xml

            Xml代碼 clip_image001

            1. <?xml version='1.0' encoding='utf-8'?> 
            2.  
            3. <Context> 
            4.  
            5.     <Resource name="jdbc/mysql"     
            6.        auth="Container"     
            7.        type="javax.sql.DataSource"     
            8.        driverClassName="com.mysql.jdbc.Driver"     
            9.        url="jdbc:mysql://localhost/bbs"     
            10.        username="root"     
            11.        password="root"     
            12.        maxActive="50"     
            13.        maxIdle="20"     
            14.        maxWait="10000" />     
            15.  
            16. </Context> 

            <?xml version='1.0' encoding='utf-8'?>

             

            <Context>

             

                <Resource name="jdbc/mysql"  

                   auth="Container"  

                   type="javax.sql.DataSource"  

                   driverClassName="com.mysql.jdbc.Driver"  

                   url="jdbc:mysql://localhost/bbs"  

                   username="root"  

                   password="root"  

                   maxActive="50"  

                   maxIdle="20"  

                   maxWait="10000" />  

             

            </Context>


            第二步:在我們的WEB項(xiàng)目下的WEB-INF文件夾下建立一個(gè)web.xml(如果存在了就不用了,直接修改就行了)
            (
            這幾天測(cè)試了一下,不做這步也可以,O(∩_∩)O哈哈~省事了)

            Xml代碼 clip_image001

            1. <resource-ref> 
            2.     <description>DB Connection</description> 
            3.     <res-ref-name>jdbc/mysql</res-ref-name> 
            4.     <res-type>javax.sql.DataSource</res-type> 
            5.     <res-auth>Container</res-auth> 
            6. </resource-ref> 

              <resource-ref>

                  <description>DB Connection</description>

                  <res-ref-name>jdbc/mysql</res-ref-name>

                  <res-type>javax.sql.DataSource</res-type>

                  <res-auth>Container</res-auth>

              </resource-ref>


            第三步:我們就可以用代碼來(lái)獲取Connection對(duì)象了

            Java代碼 clip_image001

            1. package xushun.util;  
            2.  
            3. import java.sql.*;  
            4. import javax.sql.*;  
            5. import javax.naming.*;  
            6.  
            7. public class DBHelper {  
            8.       
            9.     public static Connection getConnection() throws SQLException,NamingException  
            10.     {  
            11.         // 初始化查找命名空間  
            12.         Context initContext = new InitialContext();  
            13.         Context envContext = (Context)initContext.lookup("java:/comp/env");  
            14.         // 找到DataSource  
            15.         DataSource ds = (DataSource)envContext.lookup("jdbc/mysql");  
            16.         return ds.getConnection();  
            17.     }  

            package xushun.util;

             

            import java.sql.*;

            import javax.sql.*;

            import javax.naming.*;

             

            public class DBHelper {

               

                public static Connection getConnection() throws SQLException,NamingException

                {

                    // 初始化查找命名空間

                    Context initContext = new InitialContext();

                    Context envContext = (Context)initContext.lookup("java:/comp/env");

                    // 找到DataSource

                    DataSource ds = (DataSource)envContext.lookup("jdbc/mysql");

                    return ds.getConnection();

                }

            }


            2.
            DataSource設(shè)置到我們的Tomcat中,下面詳細(xì)的介紹下(測(cè)試用的JAVA代碼和上面的一樣就不帖出了):
            這里我查到的設(shè)置方法就有了一點(diǎn)區(qū)別了。有的人把DataSource設(shè)置在Tomcatserver.xml文件的GlobalNamingResources下面,然后在context.xml中去映射。有的直接就寫(xiě)在context.xml中了
            先說(shuō)下在server.xml添加DataSource
            第一步:在Tomcatconf中的server.xml文件中找到

            Xml代碼 clip_image001

            1. <GlobalNamingResources> 
            2.   <!-- Editable user database that can also be used by  
            3.        UserDatabaseRealm to authenticate users  
            4.   --> 
            5.   <Resource name="UserDatabase" auth="Container" 
            6.             type="org.apache.catalina.UserDatabase" 
            7.             description="User database that can be updated and saved" 
            8.             factory="org.apache.catalina.users.MemoryUserDatabaseFactory" 
            9.             pathname="conf/tomcat-users.xml" /> 
            10. </GlobalNamingResources> 

              <GlobalNamingResources>

                <!-- Editable user database that can also be used by

                     UserDatabaseRealm to authenticate users

                -->

                <Resource name="UserDatabase" auth="Container"

                          type="org.apache.catalina.UserDatabase"

                          description="User database that can be updated and saved"

                          factory="org.apache.catalina.users.MemoryUserDatabaseFactory"

                          pathname="conf/tomcat-users.xml" />

              </GlobalNamingResources>

            修改為

            Xml代碼 clip_image001

            1. <GlobalNamingResources> 
            2.   <!-- Editable user database that can also be used by  
            3.        UserDatabaseRealm to authenticate users  
            4.   --> 
            5.   <Resource name="UserDatabase" auth="Container" 
            6.             type="org.apache.catalina.UserDatabase" 
            7.             description="User database that can be updated and saved" 
            8.             factory="org.apache.catalina.users.MemoryUserDatabaseFactory" 
            9.             pathname="conf/tomcat-users.xml" /> 
            10.   <Resource name="jdbc/bbs"       
            11.          auth="Container" type="javax.sql.DataSource" 
            12.          driverClassName="com.mysql.jdbc.Driver" 
            13.          maxIdle="20" 
            14.          maxWait="5000" 
            15.          username="root" 
            16.          password="admin" 
            17.          url="jdbc:mysql://localhost:3306/bbs"       
            18.          maxActive="100"   
            19.          removeAbandoned="true" 
            20.          removeAbandonedTimeout="60" 
            21.          logAbandoned="true"/> 
            22. </GlobalNamingResources> 

              <GlobalNamingResources>

                <!-- Editable user database that can also be used by

                     UserDatabaseRealm to authenticate users

                -->

                <Resource name="UserDatabase" auth="Container"

                          type="org.apache.catalina.UserDatabase"

                          description="User database that can be updated and saved"

                          factory="org.apache.catalina.users.MemoryUserDatabaseFactory"

                          pathname="conf/tomcat-users.xml" />

                <Resource name="jdbc/bbs"    

                        auth="Container" type="javax.sql.DataSource"

                        driverClassName="com.mysql.jdbc.Driver"

                        maxIdle="20"

                        maxWait="5000"

                        username="root"

                        password="admin"

                        url="jdbc:mysql://localhost:3306/bbs"    

                        maxActive="100"

                        removeAbandoned="true"

                        removeAbandonedTimeout="60"

                        logAbandoned="true"/>

              </GlobalNamingResources>


            第二步:在Tomcatconf文件夾下的context.xml中加入

            Xml代碼 clip_image001

            1. <ResourceLink name="jdbc/bbs" global="jdbc/bbs" type="javax.sql.DataSource"/> 

            <ResourceLink name="jdbc/bbs" global="jdbc/bbs" type="javax.sql.DataSource"/>


            第三步:就是在WEB項(xiàng)目的WEB-INF中的web.xml添加

            Xml代碼 clip_image001

            1. <resource-ref> 
            2.     <description>DB Connection</description> 
            3.     <res-ref-name>jdbc/mysql</res-ref-name> 
            4.     <res-type>javax.sql.DataSource</res-type> 
            5.     <res-auth>Container</res-auth> 
            6. </resource-ref> 

              <resource-ref>

                  <description>DB Connection</description>

                  <res-ref-name>jdbc/mysql</res-ref-name>

                  <res-type>javax.sql.DataSource</res-type>

                  <res-auth>Container</res-auth>

              </resource-ref>


            還有就是在Tomcat文檔中提到的方法,直接修改context.xml文件了
            Tomcatconf文件夾下的context.xml中加入

            Xml代碼 clip_image001

            1. <Resource name="jdbc/bbs"       
            2.               auth="Container" type="javax.sql.DataSource" 
            3.               driverClassName="com.mysql.jdbc.Driver" 
            4.               maxIdle="20" 
            5.               maxWait="5000" 
            6.               username="root" 
            7.               password="admin" 
            8.               url="jdbc:mysql://localhost:3306/bbs"       
            9.               maxActive="100"   
            10.               removeAbandoned="true" 
            11.               removeAbandonedTimeout="60" 
            12.               logAbandoned="true"/> 

            <Resource name="jdbc/bbs"    

                        auth="Container" type="javax.sql.DataSource"

                        driverClassName="com.mysql.jdbc.Driver"

                        maxIdle="20"

                        maxWait="5000"

                        username="root"

                        password="admin"

                        url="jdbc:mysql://localhost:3306/bbs"    

                        maxActive="100"

                        removeAbandoned="true"

                        removeAbandonedTimeout="60"

                        logAbandoned="true"/>

            然后就是在WEB項(xiàng)目的WEB-INF中的web.xml添加

            Xml代碼 clip_image001

            1. <resource-ref> 
            2.     <description>DB Connection</description> 
            3.     <res-ref-name>jdbc/mysql</res-ref-name> 
            4.     <res-type>javax.sql.DataSource</res-type> 
            5.     <res-auth>Container</res-auth> 
            6. </resource-ref> 

              <resource-ref>

                  <description>DB Connection</description>

                  <res-ref-name>jdbc/mysql</res-ref-name>

                  <res-type>javax.sql.DataSource</res-type>

                  <res-auth>Container</res-auth>

              </resource-ref>


            就是這些了,如果有什么不太清楚的就留言,一起研究下。等以后我在搜集下資料整理出上面用到的XML文件中各個(gè)標(biāo)簽的屬性及其代表的意思。有興趣的也可以自己先查下。:-)

            posted on 2009-08-03 18:02 肥仔 閱讀(1374) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Web-后臺(tái)

            国产A级毛片久久久精品毛片| 日本精品久久久久中文字幕8| 亚洲国产视频久久| 伊人色综合久久天天人手人婷| 日韩av无码久久精品免费| 国产99久久久国产精品~~牛 | 亚洲精品乱码久久久久久按摩| 久久精品国产欧美日韩99热| 久久久亚洲欧洲日产国码二区 | 麻豆一区二区99久久久久| 久久久久国产一级毛片高清版| 久久久久99精品成人片牛牛影视| 99久久无色码中文字幕人妻| 国产精品综合久久第一页 | 久久久久综合网久久| 中文字幕久久亚洲一区| 欧美伊香蕉久久综合类网站| 精品人妻伦九区久久AAA片69| 国产综合免费精品久久久| 国产精品一区二区久久不卡| 欧美久久久久久| 亚洲乱码日产精品a级毛片久久 | 亚洲色大成网站www久久九 | 无码国内精品久久人妻麻豆按摩| 国产亚洲综合久久系列| 一本一本久久a久久综合精品蜜桃| 激情久久久久久久久久| 日韩精品久久久久久| 久久人爽人人爽人人片AV| 久久久无码精品亚洲日韩京东传媒| 99久久精品免费国产大片| 青青草国产成人久久91网| 97久久精品人妻人人搡人人玩| 亚洲AV无码1区2区久久| 国内高清久久久久久| 国产亚洲精久久久久久无码77777 国产亚洲精品久久久久秋霞 | 久久久久久亚洲精品影院| 午夜精品久久影院蜜桃| 怡红院日本一道日本久久 | 久久人人爽人人澡人人高潮AV| 国产综合精品久久亚洲|