




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、這幾天在弄個小東西,要用到數(shù)據(jù)庫,以前就聽說過數(shù)據(jù)庫連接池這個概念,所以就打算在這個小東西中加入數(shù)據(jù)庫連接池。呵呵。從網(wǎng)上搜了一些資料。今天就整理一下。我搜到的設(shè)置基本上主要有兩種方法我們以MySQL+TOMCAT為例 1.把DataSource設(shè)置到我們的WEB項目中,下面詳細(xì)的介紹下: 第一步:在我們的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中,下面詳細(xì)的介紹下(測試
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)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 農(nóng)業(yè)生物技術(shù)在種業(yè)創(chuàng)新中的基因編輯與種子抗蟲性育種研究進(jìn)展報告001
- 南昌市建設(shè)投資集團(tuán)有限公司2025年6月招聘31人筆試參考題庫附帶答案詳解版
- 備戰(zhàn)高一高二高三高考?xì)v史臨考題號押題-押第10題社會主義現(xiàn)代化建設(shè)新時期:改革開放與中國特色社會主義道路(解析版)
- 德陽市公務(wù)服務(wù)中心選調(diào)筆試真題2024
- 銀行股份管理暫行辦法
- 靈山縣殯葬管理暫行辦法
- 工人崗位轉(zhuǎn)管理暫行辦法
- 公路ppp項目管理辦法
- 醫(yī)藥學(xué)院標(biāo)準(zhǔn)化管理辦法
- 銀行系統(tǒng)模型管理辦法
- 生產(chǎn)現(xiàn)場變化點管理行動指南
- 中國古典小說巔峰:四大名著鑒賞學(xué)習(xí)通課后章節(jié)答案期末考試題庫2023年
- 模擬電子技術(shù)基礎(chǔ)知到章節(jié)答案智慧樹2023年蘭州石化職業(yè)技術(shù)大學(xué)
- JJF 1915-2021傾角儀校準(zhǔn)規(guī)范
- GA/T 1310-2016法庭科學(xué)筆跡鑒定意見規(guī)范
- 2023年本科招生考試
- 新入職護(hù)士培訓(xùn)考試試題及答案
- 《消防安全技術(shù)實務(wù)》課本完整版
- 北師大版七年級數(shù)學(xué)下冊 與信息技術(shù)相融合的數(shù)學(xué)教學(xué)案例 教案
- 鈍針穿刺法臨床應(yīng)用護(hù)理
- 水產(chǎn)養(yǎng)殖行業(yè)報告
評論
0/150
提交評論