前几天为了在Tomcat5中配置MySql的数据源,在网上到处搜刮资料,最终仍未能配置成功。迫不得已,花了些银子买本书,看完后感觉收获很大。 一、配置环境变量: softe version:tomcat5.0.12/mysql4.1.7/mysql_driver---mysql-connector-java-3.1.4-beta-bin.jar 前几天为了在Tomcat5中配置MySql的数据源,在网上到处搜刮资料,最终仍未能配置成功。迫不得已,花了些银子买本书,看完后感觉收获很大。再来配置数据源简直就是小菜一碟了,现将配置过程详述如下,一来作为版书的资料,二来希望为大家提供些帮助。 如果能够看到看到如下数据,恭喜你,成功了,若没有,说明还是有问题,只好再来了(通常都是最简单的拼错字符的问题)
再来配置数据源简直就是小菜一碟了,现将配置过程详述如下,一来作为版书的资料,二来希望为大家提供些帮助。
reloadable="true" >
<Resource name="jdbc/DBConnection"
auth="Container"
type="javax.sql.DataSource" />
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
Set to 0 for no limit.
-->
<parameter>
<name>maxIdle</name>
<value>3</value>
</parameter>
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
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.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value>1234</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
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.
-->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/forumdb?autoReconnect=true</value>
</parameter>
</ResourceParams>
再次提醒:一定要放在</host>之前!
<description>DB Connection</description>
<res-ref-name>jdbc/DBConnection</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
五、编写测试jsp page。
<%@ page contentType="text/html;charset=GB2312"%>
<html>
<head><title>DataSourse Connection Test</title></head>
<body>
<%
try{
java.sql.Connection con;
Statement stmt;
ResultSet rs;
Context ctx = new InitialContext();
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/DBConnection");
con=ds.getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery("select * from member");
while(rs.next()){
out.print(rs.getInt(1);
}
rs.close();
stmt.close();
con.close();
}catch(Exception e){
out.print(e.getMessage());
}
%>
</body>
</html>
运行tomcat.bat,打开IE在地址栏中输入:http://localhost:8080/myapps/dbtest.jsp
1 zhang
文章来源于领测软件测试网 https://www.ltesting.net/
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073