Tomcat6.0数据源JNDI配置

本文介绍了在Tomcat服务器中配置MySQL数据源的三种方法,包括配置文件的详细设置及常见异常处理,适用于Java Web项目的数据库连接管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<Context path="/demo" docBase="demo"
debug="5" reloadable="true" crossContext="true">
<!-- maxActive: Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to -1 for no limit.
-->

<!-- maxIdle: Maximum number of idle dB connections to retain in pool.
Set to -1 for no limit. See also the DBCP documentation on this
and the minEvictableIdleTimeMillis configuration parameter.
-->

<!-- maxWait: Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->

<!-- username and password: MySQL dB username and password for dB connections -->

<!-- driverClassName: Class name for the old mm.mysql JDBC driver is
org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
-->

<!-- url: The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test_lzy?autoReconnect=true"/>
</Context>
-------------------------------------------------------------------------------------

=======================================================================================
在Java Web开发中都要与数据库打交道,为了不频繁地打开和关闭数据库,以减少数据库操作负荷,可使数据库在开发过程中保持打开状态,在这里我们采用配置数据源的方式(JNDI),而不是传统地JDBC方式。下面就针对常规型的MySQL5.0.15和Tomcat6.0的数据源的基本配置进行简单的介绍:
首先声明,如果数据源没有配置好的话,在开发过程中会抛出诸如下列异常等:
1、org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
2、Caused by: java.sql.SQLException: No suitable driver
3、Name jdbc is not bound in this context
现在开始讲下如何配置好数据源同时也是解决上述异常的办法:
方案一:
步骤一、在Tomcat6.0解压目录conf下找到context.xml,在其中的<Context></Context> 中加入如下代码(要根据自己的情况稍加修改):
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" password="localhost" username="root" driverClassName="org.gjt.mm.mysql.Driver" url="jdbc:mysql://localhost/myznt" maxActive="100" maxIdle="30" maxWait="5000"/>
步骤二、在工程应用中/WEB-INF/下的web.xml中加入如下代码(要根据自己的情况稍加修改):
<resource-ref>
<description>MySQL DataSource</description>
<res-ref-name>jdbc/myznt</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
步骤三、把MySQL-Connector-java-3.0.12-bin.jar(可换更高版本)加到Tomcat安装目录中的lib目录下和工程中的lib目录下。
通过这三步,一个基本的数据源就配置成功了!
----------------------------------------------------------------------------------
方案二
步骤一、在Tomcat6.0解压目录conf下找到server.xml,在其中的
<GlobalNamingResources></GlobalNamingResources>中加入如下代码(要根据自己的情况稍加修改):
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" password="localhost" username="root" driverClassName="org.gjt.mm.mysql.Driver" url="jdbc:mysql://localhost/myznt" maxActive="100" maxIdle="30" maxWait="5000"/>
步骤二、在Tomcat6.0解压目录conf下找到context.xml,在其中的<Context></Context>中加入并修改成如下代码(要根据自己的情况稍加修改):
<Context path="/znt" debug="1" reloadable="true" docBase="E:\EclipseWorkPlace\MyZNT\WebRoot">
<ResourceLink global="jdbc/myznt" name="jdbc/myznt" type="javax.sql.Datasource"/>
................<!--此间可能有系统其它自配的内容,可不管-->
</Context>
步骤三、在工程中/WEB-INF/下的web.xml中加入如下代码(要根据自己的情况稍加修改):
<resource-ref>
<description>MySQL DataSource</description>
<res-ref-name>jdbc/myznt</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
步骤四、把MySQL-Connector-java-3.0.12-bin.jar(可换更高版本)加到Tomcat安装目录中的lib目录下和工程中的lib目录下。
通过以上四步就好了!
-----------------------------------------------------------
方案三(具有不稳定性,慎用)
步骤一、在Tomcat6.0解压目录conf下找到server.xml,在其中的<Host></Host>中加入如下代码(要根据自己的情况稍加修改):
<Context path="/znt" docBase="E:\EclipseWorkPlace\MyZNT\WebRoot"
debug="5" reloadable="true" crossContext="true">

<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_MysqlTest_log." suffix=".txt"
timestamp="true"/>
<Resource name="jdbc/myznt" auth="Container" type="javax.sql.DataSource" password="localhost" username="root" driverClassName="org.gjt.mm.mysql.Driver" url="jdbc:mysql://localhost/myznt" maxActive="100" maxIdle="30" maxWait="5000"/>
</Context>
步骤二、在工程中/WEB-INF/下的web.xml中加入如下代码(要根据自己的情况稍加修改):
<resource-ref>
<description>MySQL DataSource</description>
<res-ref-name>jdbc/myznt</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
步骤三、把MySQL-Connector-java-3.0.12-bin.jar(可换更高版本)加到Tomcat安装目录中的lib目录下和工程中的lib目录下。
通过以上三步,大部分时候还是起作用的,但有时会出现异常,因此不建议使用。
以上几种方案在实践中经受了测试,方案一和二比较稳定,方案三最好别用,同时只是进行了大致地归纳,其中的哪些地方没有必要或哪里欠妥还没有去测试,望读者进行批评指正。
--------------------------------------------------------
Tomcat链接数据源的基本代码
Context initContext;
try {
initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/TestDB");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet set = stmt.executeQuery("SELECT id,name,age FROM user_lzy");
while(set.next()){
System.out.println(set.getString("name"));
}
//etc.
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值