• <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)

            亚洲精品99久久久久中文字幕| 久久亚洲精品无码VA大香大香| 国产成人无码精品久久久免费| 久久精品国产只有精品66| 欧美精品国产综合久久| 成人妇女免费播放久久久| 久久久久久国产精品无码下载| 伊人久久久AV老熟妇色| 伊人色综合久久天天| 日韩精品久久久久久久电影| 国产精品久久久久影院嫩草| 久久久久久久综合狠狠综合| 久久91综合国产91久久精品| 国产毛片欧美毛片久久久| 99久久伊人精品综合观看| 亚洲国产精品无码久久SM| 久久精品18| 91精品免费久久久久久久久| 亚洲狠狠婷婷综合久久久久| 久久天天躁狠狠躁夜夜2020老熟妇 | 97视频久久久| 久久精品国产72国产精福利| 91精品国产综合久久久久久| 国内精品久久久久影院薰衣草| 久久久久99精品成人片| 亚洲嫩草影院久久精品| 精品久久久久久成人AV| 亚洲va久久久噜噜噜久久| 亚洲欧美久久久久9999| 久久精品一区二区影院| 精品久久人人爽天天玩人人妻| 久久精品无码专区免费东京热 | 久久精品人人做人人妻人人玩| 狠狠色综合网站久久久久久久高清 | 97香蕉久久夜色精品国产| 合区精品久久久中文字幕一区| 久久黄色视频| 亚洲国产成人久久精品99| 欧美精品一区二区久久| 久久综合亚洲色一区二区三区| 少妇无套内谢久久久久|