這幾天在弄個小東西,要用到數(shù)據(jù)庫,以前就聽說過數(shù)據(jù)庫連接池這個概念,所以就打算在這個小東西中加入數(shù)據(jù)庫連接池。呵呵。從網(wǎng)上搜了一些資料。今天就整理一下。我搜到的設(shè)置基本上主要有兩種方法我們以MySQL+TOMCAT為例
1.把DataSource設(shè)置到我們的WEB項目中,下面詳細(xì)的介紹下:
第一步:在我們的WEB項目中的
META-INF
文件夾下建立一個
context.xml
- <? 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" ??? ??
- ??????? username = "root" ??? ??
- ??????? password = "root" ??? ??
- ??????? maxActive = "50" ??? ??
- ??????? maxIdle = "20" ??? ??
- ??????? maxWait = "10000" ? /> ??? ??
- ??
- </ 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" username="root" password="root" maxActive="50" maxIdle="20" maxWait="10000" /> </Context>
第二步:在我們的WEB項目下的
WEB-INF
文件夾下建立一個
web.xml
(如果存在了就不用了,直接修改就行了)
(這幾天測試了一下,不做這步也可以,O(∩_∩)O哈哈~省事了)
- < 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 > ??
- </ 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> </resource-ref>
第三步:我們就可以用代碼來獲取
Connection
對象了
- package ?xushun.util; ??
- ??
- import ?java.sql.*; ??
- import ?javax.sql.*; ??
- import ?javax.naming.*; ??
- ??
- public ? class ?DBHelper?{ ??
- ???? ??
- ???? public ? static ?Connection?getConnection()? throws ?SQLException,NamingException ??
- ????{ ??
- ???????? //?初始化查找命名空間 ??
- ????????Context?initContext?=? new ?InitialContext(); ??
- ????????Context?envContext?=?(Context)initContext.lookup( "java:/comp/env" ); ??
- ???????? //?找到DataSource ??
- ????????DataSource?ds?=?(DataSource)envContext.lookup( "jdbc/mysql" ); ??
- ???????? return ?ds.getConnection(); ??
- ????} ??
- }??
package xushun.util; import java.sql.*; import javax.sql.*; import javax.naming.*; public class DBHelper { public static Connection getConnection() throws SQLException,NamingException { // 初始化查找命名空間 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ì)的介紹下(測試用的JAVA代碼和上面的一樣就不帖出了):
這里我查到的設(shè)置方法就有了一點區(qū)別了。有的人把DataSource設(shè)置在Tomcat的server.xml文件的GlobalNamingResources下面,然后在context.xml中去映射。有的直接就寫在context.xml中了
先說下在server.xml添加DataSource
第一步:在Tomcat的conf中的server.xml文件中找到
- < GlobalNamingResources > ??
- ??<!--?Editable?user?database?that?can?also?be?used?by ??
- ???????UserDatabaseRealm?to?authenticate?users ??
- ??-- > ??
- ?? < Resource ? name = "UserDatabase" ? auth = "Container" ??
- ???????????? type = "org.apache.catalina.UserDatabase" ??
- ???????????? description = "User?database?that?can?be?updated?and?saved" ??
- ???????????? factory = "org.apache.catalina.users.MemoryUserDatabaseFactory" ??
- ???????????? pathname = "conf/tomcat-users.xml" ? /> ??
- </ GlobalNamingResources > ??
<GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources>
修改為
- < GlobalNamingResources > ??
- ??<!--?Editable?user?database?that?can?also?be?used?by ??
- ???????UserDatabaseRealm?to?authenticate?users ??
- ??-- > ??
- ?? < Resource ? name = "UserDatabase" ? auth = "Container" ??
- ???????????? type = "org.apache.catalina.UserDatabase" ??
- ???????????? description = "User?database?that?can?be?updated?and?saved" ??
- ???????????? factory = "org.apache.catalina.users.MemoryUserDatabaseFactory" ??
- ???????????? pathname = "conf/tomcat-users.xml" ? /> ??
- ?? < Resource ? name = "jdbc/bbs" ????? ??
- ????????? auth = "Container" ? 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" ??
- ????????? removeAbandonedTimeout = "60" ??
- ????????? logAbandoned = "true" /> ??
- </ GlobalNamingResources > ??
<GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> <Resource name="jdbc/bbs" auth="Container" 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" removeAbandonedTimeout="60" logAbandoned="true"/> </GlobalNamingResources>
第二步:在Tomcat的conf文件夾下的context.xml中加入
- < ResourceLink ? name = "jdbc/bbs" ? global = "jdbc/bbs" ? type = "javax.sql.DataSource" /> ??
<ResourceLink name="jdbc/bbs" global="jdbc/bbs" type="javax.sql.DataSource"/>
第三步:就是在WEB項目的WEB-INF中的web.xml添加
- < 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 > ??
- </ 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> </resource-ref>
還有就是在Tomcat文檔中提到的方法,直接修改context.xml文件了
在Tomcat的conf文件夾下的context.xml中加入
- < Resource ? name = "jdbc/bbs" ????? ??
- ?????????????? auth = "Container" ? 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" ??
- ?????????????? removeAbandonedTimeout = "60" ??
- ?????????????? logAbandoned = "true" /> ??
<Resource name="jdbc/bbs" auth="Container" 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" removeAbandonedTimeout="60" logAbandoned="true"/>
然后就是在WEB項目的WEB-INF中的web.xml添加
- < 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 > ??
- </ 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> </resource-ref>
就是這些了,如果有什么不太清楚的就留言,一起研究下。等以后我在搜集下資料整理出上面用到的XML文件中各個標(biāo)簽的屬性及其代表的意思。有興趣的也可以自己先查下。:-)
<td>JNDI 查找名稱</td>?????? <td>關(guān)聯(lián)的引用</td>
<td>java:comp/env</td>????? <td>應(yīng)用程序環(huán)境條目</td>
<td>java:comp/env/jdbc</td> <td>JDBC 數(shù)據(jù)源資源管理器連接工廠</td>
<td>java:comp/env/ejb</td>? <td>EJB 引用</td>
<td>java:comp/UserTransaction</td><td>UserTransaction 引用</td>
<td>java:comp/env/mail</td> <td>JavaMail 會話連接工廠</td>
<td>java:comp/env/url</td>? <td>URL 連接工廠</td>
<td>java:comp/env/jms</td>? <td>JMS 連接工廠和目標(biāo)</td>
<td>java:comp/ORB</td>????? <td>應(yīng)用程序組件之間共享的 ORB 實例</td>
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
