日期:2014-05-16 浏览次数:20702 次
方法一: 在server.xml中添加配置信息,即在”<GlobalNamingResources>“标记中,声明连接池的具体信息,添加内容如下:
?
<Resource name="jdbc/lian"??? <!--资源名称-->
??? auth="Container"
??? type="javax.sql.DataSource"
??? maxActive="100" maxIdle="30" maxWait="10000" <!--活动连接数-->
??? username="root"? password="root"
??? driverClassName="com.mysql.jdbc.Driver"????? <!--驱动类-->
??? url="jdbc:mysql://localhost:3306/test"/>???? <!--数据库连接URL-->
?
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
<Resource name="jdbc/lian"
auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test"/>
</GlobalNamingResources>
?
注意: 在配置server.xml或context.xml时,由于我用的是JavaEE版本的IDE,所以我直接在IDE下的Server工程中的Tomcat v6.0 Server at localhost-config下配置server.xml或context.xml。不懂是为什么,直接在 $CATALINA_HOME/conf/server.xml或 context.xml 中配置会出现被重置到原初始状态,即每次重启tomcat, server.xml或 context.xml就会被还原到默认状态。这个我百思不得其解,望高人指教。
方法二: 在 context .xml中添加配置信息
?
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/lian"
auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test"/>
</Context>
?
方法三: 在server.xml中添加配置信息,即在最后的”</Host>“标记之前:
?
?
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false">
<Context>
<Resource name="jdbc/lian"
auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test"/>
</Context>
<Context docBase="TestDataSourse" path="/TestDataSourse"
reloadable="true" source="org.eclipse.jst.jee.server:TestDataSourse"/>
</Host>
?
?
?
?
没有加这段代码,测试也行。
?
<resource-ref>
<description>DB Connection</description> <!--资源描述名称-->
<res-ref-name>jdbc/lian</res-ref-name> <!--资源JNDI名称-->
<res-type>javax.sql.DataSource</res-type> <!--资源类型-->
<res-auth>Container</res-auth>
</resource-ref>
?
测试时:
?
在JSP、Servlet或JavaBean中用如下Java代码获取数据库连接:
Context context = new InitialContext(); // 创建初始上下文
Data