我首先是在MyEclipse中将MySQL的jar包导入
其次,在Tomcat的Server.xml配置文件中设置如下:
<?xml version="1.0" encoding="UTF-8"?>
<Server>
<Listener className="org.apache.catalina.core.AprLifecycleListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/>
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
<GlobalNamingResources>
<Environment
name="simpleValue"
type="java.lang.Integer"
value="30"/>
<Resource
auth="Container"
description="User database that can be updated and saved"
name="UserDatabase"
type="org.apache.catalina.UserDatabase"
pathname="conf/tomcat-users.xml"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"/>
<Resource
name="jdbc/mysql"
type="javax.sql.DataSource"
password="123456"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="40"
maxWait="5000"
username="root"
url="jdbc:mysql://localhost:3306/db_shopping"
maxActive="40"/>
</GlobalNamingResources>
<Service
name="Catalina">
<Connector
port="8080"
redirectPort="8443"
minSpareThreads="25"
connectionTimeout="20000"
maxSpareThreads="75"
maxThreads="150">
</Connector>
<Connector
port="8009"
redirectPort="8443"
protocol="AJP/1.3">
</Connector>
<Engine
defaultHost="localhost"
name="Catalina">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
<Host
appBase="webapps"
name="localhost">
</Host>
</Engine>
</Service>
</Server>
再次,在WEB-INF下的web.xml配置如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<resource-ref>
<description>mysqlDB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
接着,在META-INF目录下新建context.xml文件,
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<ResourceLink
name="jdbc/mysql"
global="jdbc/mysql"
type="jdbc.sql.DataSource"
/>
</Context>
最后,在WebRoot下新建测试数据库连接池文件:MySQLDSTest.jsp
在浏览器中键入地址:http://localhost:8080/TestLink/MySQLDSTest.jsp
解决方法:一开始以为将链接数据库的jar包导入MyEclipse的目录下就可以了,现在才知道还要将jar包放到tomcat共享目录lib
里面。重启ok