博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Tomcat配置多数据源
阅读量:6629 次
发布时间:2019-06-25

本文共 4633 字,大约阅读时间需要 15 分钟。

测试的tomcat为apache-tomcat-6.0.18 ,数据库为mysql和oracle。

配置步骤如下:

1、把数据库的JDBC驱动放入D:\apache-tomcat-6.0.18\lib目录下

2、在D:\apache-tomcat-6.0.18\conf\web.xml文件中,将下面代码加入到web.xml中:

 
<
resource
-
ref
>
<
description
>
DB Connection
</
description
>
<
res
-
ref
-
name
>
jdbc
/
mysql
</
res
-
ref
-
name
>
<
res
-
type
>
javax.sql.DataSource
</
res
-
type
>
<
res
-
auth
>
Container
</
res
-
auth
>
</
resource
-
ref
>
<
resource
-
ref
>
<
description
>
DB Connection
</
description
>
<
res
-
ref
-
name
>
jdbc
/
oracle
</
res
-
ref
-
name
>
<
res
-
type
>
javax.sql.DataSource
</
res
-
type
>
<
res
-
auth
>
Container
</
res
-
auth
>
</
resource
-
ref
>

3、在D:\apache-tomcat-6.0.18\conf\server.xml文件中,在Host节点下添加Context子节点,配置如下 

 
<
Context path
=
"
/ljqtest
"
docBase
=
"
ljqtest
"
debug
=
"
5
"
reloadable
=
"
true
"
crossContext
=
"
true
"
>
<
Resource name
=
"
jdbc/mysql
"
type
=
"
javax.sql.DataSource
"
username
=
"
root
"
password
=
"
mysql
"
driverClassName
=
"
org.gjt.mm.mysql.Driver
"
url
=
"
jdbc:mysql://localhost:3306/shop
"
maxIdle
=
"
2
"
maxWait
=
"
50
"
maxActive
=
"
4
"
>
<
parameter
>
<
name
>
removeAbandoned
</
name
>
<
value
>
true
</
value
>
</
parameter
>
</
Resource
>
<
Resource name
=
"
jdbc/oracle
"
type
=
"
javax.sql.DataSource
"
username
=
"
test
"
password
=
"
test
"
driverClassName
=
"
oracle.jdbc.driver.OracleDriver
"
url
=
"
jdbc:oracle:thin:@localhost:1521:ORCL
"
maxIdle
=
"
2
"
maxWait
=
"
50
"
maxActive
=
"
4
"
>
<
parameter
>
<
name
>
removeAbandoned
</
name
>
<
value
>
true
</
value
>
</
parameter
>
</
Resource
>
</
Context
>
</
Host
>

或者

 
<
Context path
=
"
/uimcardprj
"
docBase
=
"
uimcardprj
"
debug
=
"
5
"
reloadable
=
"
true
"
crossContext
=
"
true
"
>
<
Resource name
=
"
jdbc/ycxkDB
"
type
=
"
javax.sql.DataSource
"
username
=
"
ycxk
"
password
=
"
xmzh
"
driverClassName
=
"
oracle.jdbc.driver.OracleDriver
"
url
=
"
jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 134.128.48.250)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = orcl)))
"
maxIdle
=
"
2
"
maxWait
=
"
50
"
maxActive
=
"
4
"
>
</
Resource
>
</
Context
>
</
Host
>

注意:path为D:\apache-tomcat-6.0.18\webapps目录下的工程名称 

4、把web工程项目部署在D:\apache-tomcat-6.0.18\webapps目录下 

MysqlConn类:获取Mysql数据源

 
package
com.ljq.test;
import
java.sql.Connection;
import
java.sql.SQLException;
import
javax.naming.Context;
import
javax.naming.InitialContext;
import
javax.sql.DataSource;
public
final
class
MysqlConn {
//
懒汉式单例(使用时才new)
private
static
MysqlConn instance
=
null
;
MysqlConn() {
}
//
延迟初始化(用到的时候才加载)(推荐)
//
public static synchronized JdbcConn
//
getInstance(){}->这样不好,因为每调用一次就同步,效率非常低
public
static
MysqlConn getInstance() {
if
(instance
==
null
) {
synchronized
(MysqlConn.
class
) {
//
可能会产生并发的问题,我们对他进行同步
if
(instance
==
null
) {
instance
=
new
MysqlConn();
}
}
}
return
instance;
}
private
DataSource getDataSource() {
DataSource ds
=
null
;
try
{
Context ctx
=
new
InitialContext();
ds
=
(DataSource) ctx.lookup(
"
java:comp/env/jdbc/mysql
"
);
}
catch
(Exception e) {
System.out.println(
"
数据源获取失败
"
);
e.printStackTrace();
}
return
ds;
}
public
Connection getConn() {
Connection conn
=
null
;
try
{
conn
=
getDataSource().getConnection();
}
catch
(SQLException e) {
System.out.println(
"
数据库连接失败
"
);
e.printStackTrace();
}
return
conn;
}
}

OraclelConn类:获取Oracle数据源

 
package
com.ljq.test;
import
java.sql.Connection;
import
java.sql.SQLException;
import
javax.naming.Context;
import
javax.naming.InitialContext;
import
javax.sql.DataSource;
public
final
class
OracleConn {
//
懒汉式单例(使用时才new)
private
static
OracleConn instance
=
null
;
OracleConn() {
}
//
延迟初始化(用到的时候才加载)(推荐)
//
public static synchronized JdbcConn
//
getInstance(){}->这样不好,因为每调用一次就同步,效率非常低
public
static
OracleConn getInstance() {
if
(instance
==
null
) {
synchronized
(OracleConn.
class
) {
//
可能会产生并发的问题,我们对他进行同步
if
(instance
==
null
) {
instance
=
new
OracleConn();
}
}
}
return
instance;
}
private
DataSource getDataSource() {
DataSource ds
=
null
;
try
{
Context ctx
=
new
InitialContext();
ds
=
(DataSource) ctx.lookup(
"
java:comp/env/jdbc/mysql
"
);
}
catch
(Exception e) {
System.out.println(
"
数据源获取失败
"
);
e.printStackTrace();
}
return
ds;
}
public
Connection getConn() {
Connection conn
=
null
;
try
{
conn
=
getDataSource().getConnection();
}
catch
(SQLException e) {
System.out.println(
"
数据库连接失败
"
);
e.printStackTrace();
}
return
conn;
}
}

页面index.jsp:打印数据库连接对象

 
<
body
>
mysql连接对象为:
<%
Connection conn
=
MysqlConn.getInstance().getConn();
%><%=
conn
%><%
conn.close();
%><
br
/>
oracle连接对象为:
<%
Connection conn2
=
MysqlConn.getInstance().getConn();
%><%=
conn2
%><%
conn2.close();
%><
br
/>
</
body
>

5、启动tomcat,在浏览器中输入:,输出如下:

2011030316080176.png

转载地址:http://mrgpo.baihongyu.com/

你可能感兴趣的文章
eclipse菜单栏不显示 + the system is running in lou-graphics mode问题
查看>>
【WebService】使用jaxb完成对象和xml的转换
查看>>
如何去除My97 DatePicker控件上右键弹出官网的链接 - 如何debug混淆过的代码
查看>>
输入5个学生的信息(包括学号,姓名,英语成绩,计算机语言成绩和数据库成绩), 统计各学生的总分,然后将学生信息和统计结果存入test.txt文件中...
查看>>
BZOJ2337 [HNOI2011]XOR和路径
查看>>
C# 该行已经属于另一个表 ...
查看>>
android 避免线程的重复创建(HandlerThread、线程池)
查看>>
SQL Lazy Spool Eager Spool
查看>>
type的解释
查看>>
从自动驾驶到学习机器学习:解读2017科技发展的15大趋势
查看>>
在Linux中永久并安全删除文件和目录的方法
查看>>
全民直播时代 内容监管还得靠技术
查看>>
c++ 类的对象与指针
查看>>
java-JDBC
查看>>
对.NET跨平台的随想
查看>>
Nginx Rewrite规则初探(转)
查看>>
黑魔法NSURLProtocol 可拦截网络加载
查看>>
Integration Services创建ETL包
查看>>
IE浏览器开发中遇到的问题
查看>>
php实现按utf8编码对字符串进行分割
查看>>