這幾天在弄個小東西,要用到數據庫,以前就聽說過數據庫連接池這個概念,所以就打算在這個小東西中加入數據庫連接池。呵呵。從網上搜了一些資料。今天就整理一下。我搜到的設置基本上主要有兩種方法我們以MySQL+TOMCAT為例
1.把DataSource設置到我們的WEB項目中,下面詳細的介紹下:
第一步:在我們的WEB項目中的META-INF文件夾下建立一個context.xml
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哈哈~省事了)
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>
第三步:我們就可以用代碼來獲取Connection對象了
Java代碼 
- 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設置到我們的Tomcat中,下面詳細的介紹下(測試用的JAVA代碼和上面的一樣就不帖出了):
這里我查到的設置方法就有了一點區別了。有的人把DataSource設置在Tomcat的server.xml文件的GlobalNamingResources下面,然后在context.xml中去映射。有的直接就寫在context.xml中了
先說下在server.xml添加DataSource
第一步:在Tomcat的conf中的server.xml文件中找到
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>
修改為
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" />
- <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中加入
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添加
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中加入
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添加
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文件中各個標簽的屬性及其代表的意思。有興趣的也可以自己先查下。:-)