TOMCAT5.0配置文件
<Context path="/test" docBase="f:/testweb/"
debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_MysqlTest_log." suffix=".txt"
timestamp="true"/>
<Resource name="名字" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="名字">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>username</name>
<value>用户名</value>
</parameter>
<parameter>
<name>password</name>
<value>密码</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>驱动程序</value>
</parameter>
<parameter>
<name>url</name>
<value>连接地址</value>
</parameter>
</ResourceParams>
</Context>
连接代码
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%!
final String JNDINAME = "java:comp/env/jdbc" ;
%>
<%
Connection conn = null ;
try
{
// 初始化查找命名空间
Context ctx = new InitialContext() ;
// 找到DataSource
DataSource ds = (DataSource)ctx.lookup(JNDINAME) ;
conn = ds.getConnection() ;
}
catch(Exception e)
{
System.out.println(e) ;
}
%>
<%=conn%>
<%
// 将连接重新放回到池中
conn.close() ;
%>