Tomcat快速配置数据源
2007-12-16 20:19
309 查看
Tomcat本身并没有提供数据源的实现。但借助其他一些开源的数据源实现,Tomcat可提供容器对其进行管理,程序可通过JNDI查找获得数据源。
Tomcat数据源的配置分为两种:
全局数据源:对所有的web应用都可以访问。
局部数据源:只能在某个web应用下访问。
下面开始配置
Step by Step:
1。将数据库驱动程序拷贝到tomcat/common/lib目录下面。
另,开源数据源依赖的jar文件也要拷到该目录下。
2。若配置全局数据源,修改conf/server.xml文件,如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
maxActive="100" maxIdle="30" maxWait="10000"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
username="root" password="123456"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
driverClassName="com.mysql.jdbc.Driver"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
url="jdbc:mysql://127.0.0.1:3306/test"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
characterEncoding=GBK&useUnicode=TRUE"/>
注意的是:如果有&字符,需要转移成&(XML文件规范)。不推荐配置全局数据源。
若配置局部数据源,则只需修改特定web应用下的配置文件,在Context配置节点内加上资源节点,同配置全局数据源类似,只是修改文件不同。
3。修改Web应用WEB-INF/web.xml文件,加上Resource-Def,如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<resource-ref>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<description>DB Connection</description>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<res-ref-name>jdbc/TestDB</res-ref-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<res-type>javax.sql.DataSource</res-type>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<res-auth>Container</res-auth>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</resource-ref>
4。重新启动Web应用,在Web应用中可以通过下面的代码来获取数据源和数据库连接:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Context ctx=new InitialContext();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
DataSource ds=ctx.lookup("java:comp/env/jdbc/TestDB");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Connection conn=ds.getConnection();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Statement stmt=conn.createStatement();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ResultSet rs=stmt.executeQuery("select * from newsinf");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
while(rs.next())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//............,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
Tomcat数据源的配置分为两种:
全局数据源:对所有的web应用都可以访问。
局部数据源:只能在某个web应用下访问。
下面开始配置
Step by Step:
1。将数据库驱动程序拷贝到tomcat/common/lib目录下面。
另,开源数据源依赖的jar文件也要拷到该目录下。
2。若配置全局数据源,修改conf/server.xml文件,如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
maxActive="100" maxIdle="30" maxWait="10000"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
username="root" password="123456"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
driverClassName="com.mysql.jdbc.Driver"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
url="jdbc:mysql://127.0.0.1:3306/test"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
characterEncoding=GBK&useUnicode=TRUE"/>
注意的是:如果有&字符,需要转移成&(XML文件规范)。不推荐配置全局数据源。
若配置局部数据源,则只需修改特定web应用下的配置文件,在Context配置节点内加上资源节点,同配置全局数据源类似,只是修改文件不同。
3。修改Web应用WEB-INF/web.xml文件,加上Resource-Def,如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<resource-ref>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<description>DB Connection</description>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<res-ref-name>jdbc/TestDB</res-ref-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<res-type>javax.sql.DataSource</res-type>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<res-auth>Container</res-auth>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</resource-ref>
4。重新启动Web应用,在Web应用中可以通过下面的代码来获取数据源和数据库连接:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Context ctx=new InitialContext();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
DataSource ds=ctx.lookup("java:comp/env/jdbc/TestDB");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Connection conn=ds.getConnection();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Statement stmt=conn.createStatement();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ResultSet rs=stmt.executeQuery("select * from newsinf");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
while(rs.next())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//............,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- Tomcat快速配置数据源
- Tomcat6.0快速配置数据源
- Tomcat快速配置数据源(原创)
- Tomcat数据源的配置
- Tomcat环境下配置数据源
- 在Tomcat 6 中配置JNDI数据源
- Tomcat中数据源的配置和使用
- Tomcat中配置数据源连接池
- TOMCat 5.5.9+Mysql5.0.15中的数据源配置
- 详细介绍在tomcat中配置数据源以及数据源的原理
- Spring+Mybatis+Tomcat下多数据源与 atomikos 分布式事务配置
- 数据源 jdbc tomcat oracle 配置
- Tomcat6.0配置c3p0数据源
- 登录tomcat管理页面+配置连接jdbc数据源
- Tomcat 由于数据源配置引发的错误
- Tomcat 数据源配置,中文路径乱码解决
- 配置Tomcat的连接池和数据源(以oracle数据库为例)
- Tomcat 数据源配置 druid
- Tomcat配置多数据源
- Tomcat配置数据源