




下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、這幾天在弄個小東西,要用到數據庫,以前就聽說過數據庫連接池這個概念,所以就打算在這個小東西中加入數據庫連接池。呵呵。從網上搜了一些資料。今天就整理一下。我搜到的設置基本上主要有兩種方法我們以MySQL+TOMCAT為例 1.把DataSource設置到我們的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設置到我們的Tomcat中,下面詳細的介紹下(測試
12、用的JAVA代碼和上面的一樣就不帖出了): 這里我查到的設置方法就有了一點區別了。有的人把DataSource設置在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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年國際金融理財師考試素養試題及答案
- 2025年特許金融分析師模擬考試與解析試題及答案
- 網絡編輯師概念編輯試題及答案
- 理財過程中隱含的風險分析試題及答案
- 2024年小語種能力測試的趨勢試題及答案
- 2025年國際金融理財師考試經驗分享試題及答案
- 2024年畜牧師職稱考試材料準備及試題答案
- 解鎖特許金融分析師考試技巧試題及答案
- 網絡編輯師2024年考試問題解析試題及答案
- 小語種考試知識范圍及試題答案總結
- 頂管工程頂進記錄表
- (中職中專)汽車修理基本技能完整版課件匯總全書電子教案(最新)
- 人員進出潔凈區更衣流程圖
- 林業政策法規考試題庫(含答案)
- 機械、設備掛靠協議范本、合同、合約
- 管理前沿理論試題總結
- 馬坑鐵礦450-200鉬礦床的地下開采方案設計采礦工程專業畢業設計畢業論
- 高三英語教研組建設(課堂PPT)
- 排水管道非開挖預防性修復可行性研究報告
- 讀書知識競賽試題含答案
- 企業全面戰略管理、年度經營計劃、預算管理、績效管理
評論
0/150
提交評論