丄諦啲仇魜ヤ
          如 果 敵 人 讓 你 生 氣 , 那 說(shuō) 明 你 沒(méi) 有 勝 他 的 把 握!
          posts - 6,comments - 56,trackbacks - 1
          TOMCAT 4.1 與TOMCAT 5.5以上版本(我只用過(guò)5.5 和6.0)配置數(shù)據(jù)源是有區(qū)別的小弟在此寫(xiě)哈
          (記得將驅(qū)動(dòng)包放到common\lib文件夾下)
          TOMCAT 4.1是將Resoucre配置參數(shù)都看做是元素來(lái)配置的
          而TOMCATE5.5以后都是把參數(shù)當(dāng)作Resource元素的屬性來(lái)配置的,
          還要注意的4.1中 的
          <factory>元素在5.5以后就沒(méi)有了。


          》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

          來(lái)看看4.1是如何配置的 在TOMCAT 目錄conf文件夾下找到server.xml再找到</host>元素將下面的代碼寫(xiě)到
          </HOST>元素前面即可:


            <Context path="/mas" docBase="
          D:/web/voteTest/WebRoot" debug="0"
                           reloadable="true" crossContext="true">
                 <Logger className="org.apache.catalina.logger.FileLogger"
                                    timestamp="true"/>
                <Resource name="jdbc/mas" auth="Container" type="javax.sql.DataSource"/>       
                 <ResourceParams name="jdbc/mas">
                  <parameter>//在5.5以后就沒(méi)有這個(gè)元素了如果存在就會(huì)報(bào)錯(cuò)
                    <name>factory</name>
                    <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
                  </parameter>

                   <parameter>
                    <name>testonborrow </name>
                    <value>true</value>
                  </parameter>
                   <parameter>
                    <name>testonreturn  </name>
                    <value>true</value>
                  </parameter>
                   <parameter>
                    <name>testwhileidle</name>
                    <value>true</value>
                  </parameter>
                  <parameter>
                    <name>removeAbandoned</name>
                    <value>true</value>
                  </parameter>
                  <parameter>
                    <name>removeAbandonedTimeout</name>
                    <value>100</value>
                  </parameter>                           
                  <parameter>
                    <name>maxActive</name>
                    <value>100</value>
                  </parameter>
                  <parameter>
                    <name>maxIdle</name>
                    <value>30</value>
                  </parameter>
                  <parameter>
                  <name>maxWait</name>
                  <value>10000</value>
                  </parameter>           
                  <parameter>
                   <name>username</name>
                   <value>wsq</value>
                  </parameter>
                  <parameter>
                   <name>password</name>
                   <value>wsq</value>
                  </parameter>
                 <parameter>
                 <name>driverClassName</name>
                 <value>oracle.jdbc.driver.OracleDriver</value>
                 </parameter>
                  <parameter>
                    <name>url</name>
                    <value>jdbc:oracle:thin:@192.168.3.177:1521:masenv</value>
                  </parameter>
                 </ResourceParams>             
                 </Context>
              

          》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

          tomcat 6.0配置數(shù)據(jù)源

            方法1:
          直接在tomcat server.xml中配置將下面這段話寫(xiě)在serverl.xml 中</host>之前

          <Context path="/mas" docBase="D:/web/voteTest/WebRoot" debug="0"  reloadable="true" crossContext="true">   
                   <Resource name="jdbc/mas"
                       auth="Container"
                       type="javax.sql.DataSource"     
                       driverClassName="oracle.jdbc.driver.OracleDriver"
                       url="jdbc:oracle:thin:@192.168.3.177:1521:masenv"        
                       username="wsq" password="wsq" 
                       maxIdle="20"     maxWait="5000"   
                       maxActive="100"  removeAbandoned="true"
                       removeAbandonedTimeout="60" logAbandoned="true"
                    />
               </Context>

          解釋一下:1.path是指定訪問(wèn)該web應(yīng)用的URL入口;

                   2.docBase指定web應(yīng)用的文件路徑,可以是絕對(duì)路徑,也可以是相對(duì)于Host的appBase屬性的相對(duì)路徑;

                   3.type指定Resource所屬的JAVA類(lèi)名

                   4.maxActive是DBCP中處于活動(dòng)狀態(tài)的數(shù)據(jù)庫(kù)連接的最大數(shù)目,取0表示不受限制

                   5.maxIdle是DBCP中處于空閑狀態(tài)的數(shù)據(jù)庫(kù)連接的最大數(shù)目,取0表示不受限制

                   6.maxWait是是DBCP中的數(shù)據(jù)庫(kù)連接處于空閑狀態(tài)的最長(zhǎng)時(shí)間(以毫秒為單位)取0表示無(wú)限期等待

                   7.username是數(shù)據(jù)庫(kù)登陸名

                   8.password是數(shù)據(jù)庫(kù)登陸口令

                   9.driverClassName是只定數(shù)據(jù)庫(kù)的jdbc驅(qū)動(dòng)程序

                  10.url是指定連接數(shù)據(jù)庫(kù)的URL,masenv是我的數(shù)據(jù)庫(kù)名。



             方法2 :

          如果你是想將你的TOMCAT綁定到你IDE(eclipse)上啟動(dòng)的話 可以采用這個(gè)方法

          方法如下 :

            在你項(xiàng)目WebRoot 文件下面搞個(gè)META-INF文件夾在建個(gè)context.xml文件 在此XML文件里配置數(shù)據(jù)源即可。內(nèi)容如下:

          <?xml version="1.0" encoding="UTF-8"?>
          <Context path="/mas" docBase="D:/web/voteTest/WebRoot" debug="0"  reloadable="true" crossContext="true">   
             <Resource name="jdbc/mas"  auth="Container" type="javax.sql.DataSource"     
                        driverClassName="oracle.jdbc.driver.OracleDriver"
                        url="jdbc:oracle:thin:@192.168.3.177:1521:masenv"        
                        username="wsq" password="wsq"   maxIdle="20" maxWait="5000"   
                        maxActive="100" removeAbandoned="true"
                        removeAbandonedTimeout="60" logAbandoned="true"/>
          </Context>

          說(shuō)明同上

          獲得數(shù)據(jù)源方法

          /**
               * 初始化數(shù)據(jù)庫(kù)連接
               *
               * @throws SQLException
               */
              private static void loadDataSource()
              throws SQLException {
                  try {
                      if (dataSource == null) {
                           Context context = new InitialContext();
                           DataSource dataSource = (DataSource)context.lookup("java:comp/env/jdbc/mas");

                      }              
                  } catch (Exception e) {
                     
                      throw new SQLException("數(shù)據(jù)庫(kù)命名服務(wù)錯(cuò)誤:" + e.getMessage());
                  }
              }    

          /**

               * 取數(shù)據(jù)庫(kù)連接
               *
               * @return Connection
               *
               * @throws SQLException
               */
              public static Connection getConnection() {       
                  Connection conn = null;
                  try {
                      loadDataSource();
                      Connection   conn =  dataSource.getConnection();

                  } catch (SQLException sqle) {
                     
                      throw new RuntimeException(sqle);
                  }   
                  return conn;
              }
           


          posted on 2008-04-24 10:33 Crying 閱讀(1108) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Tomcat

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 海丰县| 黔江区| 邳州市| 溆浦县| 大同县| 吉木乃县| 兰西县| 象山县| 新和县| 宣恩县| 航空| 孝义市| 铜川市| 芦溪县| 新龙县| 曲靖市| 英超| 清徐县| 肥东县| 衡山县| 响水县| 襄城县| 长岛县| 清苑县| 延庆县| 揭西县| 集贤县| 平乐县| 弋阳县| 抚宁县| 竹山县| 河池市| 怀柔区| 那坡县| 甘孜| 商河县| 东莞市| 佛坪县| 宁陕县| 南皮县| 浮山县|