说明一下步骤:
一、修改server.xml文件
<Resource
name="jdbc/testA"
type="javax.sql.DataSource"auth="Container"
password="123456"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://localhost:3306/testA"
maxActive="4"/><Resource
name="jdbc/testB"
type="javax.sql.DataSource"auth="Container"
password="123456"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://localhost:3306/testB"
maxActive="4"/>PS.有多个JNDI就添加多个二、在%TOMCAT_HOME%/conf/Catalina/localhost下新建一个与你项目文件夹同名的xml文件
例如:我的项目名为testJNDI,那就创建一个testJNDI.xml的文件。内容为:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name="jdbc/testA"
type="javax.sql.DataSource"auth="Container"
password="123456"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://localhost:3306/testA"
maxActive="4"/><Resource
name="jdbc/testB"
type="javax.sql.DataSource"auth="Container"</Context>
password="123456"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://localhost:3306/testB"
maxActive="4"/>PS.其实Resource的内容和server.xml的一致。注意,要是这错误的话,会抛出错误org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'三、修改%TOMCAT_HOME%/webapps/yourweb/WEB-INF下的web.xml文件
在</web-app>之前添加:
<resource-ref>
<description>mysql JNDI</description>
<res-ref-name>jdbc/testA</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref><resource-ref>
<description>mysql JNDI</description>
<res-ref-name>jdbc/testA</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>PS.有多个就添加多个