




下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 油田開發項目資金申請報告(范文參考)
- 汽車配套產業基地項目投標書(參考模板)
- xx片區城鄉供水一體化項目投標書
- 《GB41930-2022低水平放射性廢物包特性鑒定水泥固化體》深度解析
- 四川省遂寧市2024-2025學年高一下學期期末考試歷史試卷
- 2025年汽車儀表相關計數儀表項目合作計劃書
- 2025年醫療物聯網技術在患者生命體征監測中的應用前景報告
- 2025健身房租賃合同
- 教育技術的倫理準則與實踐探索
- 航空發動機維修技術創新在成本控制中的應用與優化策略報告
- 生產現場變化點管理行動指南
- 中國古典小說巔峰:四大名著鑒賞學習通課后章節答案期末考試題庫2023年
- 模擬電子技術基礎知到章節答案智慧樹2023年蘭州石化職業技術大學
- JJF 1915-2021傾角儀校準規范
- GA/T 1310-2016法庭科學筆跡鑒定意見規范
- 2023年本科招生考試
- 新入職護士培訓考試試題及答案
- 《消防安全技術實務》課本完整版
- 北師大版七年級數學下冊 與信息技術相融合的數學教學案例 教案
- 鈍針穿刺法臨床應用護理
- 水產養殖行業報告
評論
0/150
提交評論