测试平台:tomcat5.5.20
步骤:
1、打开%CATALINA_HOME%/conf/server.xml,在GlobalNamingResources中间增加如下内容(红色字体部分)
<GlobalNamingResources>
<!-- Test entry for demonstration purposes -->
<Environment name="simpleValue" type="java.lang.Integer" value="30"/>
<!-- 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 auth="Container" driverClassName="com.ibm.db2.jcc.DB2Driver" maxActive="100" maxIdle="30" maxWait="5000" name="jdbc/police" password="111111" scope="Shareable" type="javax.sql.DataSource" url="jdbc:db2://192.168.5.251:50000/BZ" username="db2admin"/><!--针对db2数据库-->
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="100" maxIdle="30" maxWait="5000" name="jdbc/lt" password="123456" scope="Shareable" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/police" username="root"/><!--针对mysql-->
<Resource auth="Container" driverClassName="oracle.jdbc.OracleDriver" maxActive="100" maxIdle="30" maxWait="5000" name="jdbc/gsj" password="111111" scope="Shareable" type="javax.sql.DataSource" url="jdbc:oracle:thin:@localhost:1521:oracle9" username="simon"/><!--针对oracle-->
</GlobalNamingResources>
2、打开%CATALINA_HOME%/conf/context.xml,增加如下内容(红色字体部分)
<!-- The contents of this file will be loaded for each web application -->
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<ResourceLink name="jdbc/police" global="jdbc/police" type="javax.sql.DataSourcer"/>
<ResourceLink name="jdbc/lt" global="jdbc/lt" type="javax.sql.DataSourcer"/>
<ResourceLink name="jdbc/gsj" global="jdbc/gsj" type="javax.sql.DataSourcer"/>
</Context>
3、将相应数据库的jdbc驱动拷贝到%CATALINA_HOME%/common/lib下
4、打开你自己WEB-INF目录下的web.xml文件,增加如下内容:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/gsj</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
5、建立你自己的数据库连接类,提供源程序如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
/**
类介绍:数据库连接类
开发人员:xjxhd
创建日期:
修改日期:
*/
public class ConnDataBase {
static Logger logger =Logger.getRootLogger();
public Connection getConnection()
{
Connection conn=null;
Context ctx=null;
DataSource ds=null;
try {
ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/gsj");
logger.debug("datapools initial success!");
} catch (NamingException ex1) {
logger.debug("datapools initial fails!");
logger.debug(ex1.getMessage());
ex1.printStackTrace();
}
try {
conn = ds.getConnection();
logger.debug("database connect success!");
} catch (SQLException ex) {
logger.debug("database connect fails!");
logger.debug(ex.getMessage());
ex.printStackTrace();
}
return conn;
}
注意红色字体部分,必须要和web.xml中的res-ref-name内容对应。
上述关于tomcat数据源的配置过程不会有任何问题,如果还配置不出来,那就找找你自己的问题