测试的tomcat为apachetomcat 数据库为mysql和oracle
配置步骤如下
把数据库的JDBC驱动放入D:\apachetomcat\lib目录下
在D:\apachetomcat\conf\webxml文件中将下面代码加入到webxml中
<resourceref>
<description>DB Connection</description>
<resrefname>jdbc/mysql</resrefname>
<restype>javaxsqlDataSource</restype>
<resauth>Container</resauth>
</resourceref>
<resourceref>
<description>DB Connection</description>
<resrefname>jdbc/oracle</resrefname>
<restype>javaxsqlDataSource</restype>
<resauth>Container</resauth>
</resourceref>
在D:\apachetomcat\conf\serverxml文件中在Host节点下添加Context子节点配置如下
<Context path=/ljqtest docBase=ljqtest debug= reloadable=true crossContext=true>
<Resource name=jdbc/mysql
type=javaxsqlDataSource
username=root
password=mysql
driverClassName=orggjtmmmysqlDriver
url=jdbc:mysql://localhost:/shop
maxIdle=
maxWait=
maxActive=>
<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>
</Resource>
<Resource name=jdbc/oracle
type=javaxsqlDataSource
username=test
password=test
driverClassName=oraclejdbcdriverOracleDriver
url=jdbc:oracle:thin:@localhost::ORCL
maxIdle=
maxWait=
maxActive=>
<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>
</Resource>
</Context>
</Host>
或者
<Context path=/uimcardprj docBase=uimcardprj debug= reloadable=true crossContext=true>
<Resource name=jdbc/ycxkDB
type=javaxsqlDataSource
username=ycxk
password=xmzh
driverClassName=oraclejdbcdriverOracleDriver
url=jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = )(PORT = ))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = orcl)))
maxIdle=
maxWait=
maxActive=>
</Resource>
</Context>
</Host>
注意path为D:\apachetomcat\webapps目录下的工程名称
把web工程项目部署在D:\apachetomcat\webapps目录下
MysqlConn类获取Mysql数据源
package comljqtest;
import javasqlConnection;
import javasqlSQLException;
import javaxnamingContext;
import javaxnamingInitialContext;
import javaxsqlDataSource;
public final class MysqlConn {
// 懒汉式单例(使用时才new)
private static MysqlConn instance = null;
MysqlConn() {
}
// 延迟初始化(用到的时候才加载)(推荐)
// public static synchronized JdbcConn
// getInstance(){}>这样不好因为每调用一次就同步效率非常低
public static MysqlConn getInstance() {
if (instance == null) {
synchronized (MysqlConnclass) {// 可能会产生并发的问题我们对他进行同步
if (instance == null) {
instance = new MysqlConn();
}
}
}
return instance;
}
private DataSource getDataSource() {
DataSource ds = null;
try {
Context ctx = new InitialContext();
ds = (DataSource) ctxlookup(java:comp/env/jdbc/mysql);
} catch (Exception e) {
Systemoutprintln(数据源获取失败);
eprintStackTrace();
}
return ds;
}
public Connection getConn() {
Connection conn = null;
try {
conn = getDataSource()getConnection();
} catch (SQLException e) {
Systemoutprintln(数据库连接失败);
eprintStackTrace();
}
return conn;
}
}
OraclelConn类获取Oracle数据源
package comljqtest;
import javasqlConnection;
import javasqlSQLException;
import javaxnamingContext;
import javaxnamingInitialContext;
import javaxsqlDataSource;
public final class OracleConn {
// 懒汉式单例(使用时才new)
private static OracleConn instance = null;
OracleConn() {
}
// 延迟初始化(用到的时候才加载)(推荐)
// public static synchronized JdbcConn
// getInstance(){}>这样不好因为每调用一次就同步效率非常低
public static OracleConn getInstance() {
if (instance == null) {
synchronized (OracleConnclass) {// 可能会产生并发的问题我们对他进行同步
if (instance == null) {
instance = new OracleConn();
}
}
}
return instance;
}
private DataSource getDataSource() {
DataSource ds = null;
try {
Context ctx = new InitialContext();
ds = (DataSource) ctxlookup(java:comp/env/jdbc/mysql);
} catch (Exception e) {
Systemoutprintln(数据源获取失败);
eprintStackTrace();
}
return ds;
}
public Connection getConn() {
Connection conn = null;
try {
conn = getDataSource()getConnection();
} catch (SQLException e) {
Systemoutprintln(数据库连接失败);
eprintStackTrace();
}
return conn;
}
}
页面indexjsp:打印数据库连接对象
<body>
mysql连接对象为<%Connection conn=MysqlConngetInstance()getConn();%><%=conn %><%connclose();%><br/>
oracle连接对象为<%Connection conn=MysqlConngetInstance()getConn();%><%=conn %><%connclose();%><br/>
</body>
启动tomcat在浏览器中输入//localhost:/ljqtest/输出如下