转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 数据库 >> MySql >> 正文
tomcat 5.X 的mysql DBCP配置指南及相关问题小结         ★★★★

tomcat 5.X 的mysql DBCP配置指南及相关问题小结

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1032 更新时间:2009/4/22 20:56:33

1.下载Mysql JDBC驱动

Connector/J 3.0.14-stable (官方的 JDBC Driver)
下载地址:http://dev.mysql.com/downloads/connector/j/3.0.html
mm.mysql 2.0.14 (比较老的第三方 JDBC Driver,其实就是上面的官方驱动的前身,呵呵,不过现在有以前的很多文章介绍这个)
下载地址:http://prdownloads.sourceforge.net/mmmysql/mm.mysql-2.0.14-you-must-unjar-me.jar

2.安装(以Connector/J 3.0.14-stable为例说明)

解压后将mysql-connector-java-3.0.14-stable-bin.jar复制到%TOMCAT_HOME%\common\lib下!

NOTE:第三方驱动必须是jar文件,不能是zip文件;不要把jar驱动放到你web应用的/WEB-INF/lib下,也不要放在$JAVA_HOME/jre/lib/ext下,或者其它什么地方。

3.配置tomcat的相关文件;

假设:数据库:testDb 用户名:test 密码:test

(第一步)http://127.0.0.1:8080,进入tomcat页,用tomcat的Tomcat Administration--->Resources--->Data Sources页面添加,参数如下:

JNDI Name: jdbc/mysql
Data Source URL: jdbc:mysql://localhost:3306/testDb?autoReconnect=true&useUnicode=true&characterEncoding=GB2312
JDBC Driver Class: com.mysql.jdbc.Driver
User Name:test
Password:test
Max. Active Connections:4
Max. Idle Connections:2
Max. Wait for Connection:5000
Validation Query ://不添

(第二步)测试页内加入代码

你自己在mysql里建个表检单测试一下吧,我这里以userinfo表为例,在你的应用下做一个测试用的.jsp,如test.jsp,然后http访问这个jsp页,test.jsp代码如下

<%@ page import="java.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="javax.sql.*"%>
<%
try{
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");?
DataSource ds = (DataSource) ctx.lookup("jdbc/mysql");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
String strSql = " select * from userinfo";
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){
out.println(rs.getString(1));?
}
}
catch(Exception ex){
ex.printStackTrace();
}
%>

NOTE:当然数据库里得有userinfo这个表

4.问题及解决

问题1:上面的步骤我认为应是最正常的步骤,但是我按上面的做,却发现并不能将DBCP配制成功,出现org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '''''''' for onnect URL ''''null'''',这是为什么?

解决在%TOMCAT_HOME%\conf\Catalina\localhost下找到你的web应用对应的.xml文件,如test.xml,并在此文件的下添入代码:

<ResourceLink name="jdbc/mysql" global="jdbc/mysql" type="javax.sql.DataSourcer"/>

重启tomcat。

原因:Web界面配DBCP时,生成的是服务器的全局JNDI资源,查看%TOMCAT_HOME%\conf\server.xml可以得知tomcat修改了server.xml,在<server>下的<GlobalNamingResources>下添入了:

<Resource auth="Container" name="jdbc/mysql" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/mysql">
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>password</name>
<value>502</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/502?useUnicode=true&amp;characterEncoding=GB2312</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
<parameter>
<name>username</name>
<value>502</value>
</parameter>
</ResourceParams>

而用InitialContext去找server的resource当然找不到了,要想找到server的resource就得在web application中的context环境里加入一个指向该全局resource的ResourceLink。

global -->The name of the linked global resource in the global JNDI context.
name -->The name of the resource link to be created, relative to the java:comp/env context.?
type -->The fully qualified Java class name expected by the web application when it performs a lookup for this resource link.

所以,第一步+第二步+问题1的解决也就构成了一种mysql的DBCP的完成配置过程!

问题2:为什么不在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>
?很多文章不是说要配的吗?

原因:嗯,这个问题我是偶尔发现的,不加这个数据库照样连接成功,但是tomcat的文档里也是让这么配,但是我同样在文档里发现了这么一段话:

·<Resource> - Configure the name and data type of a resource made available to the application (equivalent to the inclusion of a <resource-ref> element in the web application deployment descriptor).配置resource的name和type使其针对application可用(等价于在web application deployment descriptor中包含<resource-ref>)

Resource Definitions

You can declare the characteristics of the resource to be returned for JNDI lookups of <resource-ref> and <resource-env-ref> elements in the web application deployment descriptor. You MUST also define Resource Parameters for the same resource name, to configure the object factory to be used (if not known to Tomcat already), and the properties used to configure that object factory.

For example, you can create a resource definition like this:

<Context ...>
...
<Resource name="jdbc/EmployeeDB" auth="Container"
type="javax.sql.DataSource"
description="Employees Database for HR Applications"/>
...
</Context>

This is equivalent to (等价于)the inclusion of the following element in the web application deployment descriptor (/WEB-INF/web.xml):

<resource-ref>
<description>Employees Database for HR Applications</description>
<res-ref-name>jdbc/EmployeeDB</res-ref-name>
<res-ref-type>javax.sql.DataSource</res-ref-type>
<res-auth>Container</res-auth>
</resource-ref>

于是我将web.xml内的相关元素去除,对于获取DataSource没有任何影响。

问题3:我看了许多关于DBCP的配置,里面都是说在<Context>下配,那么如何配制DBCP只应用于指定的web application呢?

解决:tomcat 5.x以后将web application的从server.xml里分离了出来,放在了%TOMCAT_HOME%\conf\Catalina\localhost下,如你的应用为test,那么在这个目录下就会有一个相应的test.xml与之对应,如:

<Context displayName="Welcome to Tomcat" docBase="G:/test" path="/test" reloadable="true">
......
</Context>

所以,我们可以直接在这里为此应用配置DBCP,在和之间加入上面原因1中的代码即可。

问题4:我如何解决中乱码问题?

解决:在配制mysql的数据库url时我们加入了useUnicode=true&characterEncoding=GB2312参数,指定数据库编码方式为GB2312,其它还需要做的就是常用的一些方式,如指定<%@ page contentType="text/html; charset=gb2312">、使用filter等,这方面的资料很多,这里不再累述。

NOTE:这里需要注意的是url中的“&“符号,如果手动在.xml内配制时,需将其转换为“&amp;”

小结:

成功的配制mysql的DBCP的方法有二种:

1. 可以按照 第一步+第二步+问题1中的解决1 配制

2.可以按照 第二步+问题3+问题1中的原因1 配制


[Web开发]Win2003完美配置Apache+IIS+Tomcat多站点  [JAVA开发]完整图解 Tomcat 5.0.28 安装笔记
[JAVA开发]Tomcat 配置技巧精华详解分析  [JAVA开发]Aspire和Tomcat使用层次数据集
[SyBase]Linux下让tomcat显示图片(jdk1.4, tomcat 4.0 或更…  [SyBase]linux下的tomcat安装
[SyBase]Linux下将Tomcat配置成为系统服务  [SyBase]linux下oracle+jdk+tomcat的配置
[ORACLE]Jboss3.0-Tomcat4.03的数据库的配置(以Oracle为例…  [ORACLE]Oracle和Tomcat端口冲突
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Sql Server  · MySql
    · Access  · ORACLE
    · SyBase  · 其他
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台