TOMCAT數(shù)據(jù)庫連接池的配置方法總結(jié)_第1頁
TOMCAT數(shù)據(jù)庫連接池的配置方法總結(jié)_第2頁
TOMCAT數(shù)據(jù)庫連接池的配置方法總結(jié)_第3頁
TOMCAT數(shù)據(jù)庫連接池的配置方法總結(jié)_第4頁
TOMCAT數(shù)據(jù)庫連接池的配置方法總結(jié)_第5頁
全文預覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、這幾天在弄個小東西,要用到數(shù)據(jù)庫,以前就聽說過數(shù)據(jù)庫連接池這個概念,所以就打算在這個小東西中加入數(shù)據(jù)庫連接池。呵呵。從網(wǎng)上搜了一些資料。今天就整理一下。我搜到的設(shè)置基本上主要有兩種方法我們以MySQL+TOMCAT為例 1.把DataSource設(shè)置到我們的WEB項目中,下面詳細的介紹下: 第一步:在我們的WEB項目中的META-INF文件夾下建立一個context.xml Xml代碼 <?xml version='1.0' encoding='utf-8'?> <Context> <Resource name="jdbc

2、/mysql" auth="Container" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql:/localhost:3306/dch" username="root" password="root" maxActive="50" maxIdle="20" maxWait="10000"

3、 /> </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" use

4、rname="root" password="root" maxActive="50" maxIdle="20" maxWait="10000" /> </Context>第二步:在我們的WEB項目下的WEB-INF文件夾下建立一個web.xml(如果存在了就不用了,直接修改就行了) (這幾天測試了一下,不做這步也可以,O(_)O哈哈省事了) Xml代碼 1. <resource-ref>  2.     

5、<description>DB Connection</description>  3.     <res-ref-name>jdbc/mysql</res-ref-name>  4.     <res-type>javax.sql.DataSource</res-type>  5.     <res-auth>Contai

6、ner</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> </res

7、ource-ref>第三步:我們就可以用代碼來獲取Connection對象了 <%page contentType="text/html;charset=utf-8" %><% page import ="java.sql.*"%><% page import = "javax.sql.*"%><% page import ="javax.naming.*"%><html><head><title></title>

8、</head><body><%DataSource ds = null; Context env = null; Connection conn = null; Statement stmt=null;ResultSet rs=null;String sql="select * from person"try System.out.println (System.getProperty("java.naming.factory.initial"); env = (Context) new InitialContext().l

9、ookup("java:comp/env"); ds = (DataSource) env.lookup("jdbc/mysql"); conn = ds.getConnection(); stmt=conn.createStatement(); rs=stmt.executeQuery(sql); while(rs.next() System.out.println("ID>>"+rs.getInt("id")+" name>>"+rs.getString("

10、name")+" password>>"+rs.getString("password"); catch (Exception e) e.printStackTrace(); %></body>package xushun.util;import java.sql.*;import javax.sql.*;import javax.naming.*;public class DBHelper public static Connection getConnection() throws SQLException,N

11、amingException / 初始化查找命名空間 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中,下面詳細的介紹下(測試

12、用的JAVA代碼和上面的一樣就不帖出了): 這里我查到的設(shè)置方法就有了一點區(qū)別了。有的人把DataSource設(shè)置在Tomcat的server.xml文件的GlobalNamingResources下面,然后在context.xml中去映射。有的直接就寫在context.xml中了 先說下在server.xml添加DataSource 第一步:在Tomcat的conf中的server.xml文件中找到 Xml代碼 1. <GlobalNamingResources>  2.   <!- Editable user&#

13、160;database that can also be used by   3.        UserDatabaseRealm to authenticate users   4.   ->  5.   <Resource name="UserDatabase" auth=&qu

14、ot;Container"  6.             type="erDatabase"  7.             description="User database that can be updated 

15、;and saved"  8.             factory="ers.MemoryUserDatabaseFactory"  9.             pathname="conf/tomcat-users.xml" />

16、60; 10. </GlobalNamingResources>   <GlobalNamingResources> <!- Editable user database that can also be used by UserDatabaseRealm to authenticate users -> <Resource name="UserDatabase" auth="Container" type="erDatabase" description=&qu

17、ot;User database that can be updated and saved" factory="ers.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources>修改為 Xml代碼 1. <GlobalNamingResources>  2.   <!- Editable user database 

18、;that can also be used by   3.        UserDatabaseRealm to authenticate users   4.   ->  5.   <Resource name="UserDatabase" auth="Container"

19、;  6.             type="erDatabase"  7.             description="User database that can be updated and saved&q

20、uot;  8.             factory="ers.MemoryUserDatabaseFactory"  9.             pathname="conf/tomcat-users.xml" />  10. 

21、0; <Resource name="jdbc/bbs"        11.          auth="Container" type="javax.sql.DataSource"  12.          driverCla

22、ssName="com.mysql.jdbc.Driver"  13.          maxIdle="20"  14.          maxWait="5000"  15.          user

23、name="root"  16.          password="admin"  17.          url="jdbc:mysql:/localhost:3306/bbs"        18.    &#

24、160;     maxActive="100"    19.          removeAbandoned="true"  20.          removeAbandonedTimeout="60"  21.  

25、60;       logAbandoned="true"/>  22. </GlobalNamingResources>   <GlobalNamingResources> <!- Editable user database that can also be used by UserDatabaseRealm to authenticate users -> <Resource name="UserDat

26、abase" auth="Container" type="erDatabase" description="User database that can be updated and saved" factory="ers.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> <Resource name="jdbc/bbs" auth="Container"

27、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" remov

28、eAbandonedTimeout="60" logAbandoned="true"/> </GlobalNamingResources>第二步:在Tomcat的conf文件夾下的context.xml中加入 Xml代碼 1. <ResourceLink name="jdbc/bbs" global="jdbc/bbs" type="javax.sql.DataSource"/>  <ResourceLink

29、name="jdbc/bbs" global="jdbc/bbs" type="javax.sql.DataSource"/>第三步:就是在WEB項目的WEB-INF中的web.xml添加 Xml代碼 1. <resource-ref>  2.     <description>DB Connection</description>  3.     <res-

30、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> <descriptio

31、n>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文件了 在Tomcat的conf文件夾下的context.xml中加入 Xml代碼 1. <Resourc

32、e name="jdbc/bbs"        2.               auth="Container" type="javax.sql.DataSource"  3.          

33、     driverClassName="com.mysql.jdbc.Driver"  4.               maxIdle="20"  5.               m

34、axWait="5000"  6.               username="root"  7.               password="admin"  8.   &

35、#160;           url="jdbc:mysql:/localhost:3306/bbs"        9.               maxActive="100"    10.  

36、;             removeAbandoned="true"  11.               removeAbandonedTimeout="60"  12.       

37、        logAbandoned="true"/>  <Resource name="jdbc/bbs" auth="Container" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" maxIdle="20" maxWait="5000" user

38、name="root" password="admin" url="jdbc:mysql:/localhost:3306/bbs" maxActive="100" removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true"/>然后就是在WEB項目的WEB-INF中的web.xml添加 Xml代碼 1. <resource-ref>  2.     <description>DB Connection</description>  3.     <res-ref-name>jdbc/mysql</res-ref-name>  4.    &

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論