在tomcat下配置mysql的链接池步骤
来源:互联网 发布:mac win7 共享文件夹 编辑:程序博客网 时间:2024/06/05 19:42
在tomcat下配置mysql的链接池步骤如下:
1.在CATALINA_HOME/conf/server.xml中添加配置信息,声明连接池的具体信息,添加内容如下:
<!--声明连接池-->
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"/>
<!-- 对连接池的参数进行设置 -->
<ResourceParams name="jdbc/mysql">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>username</name>
<value>shopadm</value>
</parameter>
<parameter>
<name>password</name>
<value>123</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost/shopdb?useUnicode=true&charact-erEncoding=gb2312</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
</ResourceParams>
2. 在CATALINA_HOME/conf/web.xml的</web-app>前添加如下信息:
<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>
其中<res-ref-name>中的参数名必须和server.xml中声明的连接名一样。
3. 在CATALINA_HOME/conf/catalina/localhost目录下找到需要进行数据库连接的当前程序的配置信息,比如这里是shopping.xml,在这个文件中添加如下信息:
<Context …>
…
<ResourceLink name=”jdbc/mysql” global=”jdbc/mysql” type=”javax.sql.DataSource”/>
…
</Context>
1.在CATALINA_HOME/conf/server.xml中添加配置信息,声明连接池的具体信息,添加内容如下:
<!--声明连接池-->
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"/>
<!-- 对连接池的参数进行设置 -->
<ResourceParams name="jdbc/mysql">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>username</name>
<value>shopadm</value>
</parameter>
<parameter>
<name>password</name>
<value>123</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost/shopdb?useUnicode=true&charact-erEncoding=gb2312</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
</ResourceParams>
2. 在CATALINA_HOME/conf/web.xml的</web-app>前添加如下信息:
<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>
其中<res-ref-name>中的参数名必须和server.xml中声明的连接名一样。
3. 在CATALINA_HOME/conf/catalina/localhost目录下找到需要进行数据库连接的当前程序的配置信息,比如这里是shopping.xml,在这个文件中添加如下信息:
<Context …>
…
<ResourceLink name=”jdbc/mysql” global=”jdbc/mysql” type=”javax.sql.DataSource”/>
…
</Context>
2014年4月微软MVP当选名单揭晓!
对我有用[0]丢个板砖[0] 引用 | 举报 |
编辑删除
管理 - rengh8288
- rengh8288
- 等级:
- 结帖率:100%
#2 得分:20回复于: 2009-09-04 09:06:34
1、把数据库驱动包 copy 到 %CATALINA_HOME%\common\lib 下。
2、修改 %CATALINA_HOME%\conf\server.xml 文件,在 <Host> 节点下添加:
(或者在 %appName%\META-INF 下建立 context.xml,内容为上面的代码。)
3、修改 web.xml,在 <web-app> 节点下添加:
(这一步不添加页没问题。)
4、在代码中获取数据库连接:
2、修改 %CATALINA_HOME%\conf\server.xml 文件,在 <Host> 节点下添加:
1
2
3
4
5
6
7
8
9
<!-- appName 为项目名 --!>
<
Context
path
=
"/appName"
docBase
=
"appName"
auth
=
"Container"
>
<
Resource
name
=
"jdbc/MySQLDS"
scope
=
"Shareable"
type
=
"javax.sql.DataSource"
url
=
"jdbc:mysql://localhost:3306/test?useUnicode=true&charact-erEncoding=GBK"
driverClassName
=
"com.mysql.jdbc.Driver"
username
=
"root"
password
=
"111111"
maxWait
=
"3000"
maxIdle
=
"100"
maxActive
=
"10"
/>
</
Context
>
(或者在 %appName%\META-INF 下建立 context.xml,内容为上面的代码。)
3、修改 web.xml,在 <web-app> 节点下添加:
1
2
3
4
5
6
<
resource-ref
>
<
description
>Oracle Datasource example</
description
>
<
res-ref-name
>MySQLDS</
res-ref-name
>
<
res-type
>javax.sql.DataSource</
res-type
>
<
res-auth
>Container</
res-auth
>
</
resource-ref
>
(这一步不添加页没问题。)
4、在代码中获取数据库连接:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import
java.sql.Connection;
import
javax.naming.Context;
import
javax.naming.InitialContext;
import
javax.sql.DataSource;
public
class
DBUtil {
public
Connection getConnection()
throws
Exception {
Context context =
new
InitialContext();
// 获取数据源
DataSource ds = (DataSource) context.lookup(
"java:comp/env/jdbc/MySQLDS"
);
// 获取数据库连接
Connection conn = ds.getConnection();
if
(conn !=
null
&& !conn.isClosed()) {
return
conn;
}
else
{
return
null
;
}
}
}
0 0
- 在tomcat下配置mysql的链接池步骤
- proxool连接池在tomcat+mysql环境下的配置
- proxool连接池在tomcat+mysql环境下的配置
- Proxool连接池在tomcat+mysql环境下的配置
- Tomcat下配置Mysql数据库链接池(DBCP)
- JAVA链接sql server数据库的步骤(包括tomcat下配置数据源)
- Tomcat下的mysql连接池配置
- TOMCAT连接池(MYSQL)配置步骤
- 配置Tomcat的步骤
- 在linux下安装MySQL的步骤
- 在linux下编写动态链接库的步骤:
- 在linux下编写动态链接库的步骤
- 在linux下编写动态链接库的步骤
- 在linux下编写动态链接库的步骤
- 在linux下编写动态链接库的步骤
- 关于在myeclipse下链接oracle 数据库的步骤
- 在linux下编写动态链接库的步骤
- 在linux下编写动态链接库的步骤
- FragmentTabHost + pageView 实现可左右滑动的底部菜单 ...
- java往txt文件中写内容
- Objective-C中的命名前缀说明 & 与C++的不同之处 &Modern Objective-C 基础知识
- 自定义模块的配置说明
- WINCE 音频驱动之一:分类
- 在tomcat下配置mysql的链接池步骤
- 第一次使用csdn的博客,记录一个小产品的开发过程
- hdu 1753 大明A+B(高精度小数加法)
- MD5加密算法实现
- Java中的小细节
- PyCharm常用快捷键
- 面向对象、面向过程的思考
- 纯洁的友谊
- 机器学习自学入门相关推荐