1.把DataSource设置到我们的WEB项目中,下面详细的介绍下:
第一步:在我们的WEB项目中的META-INF文件夹下建立一个context.xml
Xml代码
1. <?xml version='1.0' encoding='utf-8'?>
2.
3. <Context>
4.
5. <Resource name="jdbc/mysql"
6. auth="Container"
7. type="javax.sql.DataSource"
8. driverClassName="com.mysql.jdbc.Driver"
9. url="jdbc:mysql://localhost/bbs"
10. username="root"
11. password="root"
12. maxActive="50"
13. maxIdle="20"
14. maxWait="10000" />
15.
16. </Context>
第二步:在我们的WEB项目下的WEB-INF文件夹下建立一个web.xml(如果存在了就不用了,直接修改就行了)
(这几天测试了一下,不做这步也可以,O(∩_∩)O哈哈~省事了)
Xml代码
1. <resource-ref>
2. <description>DB Connection</description>
3. <res-ref-name>jdbc/mysql</res-ref-name>
4. <res-type>javax.sql.DataSource</res-type>
5. <res-auth>Container</res-auth>
6. </resource-ref>
第三步:我们就可以用代码来获取Connection对象了
Java代码
1. package xushun.util;
2.
3. import java.sql.*;
4. import javax.sql.*;
5. import javax.naming.*;
6.
7. public class DBHelper {
8.
9. public static Connection getConnection() throws SQLException,NamingException
10. {
11. // 初始化查找命名空间
12. Context initContext = new InitialContext();
13. Context envContext = (Context)initContext.lookup("java:/comp/env");
14. // 找到DataSource
15. DataSource ds = (DataSource)envContext.lookup("jdbc/mysql");
16. return ds.getConnection();
17. }
18. }
2.把DataSource设置到我们的Tomcat中,下面详细的介绍下(测试用的JAVA代码和上面的一样就不帖出了):
这里我查到的设置方法就有了一点区别了。有的人把DataSource设置在Tomcat的server.xml文件的GlobalNamingResources下面,然后在context.xml中去映射。有的直接就写在context.xml中了
先说下在server.xml添加DataSource
第一步:在Tomcat的conf中的server.xml文件中找到
Xml代码
1. <GlobalNamingResources>
2. <!-- Editable user database that can also be used by
3. UserDatabaseRealm to authenticate users
4. -->
5. <Resource name="UserDatabase" auth="Container"
6. type="org.apache.catalina.UserDatabase"
7. description="User database that can be updated and saved"
8. factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
9. pathname="conf/tomcat-users.xml" />
10. </GlobalNamingResources>
修改为
Xml代码
1. <GlobalNamingResources>
2. <!-- Editable user database that can also be used by
3. UserDatabaseRealm to authenticate users
4. -->
5. <Resource name="UserDatabase" auth="Container"
6. type="org.apache.catalina.UserDatabase"
7. description="User database that can be updated and saved"
8. factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
9. pathname="conf/tomcat-users.xml" />
10. <Resource name="jdbc/bbs"
11. auth="Container" type="javax.sql.DataSource"
12. driverClassName="com.mysql.jdbc.Driver"
13. maxIdle="20"
14. maxWait="5000"
15. username="root"
16. password="admin"
17. url="jdbc:mysql://localhost:3306/bbs"
18. maxActive="100"
19. removeAbandoned="true"
20. removeAbandonedTimeout="60"
21. logAbandoned="true"/>
22. </GlobalNamingResources>
第二步:在Tomcat的conf文件夹下的context.xml中加入
Xml代码
1. <ResourceLink 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-ref-name>jdbc/mysql</res-ref-name>
4. <res-type>javax.sql.DataSource</res-type>
5. <res-auth>Container</res-auth>
6. </resource-ref>
还有就是在Tomcat文档中提到的方法,直接修改context.xml文件了
在Tomcat的conf文件夹下的context.xml中加入
Xml代码
1. <Resource name="jdbc/bbs"
2. auth="Container" type="javax.sql.DataSource"
3. driverClassName="com.mysql.jdbc.Driver"
4. maxIdle="20"
5. maxWait="5000"
6. username="root"
7. password="admin"
8. url="jdbc:mysql://localhost:3306/bbs"
9. maxActive="100"
10. removeAbandoned="true"
11. removeAbandonedTimeout="60"
12. 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. <res-type>javax.sql.DataSource</res-type>
5. <res-auth>Container</res-auth>
6. </resource-ref>
分享到:
相关推荐
context.xml, 数据库连接池配置文
Tomcat服务器配置及数据库连接池配置(个人整理版)。纯粹个人整理意见。
Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置...
详细介绍常用Java各数据库连接池配置,包括C3P0,DBCP,Proxool等。
tomcat数据库连接池配置网上文章&csdn上的代码tomcat数据库连接池配置网上文章&csdn上的代码tomcat数据库连接池配置网上文章&csdn上的代码tomcat数据库连接池配置网上文章&csdn上的代码tomcat数据库连接池配置网上...
Tomcat6.0+MyEclipse6.0+mysql5.0数据库连接池配置!.pdf
WebSphere 数据库连接池配置 WebSphere 数据库连接池配置是指在 WebSphere 应用服务器中配置数据库连接池,以便更好地管理数据库连接。下面是 WebSphere 数据库连接池配置的详细过程: 一、数据库连接池配置...
WebSphere 数据库连接池配置 在 WebSphere 环境中,配置数据库连接池是非常重要的一步骤。数据库连接池是指在应用服务器中维护的一组数据库连接的集合,使应用程序能够能够快速地获取数据库连接,从而提高应用程序...
tomcat数据库连接池配置.TXTtomcat数据库连接池配置.TXTtomcat数据库连接池配置.TXTtomcat数据库连接池配置.TXT
数据库连接池配置终极指南,详细介绍了tomcat的部署流程
proxool数据库连接池配置.doc
达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,
关于DBCP数据库连接池配置整理宣贯.pdf
orcal数据库连接池配置信息 可以直接复制到tomcat 配置文件下使用
tomcat配置数据库连接池,本次以MYSQL为例
TOMCAT数据库连接池配置说明文档,讲解很清楚。不知道的可以看看哦
Tomcat数据库连接池配置介绍AO,方便大家准确配置数据可连接池
Tomcat 5.5数据库连接池配置